Java Code Examples for com.mongodb.client.FindIterable#iterator()

The following examples show how to use com.mongodb.client.FindIterable#iterator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoTable.java    From Quicksql with MIT License 7 votes vote down vote up
/**
 * Executes a "find" operation on the underlying collection.
 *
 * <p>For example,
 * <code>zipsTable.find("{state: 'OR'}", "{city: 1, zipcode: 1}")</code></p>
 *
 * @param mongoDb MongoDB connection
 * @param filterJson Filter JSON string, or null
 * @param projectJson Project JSON string, or null
 * @param fields List of fields to project; or null to return map
 * @return Enumerator of results
 */
private Enumerable<Object> find(MongoDatabase mongoDb, String filterJson,
    String projectJson, List<Map.Entry<String, Class>> fields) {
    final MongoCollection collection =
        mongoDb.getCollection(collectionName);
    final Bson filter =
        filterJson == null ? null : BsonDocument.parse(filterJson);
    final Bson project =
        projectJson == null ? null : BsonDocument.parse(projectJson);
    final Function1<Document, Object> getter = MongoEnumerator.getter(fields);
    return new AbstractEnumerable<Object>() {
        public Enumerator<Object> enumerator() {
            @SuppressWarnings("unchecked") final FindIterable<Document> cursor =
                collection.find(filter).projection(project);
            return new MongoEnumerator(cursor.iterator(), getter);
        }
    };
}
 
Example 2
Source File: MongodbHelper.java    From cassandana with Apache License 2.0 6 votes vote down vote up
@Override
public AclEntity getAcl(String topic, String username, String clientId) {
	FindIterable<Document> findIterable = aclCollection.find(eq("username", username));
	MongoCursor<Document> cursor = findIterable.iterator();
	
	AclEntity acl = null;
	if(cursor.hasNext()) {
		Document document = cursor.next();
		acl = new AclEntity();
		acl.username = username;
		acl.clientId = clientId;
		acl.topic = topic;
		acl.canPublish = (document.getInteger("write") == 1);
		acl.canSubscribe = (document.getInteger("read") == 1);
	}
	
	cursor.close();
	return acl; 
}
 
Example 3
Source File: MongoSdkBase.java    From Mongodb-WeAdmin with Apache License 2.0 6 votes vote down vote up
/**
 * 查询所有记录  代码控制返回结果数
 *
 * @param table  表连接
 * @param filter 条件  com.mongodb.client.model.Filter
 * @param sort   排序    com.mongodb.client.model.Sorts   可空
 * @return
 */
public  List<JSONObject> getAll(MongoCollection table, Bson filter, Bson sort) {
    List<JSONObject> list = new ArrayList<JSONObject>();
    FindIterable<Document> result = null;
    if (filter == null) {
        result = table.find().sort(sort);
    } else {
        result = table.find(filter).sort(sort);
    }

    MongoCursor<Document> iterator = result.iterator();

    while (iterator.hasNext()) {
        Object ddd = iterator.next();
        list.add(JSON.parseObject(diyObjectIdToJson(ddd)));
    }
    return list;
}
 
Example 4
Source File: MongoCompensableLock.java    From ByteTCC with GNU Lesser General Public License v3.0 6 votes vote down vote up
private String getTransactionOwnerInMongoDB(TransactionXid transactionXid) {
	byte[] global = transactionXid.getGlobalTransactionId();
	String instanceId = ByteUtils.byteArrayToString(global);

	try {
		String application = CommonUtils.getApplication(this.endpoint);
		String databaseName = application.replaceAll("\\W", "_");
		MongoDatabase mdb = this.mongoClient.getDatabase(databaseName);
		MongoCollection<Document> collection = mdb.getCollection(CONSTANTS_TB_LOCKS);

		FindIterable<Document> findIterable = collection.find(Filters.eq(CONSTANTS_FD_GLOBAL, instanceId));
		MongoCursor<Document> cursor = findIterable.iterator();
		if (cursor.hasNext()) {
			Document document = cursor.next();
			return document.getString("identifier");
		} else {
			return null;
		}
	} catch (RuntimeException rex) {
		logger.error("Error occurred while querying the lock-owner of transaction(gxid= {}).", instanceId, rex);
		return null;
	}
}
 
Example 5
Source File: MongoPcjDocuments.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * List the document Ids of the PCJs that are stored in MongoDB
 * for this instance of Rya.
 *
 * @return A list of pcj document Ids that hold PCJ index data for the current
 *   instance of Rya.
 */
public List<String> listPcjDocuments() {
    final List<String> pcjIds = new ArrayList<>();

    //This Bson string reads as:
    //{} - no search criteria: find all
    //{ _id: 1 } - only return the _id, which is the PCJ Id.
    final FindIterable<Document> rez = pcjCollection.find(Document.parse("{ }, { " + PCJ_METADATA_ID + ": 1 , _id: 0}"));
    try (final MongoCursor<Document> cursor = rez.iterator()) {
        while(cursor.hasNext()) {
            final Document doc = cursor.next();
            final String pcjMetadataId = doc.get(PCJ_METADATA_ID).toString();
            pcjIds.add(pcjMetadataId.replace(METADATA_ID_SUFFIX, ""));
        }
    }

    return pcjIds;
}
 
Example 6
Source File: RestaurantService.java    From HTTP-RPC with Apache License 2.0 5 votes vote down vote up
@RequestMethod("GET")
public void getRestaurants(String zipCode, String format) throws IOException {
    MongoDatabase db = mongoClient.getDatabase("test");

    FindIterable<Document> iterable = db.getCollection("restaurants").find(new Document("address.zipcode", zipCode));

    try (MongoCursor<Document> cursor = iterable.iterator()) {
        Iterable<Document> cursorAdapter = () -> cursor;

        if (format == null || format.equals("json")) {
            getResponse().setContentType("application/json");

            JSONEncoder jsonEncoder = new JSONEncoder();

            jsonEncoder.write(cursorAdapter, getResponse().getOutputStream());
        } else if (format.equals("csv")) {
            getResponse().setContentType("text/csv");

            CSVEncoder csvEncoder = new CSVEncoder(Arrays.asList("name", "address.building", "address.street", "borough", "cuisine"));

            csvEncoder.write(cursorAdapter, getResponse().getOutputStream());
        } else if (format.equals("html")) {
            getResponse().setContentType("text/html");

            TemplateEncoder templateEncoder = new TemplateEncoder(getClass().getResource("restaurants.html"));

            templateEncoder.setBaseName(getClass().getPackage().getName() + ".restaurants");
            templateEncoder.write(cursorAdapter, getResponse().getOutputStream());
        } else {
            throw new IllegalArgumentException();
        }
    }
}
 
Example 7
Source File: MongoSession.java    From presto with Apache License 2.0 5 votes vote down vote up
public MongoCursor<Document> execute(MongoTableHandle tableHandle, List<MongoColumnHandle> columns)
{
    Document output = new Document();
    for (MongoColumnHandle column : columns) {
        output.append(column.getName(), 1);
    }
    MongoCollection<Document> collection = getCollection(tableHandle.getSchemaTableName());
    FindIterable<Document> iterable = collection.find(buildQuery(tableHandle.getConstraint())).projection(output);

    if (cursorBatchSize != 0) {
        iterable.batchSize(cursorBatchSize);
    }

    return iterable.iterator();
}
 
Example 8
Source File: MongoDBLookupLoader.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private Optional<List<Map<String, Field>>> lookupValuesForRecord(Document doc) throws StageException {
  List<Map<String, Field>> lookupItems = new ArrayList<>();
  if (LOG.isTraceEnabled()) {
    LOG.trace("Going to lookup with:" + doc.toJson());
  }

  FindIterable<Document> it = mongoCollection.find(doc);
  if (it.first() != null) {
    MongoCursor<Document> ite = it.iterator();
    while (ite.hasNext()) {
      Document result = ite.next();
      if (LOG.isTraceEnabled()) {
        LOG.trace("Found document:" + result.toJson());
      }
      try {
        Map<String, Field> fields = MongoDBUtil.createFieldFromDocument(result);
        lookupItems.add(fields);
      } catch (IOException io) {
        LOG.error(Errors.MONGODB_06.getMessage(), mongoCollection, result.toJson());
        throw new OnRecordErrorException(Errors.MONGODB_10, result.toJson());
      }
    }
  } else {
    // no lookup result.
    return Optional.empty();
  }
  return Optional.of(lookupItems);
}
 
Example 9
Source File: MongoDBOplogSource.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private void prepareCursor(int timestampSeconds, int ordinal, List<OplogOpType> filterOplogTypes, int batchSize) {
  LOG.debug("Getting new cursor with offset - TimeStampInSeconds:'{}', Ordinal : '{}' and Batch Size : '{}'",timestampSeconds, ordinal, batchSize);
  FindIterable<Document> mongoCursorIterable = mongoCollection
      .find()
      //As the collection is a capped collection we use Tailable cursor which will return results in natural order in this case
      //based on ts timestamp field.
      //Tailable Await does not return and blocks, so we are using tailable.
      .cursorType(CursorType.Tailable)
      .batchSize(batchSize);

  List<Bson> andFilters = new ArrayList<>();
  //Only filter if we already have saved/initial offset specified or else both time_t and ordinal will not be -1.
  if (timestampSeconds > 0 && ordinal >= 0) {
    andFilters.add(Filters.gt(TIMESTAMP_FIELD, new BsonTimestamp(timestampSeconds, ordinal)));
  }

  if (!filterOplogTypes.isEmpty()) {
    List<Bson> oplogOptypeFilters = new ArrayList<>();
    Set<OplogOpType> oplogOpTypesSet = new HashSet<>();
    for (OplogOpType filterOplogopType : filterOplogTypes) {
      if (oplogOpTypesSet.add(filterOplogopType)) {
        oplogOptypeFilters.add(Filters.eq(OP_TYPE_FIELD, filterOplogopType.getOp()));
      }
    }
    //Add an or filter for filtered Or Types
    andFilters.add(Filters.or(oplogOptypeFilters));
  }
  //Finally and timestamp with oplog filters
  if (!andFilters.isEmpty()) {
    mongoCursorIterable = mongoCursorIterable.filter(Filters.and(andFilters));
  }
  cursor = mongoCursorIterable.iterator();
}
 
Example 10
Source File: MaxEntClassifierTrainerTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testTaskSavesValuesToMongo() throws Exception {
  FindIterable<Document> find = documents.find();
  MongoCursor<Document> iterator = find.iterator();
  int count = 0;
  ImmutableList<String> labels = ImmutableList.of("pos", "neg");
  while (iterator.hasNext()) {
    Document document = iterator.next();
    String classification = document.getString(CLASSIFICATION_FIELD);
    assertTrue(labels.contains(classification));
    count++;
  }

  assertEquals(16, count);
}
 
Example 11
Source File: SimpleMongoDBNamespaceManager.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableIteration<? extends Namespace, RyaDAOException> iterateNamespace()
        throws RyaDAOException {
    final FindIterable<Document> iterable = nsColl.find();
    final MongoCursor<Document> cursor = iterable.iterator();
    return new MongoCursorIteration(cursor);
}
 
Example 12
Source File: ChronoGraph.java    From epcis with Apache License 2.0 5 votes vote down vote up
public ArrayList<CachedChronoVertex> getCachedChronoVertices(BsonArray filters, String sortKey, Boolean isDesc,
		Integer limit) {
	ArrayList<CachedChronoVertex> vList = new ArrayList<CachedChronoVertex>();
	// Merge All the queries with $and
	CachedChronoGraph g = new CachedChronoGraph();
	BsonDocument baseQuery = new BsonDocument();
	FindIterable<BsonDocument> cursor;
	if (filters.isEmpty() == false) {
		baseQuery.put("$and", filters);
		cursor = vertices.find(baseQuery);
	} else {
		cursor = vertices.find();
	}
	if (sortKey != null) {
		if (isDesc == null)
			cursor.sort(new BsonDocument(sortKey, new BsonInt32(-1)));
		else if (isDesc == true) {
			cursor.sort(new BsonDocument(sortKey, new BsonInt32(-1)));
		} else
			cursor.sort(new BsonDocument(sortKey, new BsonInt32(1)));
	}
	if (limit != null)
		cursor.limit(limit);

	MongoCursor<BsonDocument> iter = cursor.iterator();
	while (iter.hasNext()) {
		BsonDocument doc = iter.next();
		String vid = doc.remove("_id").asString().getValue();
		CachedChronoVertex v = g.getChronoVertex(vid);
		v.setProperties(doc);
		vList.add(v);
	}
	return vList;
}
 
Example 13
Source File: MongoDBClient.java    From redtorch with MIT License 5 votes vote down vote up
/**
 * 分页查询
 * 
 * @param dbName
 * @param collectionName
 * @param filter
 * @param pageIndex      从1开始
 * @param pageSize
 * @return
 */
public List<Document> findByPage(String dbName, String collectionName, Bson filter, int pageIndex, int pageSize) {
	Bson orderBy = new BasicDBObject("_id", 1);

	List<Document> resultList = new ArrayList<Document>();
	FindIterable<Document> docs = getDatabase(dbName).getCollection(collectionName).find(filter).sort(orderBy)
			.skip((pageIndex - 1) * pageSize).limit(pageSize);

	MongoCursor<Document> mongoCursor = docs.iterator();
	while (mongoCursor.hasNext()) {
		resultList.add(mongoCursor.next());
	}

	return resultList;
}
 
Example 14
Source File: MongoTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Executes a "find" operation on the underlying collection.
 *
 * <p>For example,
 * <code>zipsTable.find("{state: 'OR'}", "{city: 1, zipcode: 1}")</code></p>
 *
 * @param mongoDb MongoDB connection
 * @param filterJson Filter JSON string, or null
 * @param projectJson Project JSON string, or null
 * @param fields List of fields to project; or null to return map
 * @return Enumerator of results
 */
private Enumerable<Object> find(MongoDatabase mongoDb, String filterJson,
    String projectJson, List<Map.Entry<String, Class>> fields) {
  final MongoCollection collection =
      mongoDb.getCollection(collectionName);
  final Bson filter =
      filterJson == null ? null : BsonDocument.parse(filterJson);
  final Bson project =
      projectJson == null ? null : BsonDocument.parse(projectJson);
  final Function1<Document, Object> getter = MongoEnumerator.getter(fields);
  return new AbstractEnumerable<Object>() {
    public Enumerator<Object> enumerator() {
      @SuppressWarnings("unchecked") final FindIterable<Document> cursor =
          collection.find(filter).projection(project);
      return new MongoEnumerator(cursor.iterator(), getter);
    }
  };
}
 
Example 15
Source File: MongoDBClient.java    From redtorch with MIT License 5 votes vote down vote up
/**
 * 通过过滤条件查询数据
 * 
 * @param dbName
 * @param collectionName
 * @param filter
 * @return
 */
public List<Document> find(String dbName, String collectionName, Bson filter) {
	List<Document> resultList = new ArrayList<Document>();
	if (filter != null) {
		FindIterable<Document> docs = mongoClient.getDatabase(dbName).getCollection(collectionName).find(filter);
		MongoCursor<Document> mongoCursor = docs.iterator();
		while (mongoCursor.hasNext()) {
			resultList.add(mongoCursor.next());
		}
	}

	return resultList;
}
 
Example 16
Source File: MongoDBClient.java    From redtorch with MIT License 5 votes vote down vote up
/**
 * 查询全部数据(可能造成溢出)
 * 
 * @param dbName
 * @param collectionName
 * @return
 */
public List<Document> find(String dbName, String collectionName) {
	List<Document> resultList = new ArrayList<Document>();
	FindIterable<Document> docs = mongoClient.getDatabase(dbName).getCollection(collectionName).find();

	MongoCursor<Document> mongoCursor = docs.iterator();
	while (mongoCursor.hasNext()) {
		resultList.add(mongoCursor.next());
	}
	return resultList;
}
 
Example 17
Source File: MongoSdkBase.java    From Mongodb-WeAdmin with Apache License 2.0 5 votes vote down vote up
/**
 * 分页查询
 * @param table    表连接
 * @param filter   条件  com.mongodb.client.model.Filter
 * @param sort     排序    com.mongodb.client.model.Sorts
 * @param pageNum
 * @param pageSize
 * @return
 */
public  JSONObject getPage(MongoCollection table, Bson filter, Bson sort, int pageNum, int pageSize) {
    int totalCount = (int) (filter == null ? table.count(): table.count(filter));
    int totalPage = (int) (totalCount / pageSize + ((totalCount % pageSize == 0) ? 0 : 1));
    if (pageNum > totalPage){ pageNum = totalPage;}
    JSONObject msg = new JSONObject();
    msg.put("pageNum", pageNum);
    msg.put("pageSize", pageSize);
    msg.put("totalCount", totalCount);
    msg.put("totalPage", totalPage);
    List<JSONObject> list = new ArrayList<JSONObject>();
    if (totalCount > 0) {
        int startRow = pageNum > 0 ? (pageNum - 1) * pageSize : 0;
        FindIterable<Document> result = null;
        if (filter == null) {
            result = table.find().sort(sort).skip(startRow).limit(pageSize);
        } else {
            result = table.find(filter).sort(sort).skip(startRow).limit(pageSize);
        }
        MongoCursor<Document> iterator = result.iterator();
        while (iterator.hasNext()) {
            Document ddd = (Document) iterator.next();
            list.add(JSON.parseObject(diyObjectIdToJson(ddd)));
        }
    }
    msg.put("data", list);
    return msg;
}
 
Example 18
Source File: SimpleMongoDBNamespaceManager.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public String getNamespace(final String prefix) throws RyaDAOException {
    final Document query = new Document().append(PREFIX, prefix);
    final FindIterable<Document> iterable = nsColl.find(query);
    String nameSpace = prefix;
    try (final MongoCursor<Document> cursor = iterable.iterator()) {
        while (cursor.hasNext()){
            final Document obj = cursor.next();
            nameSpace = (String) obj.get(NAMESPACE);
        }
    }
    return nameSpace;
}
 
Example 19
Source File: GetMongoRecord.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile input = null;

    if (context.hasIncomingConnection()) {
        input = session.get();
        if (input == null && context.hasNonLoopConnection()) {
            return;
        }
    }

    final String database = context.getProperty(DATABASE_NAME).evaluateAttributeExpressions(input).getValue();
    final String collection = context.getProperty(COLLECTION_NAME).evaluateAttributeExpressions(input).getValue();
    final String schemaName = context.getProperty(SCHEMA_NAME).evaluateAttributeExpressions(input).getValue();
    final Document query = getQuery(context, session, input);

    MongoCollection mongoCollection = clientService.getDatabase(database).getCollection(collection);

    FindIterable<Document> find = mongoCollection.find(query);
    if (context.getProperty(SORT).isSet()) {
        find = find.sort(Document.parse(context.getProperty(SORT).evaluateAttributeExpressions(input).getValue()));
    }
    if (context.getProperty(PROJECTION).isSet()) {
        find = find.projection(Document.parse(context.getProperty(PROJECTION).evaluateAttributeExpressions(input).getValue()));
    }
    if (context.getProperty(LIMIT).isSet()) {
        find = find.limit(context.getProperty(LIMIT).evaluateAttributeExpressions(input).asInteger());
    }

    MongoCursor<Document> cursor = find.iterator();

    FlowFile output = input != null ? session.create(input) : session.create();
    final FlowFile inputPtr = input;
    try {
        final Map<String, String> attributes = getAttributes(context, input, query, mongoCollection);
        try (OutputStream out = session.write(output)) {
            Map<String, String> attrs = inputPtr != null ? inputPtr.getAttributes() : new HashMap<String, String>(){{
                put("schema.name", schemaName);
            }};
            RecordSchema schema = writerFactory.getSchema(attrs, null);
            RecordSetWriter writer = writerFactory.createWriter(getLogger(), schema, out, attrs);
            long count = 0L;
            writer.beginRecordSet();
            while (cursor.hasNext()) {
                Document next = cursor.next();
                if (next.get("_id") instanceof ObjectId) {
                    next.put("_id", next.get("_id").toString());
                }
                Record record = new MapRecord(schema, next);
                writer.write(record);
                count++;
            }
            writer.finishRecordSet();
            writer.close();
            out.close();
            attributes.put("record.count", String.valueOf(count));
        } catch (SchemaNotFoundException e) {
            throw new RuntimeException(e);
        }


        output = session.putAllAttributes(output, attributes);

        session.getProvenanceReporter().fetch(output, getURI(context));
        session.transfer(output, REL_SUCCESS);
        if (input != null) {
            session.transfer(input, REL_ORIGINAL);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        getLogger().error("Error writing record set from Mongo query.", ex);
        session.remove(output);
        if (input != null) {
            session.transfer(input, REL_FAILURE);
        }
    }
}
 
Example 20
Source File: MongosSystemForTestFactory.java    From spring-data-examples with Apache License 2.0 4 votes vote down vote up
private void configureMongos() throws Exception {
	Document cr;
	MongoClientSettings options = MongoClientSettings.builder()
			.applyToSocketSettings(builder -> builder.connectTimeout(10, TimeUnit.SECONDS))
			.applyToClusterSettings(builder -> builder.hosts(Collections.singletonList(toAddress(this.config.net()))))
			.build();
	try (MongoClient mongo = MongoClients.create(options)) {
		MongoDatabase mongoAdminDB = mongo.getDatabase(ADMIN_DATABASE_NAME);

		// Add shard from the replica set list
		for (Entry<String, List<IMongodConfig>> entry : this.replicaSets
				.entrySet()) {
			String replicaName = entry.getKey();
			String command = "";
			for (IMongodConfig mongodConfig : entry.getValue()) {
				if (command.isEmpty()) {
					command = replicaName + "/";
				} else {
					command += ",";
				}
				command += mongodConfig.net().getServerAddress().getHostName()
						+ ":" + mongodConfig.net().getPort();
			}
			logger.info("Execute add shard command: {}", command);
			cr = mongoAdminDB.runCommand(new Document("addShard", command));
			logger.info(cr.toString());
		}

		logger.info("Execute list shards.");
		cr = mongoAdminDB.runCommand(new Document("listShards", 1));
		logger.info(cr.toString());

		// Enabled sharding at database level
		logger.info("Enabled sharding at database level");
		cr = mongoAdminDB.runCommand(new Document("enableSharding",
				this.shardDatabase));
		logger.info(cr.toString());

		// Create index in sharded collection
		logger.info("Create index in sharded collection");
		MongoDatabase db = mongo.getDatabase(this.shardDatabase);
		db.getCollection(this.shardCollection).createIndex(new Document(this.shardKey, 1));

		// Shard the collection
		logger.info("Shard the collection: {}.{}", this.shardDatabase, this.shardCollection);
		Document cmd = new Document();
		cmd.put("shardCollection", this.shardDatabase + "." + this.shardCollection);
		cmd.put("key", new BasicDBObject(this.shardKey, 1));
		cr = mongoAdminDB.runCommand(cmd);
		logger.info(cr.toString());

		logger.info("Get info from config/shards");
		FindIterable<Document> cursor = mongo.getDatabase("config").getCollection("shards").find();
		MongoCursor<Document> iterator = cursor.iterator();
		while (iterator.hasNext()) {
			Document item = iterator.next();
			logger.info(item.toString());
		}
	}

}