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

The following examples show how to use com.mongodb.client.MongoIterable#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: MongoDbResource.java    From camel-quarkus with Apache License 2.0 7 votes vote down vote up
@GET
@Path("/collection/{collectionName}")
@Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("unchecked")
public JsonArray getCollection(@PathParam("collectionName") String collectionName) {
    JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();

    MongoIterable<Document> iterable = producerTemplate.requestBody(
            "mongodb:camelMongoClient?database=test&collection=" + collectionName
                    + "&operation=findAll&dynamicity=true&outputType=MongoIterable",
            null, MongoIterable.class);

    MongoCursor<Document> iterator = iterable.iterator();
    while (iterator.hasNext()) {
        Document document = iterator.next();
        JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
        objectBuilder.add("message", (String) document.get("message"));
        arrayBuilder.add(objectBuilder.build());
    }

    return arrayBuilder.build();
}
 
Example 2
Source File: MongoDBFactory.java    From database-transform-tool with Apache License 2.0 6 votes vote down vote up
/**
 * @decription 查询数据库表名
 * @author yi.zhang
 * @time 2017年6月30日 下午2:16:02
 * @param table	表名
 * @return
 */
public List<String> queryTables(){
	try {
		if(session==null){
			init(servers, database, schema, username, password);
		}
		MongoIterable<String> collection = session.listCollectionNames();
		if (collection == null) {
			return null;
		}
		List<String> tables = new ArrayList<String>();
		MongoCursor<String> cursor = collection.iterator();
		while(cursor.hasNext()){
			String table = cursor.next();
			tables.add(table);
		}
		return tables;
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example 3
Source File: MongoAdminClient.java    From df_data_service with Apache License 2.0 6 votes vote down vote up
public boolean collectionExists(String collectionName) {
    if (this.database == null) {
        return false;
    }

    final MongoIterable<String> iterable = database.listCollectionNames();
    try (final MongoCursor<String> it = iterable.iterator()) {
        while (it.hasNext()) {
            if (it.next().equalsIgnoreCase(collectionName)) {
                return true;
            }
        }
    }

    return false;
}
 
Example 4
Source File: WebController.java    From Mongodb-WeAdmin with Apache License 2.0 5 votes vote down vote up
/**
   * 数据库对应的数据集合列表
   * @param dbName
   * @return
   */
  @ResponseBody
  @RequestMapping("/db")
  public Res db(String dbName) {

  	if(StringUtils.isEmpty(dbName)){
  		return Res.error("dbName参数不能为空");
  	}
  	if("undefined".equals(dbName)){
	return Res.error("请关闭所有的iframe后在执行F5");
}
      MongoDatabase mogo = mongoSdkBase.getMongoDb(dbName);
      //获取所有集合的名称
      MongoIterable<String> collectionNames = mogo.listCollectionNames();
      MongoCursor<String> i = collectionNames.iterator();
      List<JSONObject> listNames = new ArrayList<JSONObject>();
      while (i.hasNext()) {
          String tableName = i.next();
	if(!Arrays.asList(TAVLEARR).contains(tableName)) {
		JSONObject t = new JSONObject();
		t.put("tableName", tableName);
		BasicDBObject obj = mongoSdkBase.getStats(dbName, tableName);
		t.put("size", ByteConvKbUtils.getPrintSize(obj.getInt("size")));
		listNames.add(t);
	}

      }
      return Res.ok().put("listNames", listNames);
  }
 
Example 5
Source File: MongoDbWriter.java    From MongoDb-Sink-Connector with Apache License 2.0 5 votes vote down vote up
private void retrieveOffsets() {
    MongoIterable<Document> documentIterable = mongoHelper.getDocuments(OFFSETS_COLLECTION);
    try (MongoCursor<Document> documents = documentIterable.iterator()) {
        while (documents.hasNext()) {
            Document doc = documents.next();
            Document id = (Document) doc.get("_id");
            String topic = id.getString("topic");
            int partition = id.getInteger("partition");
            long offset = doc.getLong("offset");

            latestOffsets.put(new TopicPartition(topic, partition), offset);
        }
    }
}
 
Example 6
Source File: ResultSetHelper.java    From ymate-platform-v2 with Apache License 2.0 5 votes vote down vote up
public static <T extends IEntity> List<T> toEntities(Class<T> entity, MongoIterable<Document> iterable) throws Exception {
    MongoCursor<Document> _documentIt = iterable.iterator();
    List<T> _resultSet = new ArrayList<T>();
    while (_documentIt.hasNext()) {
        _resultSet.add(toEntity(entity, _documentIt.next()));
    }
    return _resultSet;
}
 
Example 7
Source File: MongoCollectionImpl.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
private <V> void fetch(MongoIterable<V> iterable, List<V> results) {
    try (MongoCursor<V> cursor = iterable.iterator()) {
        while (cursor.hasNext()) {
            results.add(cursor.next());
        }
    }
}
 
Example 8
Source File: QueryResultIterator.java    From sql-to-mongo-db-query-converter with Apache License 2.0 4 votes vote down vote up
public QueryResultIterator(MongoIterable<T> mongoIterable) {
    this.mongoCursor = mongoIterable.iterator();
}