Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int getProfileDay( String keyType, String keyValue, String getValue, int dateValue) throws Exception
- {
- Integer rValue = 0;
- Properties profileProps = ConfigLoader.initProperties("/db.properties");
- String mongodbUser = profileProps.getProperty("mongodb.user");
- String mongoLoginDb = profileProps.getProperty("mongodb.logindb");
- String mongodbPassword = profileProps.getProperty("mongodb.password");
- String mongodbIp = profileProps.getProperty("mongodb.ip");
- int mongodbport = Integer.parseInt( profileProps.getProperty("mongodb.port") );
- String sourcedb = profileProps.getProperty("mongodb.sourcedb");
- String targetdb = profileProps.getProperty("mongodb.targetdb");
- String prof_collection = profileProps.getProperty("mongodb.prof_collection");
- MongoCredential credential
- = MongoCredential.createMongoCRCredential(mongodbUser, mongoLoginDb, mongodbPassword.toCharArray());
- MongoClient mongoClient = null;
- try {
- mongoClient = new MongoClient(new ServerAddress(mongodbIp , mongodbport), Arrays.asList(credential));
- } catch (UnknownHostException e) {
- e.printStackTrace();
- logger.error(e.toString());
- throw new Exception(e);
- }
- DB db = mongoClient.getDB(targetdb);
- DBCollection collection = db.getCollection(prof_collection);
- String dayValueFrom = DateUtil.getDateCal(DateUtil.getSysDate(), DateConstants.DAY, dateValue)+ "235959";
- String dayValueTo = DateUtil.getDateCal(DateUtil.getSysDate(), DateConstants.DAY, 0) + "000000";
- logger.debug("dayValueFrom : " + dayValueFrom);
- logger.debug("dayValueTo : " + dayValueTo );
- //----------------------------------------------------------------------
- // MATCH FIRST
- DBObject match = null;
- BasicDBObject whereQuery = new BasicDBObject();
- BasicDBObject dateQuery = new BasicDBObject();
- // MACTH FACT VALUE
- whereQuery.put("key", keyValue);
- // TOODO : MACTH FACT KEYWORD
- whereQuery.put("key_type", keyType);
- // DATE
- dateQuery.append("$gte", dayValueFrom).append("$lte", dayValueTo);
- whereQuery.put("history.I_DATETIME", dateQuery);
- match = new BasicDBObject("$match", whereQuery);
- //----------------------------------------------------------------------
- DBObject unwindFisrt = new BasicDBObject("$unwind", "$history");
- //----------------------------------------------------------------------
- DBObject groupFirst = null;
- BasicDBObject groupFirstFields = new BasicDBObject();
- groupFirstFields.append("key", "$key");
- // TODO : CHANGE TO ARG
- // getValue
- groupFirstFields.append("USER_ID", "$history.USER_ID");
- BasicDBObject groupFirstContents = new BasicDBObject();
- groupFirstContents.put("_id", groupFirstFields);
- groupFirstContents.append("USER_COUNT",new BasicDBObject("$addToSet",1));
- groupFirst = new BasicDBObject("$group",groupFirstContents);
- //----------------------------------------------------------------------
- DBObject unwindSecond = new BasicDBObject("$unwind", "$USER_COUNT");
- //----------------------------------------------------------------------
- DBObject groupSecond = null;
- BasicDBObject groupSecondContents = new BasicDBObject();
- groupSecondContents.append("_id", "$_id.key");
- groupSecondContents.append("count", new BasicDBObject("$sum",1));
- groupSecond = new BasicDBObject("$group",groupSecondContents);
- logger.debug("==========================================================");
- logger.debug("match :" + match);
- logger.debug("unwindFisrt :" + unwindFisrt);
- logger.debug("groupFirst :" + groupFirst);
- logger.debug("unwindSecond:" + unwindSecond);
- logger.debug("groupSecond :" + groupSecond);
- logger.debug("==========================================================");
- /* db.upi_test_fact_profile.aggregate(
- { $match : { "key_type" : "MAC" } }
- ,{ $unwind : "$history" }
- ,{ $group : {
- _id:{"key":"$key"
- ,"USER_ID":"$history.USER_ID"
- }
- ,USER_COUNT:{$addToSet:1}
- }
- }
- ,{ $unwind :"$USER_COUNT" }
- ,{ $group : { _id:"$_id.key", count:{"$sum":1} } }
- );
- */
- List<DBObject> pipeline = Arrays.asList(match,unwindFisrt,groupFirst,unwindSecond,groupSecond);
- // AggregationOptions aggregationOptions = AggregationOptions.builder()
- // .batchSize(100)
- // .outputMode(AggregationOptions.OutputMode.CURSOR)
- // .allowDiskUse(true)
- // .build();
- //
- // Cursor cursor = collection.aggregate(pipeline, aggregationOptions);
- AggregationOutput output = collection.aggregate(pipeline);
- try {
- for ( DBObject obj: output.results() ) {
- if(obj.get("_id") != null) {
- logger.debug(keyType + " : " + obj.get("_id").toString());
- logger.debug("CNT : " + obj.get("count").toString());
- //rValue = (Integer)obj.get("count");
- resultValue = (Integer)obj.get("count");
- }
- }
- }
- catch( Exception e )
- {
- logger.error(e.toString());
- e.printStackTrace();
- }
- finally
- {
- db = null;
- mongoClient = null;
- credential = null;
- }
- return rValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement