public List<DBObject> getSourceBrowsers() { DBObject groupFields = new BasicDBObject("_id", "$device"); groupFields.put("sum", new BasicDBObject("$sum", "$sum")); DBObject group = new BasicDBObject("$group", groupFields); DBObject sort = new BasicDBObject("$sort", new BasicDBObject("sum", 1)); List<DBObject> pipeline = Arrays.asList(group, sort); // allowDiskUse AggregationOptions options = AggregationOptions.builder().allowDiskUse(true).batchSize(10000).build(); Cursor cursor = device.aggregate(pipeline, options); List<DBObject> list = new ArrayList<DBObject>(); while (cursor.hasNext()) { DBObject object = cursor.next(); DBObject newObj = new BasicDBObject(); newObj.put("device", object.get("_id")); newObj.put("sum", object.get("sum")); list.add(newObj); } cursor.close(); return list; }
@Override public void close() { if (__cursor != null) { __cursor.close(); } else { super.close(); } }
// 获得一天的转换漏斗 public List<DBObject> getConversionFunnel(String date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); DBObject match = null; try { match = new BasicDBObject("$match", new BasicDBObject("date", sdf.parse(date))); } catch (ParseException e) { } DBObject groupFields = new BasicDBObject("_id", ""); groupFields.put("step1sum", new BasicDBObject("$sum", "$step1")); groupFields.put("step2sum", new BasicDBObject("$sum", "$step2")); groupFields.put("step3sum", new BasicDBObject("$sum", "$step3")); DBObject group = new BasicDBObject("$group", groupFields); // DBObject sort = new BasicDBObject("$sort", new BasicDBObject("sum", 1)); List<DBObject> pipeline = Arrays.asList(match, group); // allowDiskUse AggregationOptions options = AggregationOptions.builder().allowDiskUse(true).batchSize(10000).build(); Cursor cursor = CF.aggregate(pipeline, options); List<DBObject> list = new ArrayList<DBObject>(); while (cursor.hasNext()) { DBObject object = cursor.next(); DBObject newObj = new BasicDBObject(); // DBObject _id = (DBObject)object.get("_id"); // newObj.put("date",sdf.format(_id.get("$date"))); DBObject newObj1 = new BasicDBObject(); newObj1.put("step", "访问本站"); newObj1.put("num", object.get("step1sum")); list.add(newObj1); DBObject newObj2 = new BasicDBObject(); newObj2.put("step", "浏览商品"); newObj2.put("num", object.get("step2sum")); list.add(newObj2); DBObject newObj3 = new BasicDBObject(); newObj3.put("step", "完成交易"); newObj3.put("num", object.get("step3sum")); list.add(newObj3); } cursor.close(); return list; }