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 execute() throws TranslatorException {
    this.visitor = new MongoDBSelectVisitor(this.executionFactory, this.metadata);
    this.visitor.visitNode(this.command);

    if (!this.visitor.exceptions.isEmpty()) {
      throw this.visitor.exceptions.get(0);
    }

    LogManager.logInfo(LogConstants.CTX_CONNECTOR, this.command);

    DBCollection collection =
        this.mongoDB.getCollection(this.visitor.mongoDoc.getTargetTable().getName());
    if (collection != null) {
      // TODO: check to see how to pass the hint
      ArrayList<DBObject> ops = new ArrayList<DBObject>();
      buildAggregate(ops, "$project", this.visitor.unwindProject); // $NON-NLS-1$

      if (this.visitor.project.isEmpty()) {
        throw new TranslatorException(
            MongoDBPlugin.Event.TEIID18025, MongoDBPlugin.Util.gs(MongoDBPlugin.Event.TEIID18025));
      }

      if (this.visitor.projectBeforeMatch) {
        buildAggregate(ops, "$project", this.visitor.project); // $NON-NLS-1$
      }

      if (!this.visitor.unwindTables.isEmpty()) {
        for (String ref : this.visitor.unwindTables) {
          buildAggregate(ops, "$unwind", "$" + ref); // $NON-NLS-1$ //$NON-NLS-2$
        }
      }
      buildAggregate(ops, "$match", this.visitor.match); // $NON-NLS-1$

      buildAggregate(ops, "$group", this.visitor.group); // $NON-NLS-1$
      buildAggregate(ops, "$match", this.visitor.having); // $NON-NLS-1$

      if (!this.visitor.projectBeforeMatch) {
        buildAggregate(ops, "$project", this.visitor.project); // $NON-NLS-1$
      }

      buildAggregate(ops, "$sort", this.visitor.sort); // $NON-NLS-1$
      buildAggregate(ops, "$skip", this.visitor.skip); // $NON-NLS-1$
      buildAggregate(ops, "$limit", this.visitor.limit); // $NON-NLS-1$

      try {
        AggregationOptions options =
            AggregationOptions.builder()
                .batchSize(this.executionContext.getBatchSize())
                .outputMode(AggregationOptions.OutputMode.CURSOR)
                .allowDiskUse(this.executionFactory.useDisk())
                .build();
        this.results = collection.aggregate(ops, options);
      } catch (MongoException e) {
        throw new TranslatorException(e);
      }
    }
  }
Example #3
0
  // 获得一天的转换漏斗
  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;
  }
 @Override
 public MorphiaIterator<U, U> aggregate(final Class<U> target) {
   return aggregate(target, AggregationOptions.builder().build(), collection.getReadPreference());
 }
 @Override
 public MorphiaIterator<U, U> out(final String collectionName, final Class<U> target) {
   return out(collectionName, target, AggregationOptions.builder().build());
 }