메뉴 건너뛰기

Cloudera, BigData, Semantic IoT, Hadoop, NoSQL

Cloudera CDH/CDP 및 Hadoop EcoSystem, Semantic IoT등의 개발/운영 기술을 정리합니다. gooper@gooper.com로 문의 주세요.


* MongoDB shell 구문을 Java Code로 표현하면 아래와 같다.

 db.employee.aggregate({$unwind: '$dp.fin.Record'},       
 {$match:{"dp.mon":"patch.metrics",'dp.fin.Record':{$exists:1}}},      
 {$group:{_id: '$dp.fin.Record', count:{$sum:1}}},      
 {$project:{count:'$count'}},   {$group:{_id:'Total
 Count',total:{$sum:'$count'}}});


-->

DBCollection table=null;
MongoClient mongoClient=null;
DB db = null;

mongoClient = new MongoClient(new ServerAddress(db_server, Integer.parseInt(db_port)));
db = mongoClient.getDB(db_name);
table = db.getCollection(collection_name);


//Forming Unwind parts
DBObject unwind = new BasicDBObject("$unwind","$dp.fin.record");

//Forming Match parts
DBObject match = new BasicDBObject();
match.put("dp.mon","patch.metrics");
match.put("dp.fin.Record", new BasicDBObject("$exists",1));

//Forming Group parts
DBObject group1 = new BasicDBObject();
group1.put("_id", "$dp.fin.Record");
group1.put("count", new BasicDBObject("$sum", 1));

//Forming Project parts
DBObject project = new BasicDBObject();
project.put("count", "$count");

//Forming Group parts
DBObject group2 = new BasicDBObject();
group2.put("_id", "Total Count");
group2.put("total", new BasicDBObject("$sum", "$count"));


/**
 * Executing aggregation 
 */
AggregationOutput output = table.getCollection("employee").aggregate(unwind,				                                                                            
          new BasicDBObject("$match",match),			                                                                            
          new BasicDBObject("$group",group1),				                                                                            
          new BasicDBObject("$project",project),				                                                                            
          new BasicDBObject("$group",group2));

// 결과확인		
System.out.println("output : "+output);;


위로