Advertisement
Guest User

Untitled

a guest
Jul 15th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. public int getProfileDay( String keyType, String keyValue, String getValue, int dateValue) throws Exception
  2. {
  3. Integer rValue = 0;
  4.  
  5. Properties profileProps = ConfigLoader.initProperties("/db.properties");
  6.  
  7. String mongodbUser = profileProps.getProperty("mongodb.user");
  8. String mongoLoginDb = profileProps.getProperty("mongodb.logindb");
  9. String mongodbPassword = profileProps.getProperty("mongodb.password");
  10. String mongodbIp = profileProps.getProperty("mongodb.ip");
  11. int mongodbport = Integer.parseInt( profileProps.getProperty("mongodb.port") );
  12.  
  13. String sourcedb = profileProps.getProperty("mongodb.sourcedb");
  14. String targetdb = profileProps.getProperty("mongodb.targetdb");
  15.  
  16. String prof_collection = profileProps.getProperty("mongodb.prof_collection");
  17.  
  18. MongoCredential credential
  19. = MongoCredential.createMongoCRCredential(mongodbUser, mongoLoginDb, mongodbPassword.toCharArray());
  20.  
  21. MongoClient mongoClient = null;
  22.  
  23. try {
  24.  
  25. mongoClient = new MongoClient(new ServerAddress(mongodbIp , mongodbport), Arrays.asList(credential));
  26.  
  27. } catch (UnknownHostException e) {
  28.  
  29. e.printStackTrace();
  30. logger.error(e.toString());
  31. throw new Exception(e);
  32.  
  33. }
  34.  
  35. DB db = mongoClient.getDB(targetdb);
  36.  
  37. DBCollection collection = db.getCollection(prof_collection);
  38.  
  39. String dayValueFrom = DateUtil.getDateCal(DateUtil.getSysDate(), DateConstants.DAY, dateValue)+ "235959";
  40. String dayValueTo = DateUtil.getDateCal(DateUtil.getSysDate(), DateConstants.DAY, 0) + "000000";
  41.  
  42. logger.debug("dayValueFrom : " + dayValueFrom);
  43. logger.debug("dayValueTo : " + dayValueTo );
  44.  
  45. //----------------------------------------------------------------------
  46. // MATCH FIRST
  47. DBObject match = null;
  48. BasicDBObject whereQuery = new BasicDBObject();
  49. BasicDBObject dateQuery = new BasicDBObject();
  50.  
  51. // MACTH FACT VALUE
  52. whereQuery.put("key", keyValue);
  53.  
  54. // TOODO : MACTH FACT KEYWORD
  55. whereQuery.put("key_type", keyType);
  56.  
  57. // DATE
  58. dateQuery.append("$gte", dayValueFrom).append("$lte", dayValueTo);
  59. whereQuery.put("history.I_DATETIME", dateQuery);
  60.  
  61. match = new BasicDBObject("$match", whereQuery);
  62.  
  63. //----------------------------------------------------------------------
  64. DBObject unwindFisrt = new BasicDBObject("$unwind", "$history");
  65.  
  66. //----------------------------------------------------------------------
  67. DBObject groupFirst = null;
  68.  
  69. BasicDBObject groupFirstFields = new BasicDBObject();
  70.  
  71. groupFirstFields.append("key", "$key");
  72.  
  73. // TODO : CHANGE TO ARG
  74. // getValue
  75. groupFirstFields.append("USER_ID", "$history.USER_ID");
  76.  
  77. BasicDBObject groupFirstContents = new BasicDBObject();
  78.  
  79. groupFirstContents.put("_id", groupFirstFields);
  80. groupFirstContents.append("USER_COUNT",new BasicDBObject("$addToSet",1));
  81.  
  82. groupFirst = new BasicDBObject("$group",groupFirstContents);
  83. //----------------------------------------------------------------------
  84. DBObject unwindSecond = new BasicDBObject("$unwind", "$USER_COUNT");
  85.  
  86. //----------------------------------------------------------------------
  87. DBObject groupSecond = null;
  88. BasicDBObject groupSecondContents = new BasicDBObject();
  89. groupSecondContents.append("_id", "$_id.key");
  90. groupSecondContents.append("count", new BasicDBObject("$sum",1));
  91.  
  92. groupSecond = new BasicDBObject("$group",groupSecondContents);
  93.  
  94. logger.debug("==========================================================");
  95. logger.debug("match :" + match);
  96. logger.debug("unwindFisrt :" + unwindFisrt);
  97. logger.debug("groupFirst :" + groupFirst);
  98. logger.debug("unwindSecond:" + unwindSecond);
  99. logger.debug("groupSecond :" + groupSecond);
  100. logger.debug("==========================================================");
  101.  
  102. /* db.upi_test_fact_profile.aggregate(
  103. { $match : { "key_type" : "MAC" } }
  104. ,{ $unwind : "$history" }
  105. ,{ $group : {
  106. _id:{"key":"$key"
  107. ,"USER_ID":"$history.USER_ID"
  108. }
  109. ,USER_COUNT:{$addToSet:1}
  110. }
  111. }
  112. ,{ $unwind :"$USER_COUNT" }
  113. ,{ $group : { _id:"$_id.key", count:{"$sum":1} } }
  114. );
  115. */
  116.  
  117. List<DBObject> pipeline = Arrays.asList(match,unwindFisrt,groupFirst,unwindSecond,groupSecond);
  118.  
  119. // AggregationOptions aggregationOptions = AggregationOptions.builder()
  120. // .batchSize(100)
  121. // .outputMode(AggregationOptions.OutputMode.CURSOR)
  122. // .allowDiskUse(true)
  123. // .build();
  124. //
  125. // Cursor cursor = collection.aggregate(pipeline, aggregationOptions);
  126. AggregationOutput output = collection.aggregate(pipeline);
  127.  
  128.  
  129. try {
  130.  
  131. for ( DBObject obj: output.results() ) {
  132.  
  133. if(obj.get("_id") != null) {
  134. logger.debug(keyType + " : " + obj.get("_id").toString());
  135. logger.debug("CNT : " + obj.get("count").toString());
  136.  
  137. //rValue = (Integer)obj.get("count");
  138. resultValue = (Integer)obj.get("count");
  139.  
  140. }
  141. }
  142.  
  143. }
  144. catch( Exception e )
  145. {
  146. logger.error(e.toString());
  147. e.printStackTrace();
  148. }
  149. finally
  150. {
  151. db = null;
  152. mongoClient = null;
  153. credential = null;
  154. }
  155.  
  156. return rValue;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement