com.mongodb.client.MongoIterable Java Examples

The following examples show how to use com.mongodb.client.MongoIterable. 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: MongoDBQueryTool.java    From datax-web with MIT License 6 votes vote down vote up
/**
 * 获取DB名称列表
 *
 * @return
 */
public List<String> getDBNames() {
    MongoIterable<String> dbs = connection.listDatabaseNames();
    List<String> dbNames = new ArrayList<>();
    dbs.forEach((Block<? super String>) dbNames::add);
    return dbNames;
}
 
Example #3
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 #4
Source File: CmdIdxAccessStats.java    From mongodb-slow-operations-profiler with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public TableDto runCommand(ProfilingReader profilingReader, MongoDbAccessor mongoDbAccessor) {
    final TableDto result = new TableDto();
    final MongoDatabase database = mongoDbAccessor.getMongoDatabase(profilingReader.getDatabase());
    final MongoIterable<String> collNames = database.listCollectionNames();

    if(collNames != null){
        for(String collName : collNames){
            if(!"system.profile".equals(collName)) {
                final TableDto collStats = getIndexStats(database.getCollection(collName), profilingReader.getProfiledServerDto().getLabel());
                result.addRows(collStats);
            }
        }
    }
    return result;
}
 
Example #5
Source File: MongoDbFeatureExecutorTest.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testVerifyDataAfterFeatureExecution() throws DbFeatureException {
    // GIVEN
    final MongoIterable<String> collectionIterable = mock(MongoIterable.class);
    final MongoCursor<String> iterator = mock(MongoCursor.class);
    when(expectedDataSets.strict()).thenReturn(Boolean.FALSE);
    when(expectedDataSets.value()).thenReturn(new String[] {});
    when(expectedDataSets.orderBy()).thenReturn(new String[] {});
    when(expectedDataSets.excludeColumns()).thenReturn(new String[] {});
    when(connection.listCollectionNames()).thenReturn(collectionIterable);
    when(collectionIterable.iterator()).thenReturn(iterator);
    when(iterator.hasNext()).thenReturn(Boolean.FALSE);

    // WHEN
    final DbFeature<MongoDatabase> feature = featureExecutor.createVerifyDataAfterFeature(expectedDataSets);
    assertThat(feature, notNullValue());
    feature.execute(connection);

    // THEN
    verify(connection).listCollectionNames();
    verifyNoMoreInteractions(connection);
}
 
Example #6
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 #7
Source File: MDbOperation.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
private void applyPropertiesToCursor( MongoIterable<Document> mongoIterable, QueryProperties queryProps, 
         boolean includeMetaDataSearchLimit, boolean includeSortExpr )
 {
     if( includeMetaDataSearchLimit )
     {
         Integer searchLimit = getModel().getEffectiveMDSearchLimit( queryProps ); 
         if( searchLimit > 0 )
{
             // Apply to FindIterable or MapReduceIterable
	if ( mongoIterable instanceof FindIterable )
	{
		FindIterable<Document> findIterable = (FindIterable<Document>) mongoIterable;
		findIterable.limit( searchLimit.intValue( ) );
	}
	else if ( mongoIterable instanceof MapReduceIterable )
	{
		MapReduceIterable<Document> mapReduceIterable = (MapReduceIterable<Document>) mongoIterable;
		mapReduceIterable.limit( searchLimit.intValue( ) );
	}       
}
     }
     
     applyPropertiesToCursor( mongoIterable, queryProps, includeSortExpr );
 }
 
Example #8
Source File: LumongoIndexManager.java    From lumongo with Apache License 2.0 6 votes vote down vote up
public List<String> getIndexNames() {
	globalLock.writeLock().lock();

	try {
		ArrayList<String> indexNames = new ArrayList<>();
		log.info("Searching database <" + mongoConfig.getDatabaseName() + "> for indexes");
		MongoDatabase db = mongo.getDatabase(mongoConfig.getDatabaseName());
		MongoIterable<String> allCollections = db.listCollectionNames();

		for (String collection : allCollections) {
			if (collection.endsWith(LumongoIndex.CONFIG_SUFFIX)) {
				String indexName = collection.substring(0, collection.length() - LumongoIndex.CONFIG_SUFFIX.length());
				indexNames.add(indexName);
			}

		}

		return indexNames;
	}
	finally {
		globalLock.writeLock().unlock();
	}
}
 
Example #9
Source File: MongoMetadataDaoImpl.java    From eagle with Apache License 2.0 5 votes vote down vote up
private boolean isCollectionExists(String collectionName) {
    boolean result = false;
    MongoIterable<String> allCollections = db.listCollectionNames();
    for ( String collection : allCollections ) {
        if (collection.equals(collectionName)) {
            result = true;
            break;
        }
    }

    return result;
}
 
Example #10
Source File: MongoDb4TestTestRuleTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testAccess() {
    @SuppressWarnings("resource")
    final MongoIterable<String> databaseNames = mongoDbTestRule.getMongoClient().listDatabaseNames();
    Assert.assertNotNull(databaseNames);
    Assert.assertNotNull(databaseNames.first());
}
 
Example #11
Source File: MongoExtension.java    From jphp with Apache License 2.0 5 votes vote down vote up
@Override
public void onRegister(CompileScope scope) {
    MemoryOperation.register(new BasicDBObjectMemoryOperation());
    MemoryOperation.register(new DocumentMemoryOperation());
    MemoryOperation.register(new IndexOptionsMemoryOperation());
    MemoryOperation.register(new CountOptionsMemoryOperation());
    MemoryOperation.register(new ObjectIdMemoryOperation());

    registerWrapperClass(scope, ObjectId.class, WrapObjectId.class);
    registerWrapperClass(scope, MongoIterable.class, WrapMongoIterable.class);
    registerWrapperClass(scope, MongoCollection.class, WrapMongoCollection.class);
    registerWrapperClass(scope, MongoDatabase.class, WrapMongoDatabase.class);
    registerWrapperClass(scope, MongoClient.class, WrapMongoClient.class);
    // register classes ...
}
 
Example #12
Source File: MongoDbAccessor.java    From mongodb-slow-operations-profiler with GNU Affero General Public License v3.0 5 votes vote down vote up
private void find(String dbName, String collName, int limit){

        final MongoIterable<Document> res = getMongoDatabase(dbName).getCollection(collName)
                .find()
                .limit(limit);

        if(res != null){
            for(Document doc : res){
                LOG.info("doc: {}", JSON.serialize(doc));
            }
        }

    }
 
Example #13
Source File: CmdCollectionStats.java    From mongodb-slow-operations-profiler with GNU Affero General Public License v3.0 5 votes vote down vote up
private ArrayList<String> getCollectionNames(MongoDbAccessor mongoDbAccessor, String dbName){
    final ArrayList<String> result = new ArrayList<String>();
    final MongoIterable<String> collNames = mongoDbAccessor.getMongoDatabase(dbName).listCollectionNames();

    if(collNames != null){
       for(String collName : collNames){
           result.add(collName);
       }
    }
    return result;
}
 
Example #14
Source File: CmdIdxAccessStats.java    From mongodb-slow-operations-profiler with GNU Affero General Public License v3.0 5 votes vote down vote up
private TableDto getIndexStats(MongoCollection<Document> collection, String dbsLabel){
    final TableDto result = new TableDto();
    final MongoIterable<Document> stats = collection
            .aggregate(Arrays.asList(
                    new Document("$indexStats", new Document()),
                    new Document("$sort", new Document("accesses.ops", 1))
            ));
    final HashMap<String, Document> indexesProperties = getIndexesProperties(collection);

    for(Document doc : stats){
        LOG.info("doc: {}", JSON.serialize(doc));
        final ArrayList<Object> row = new ArrayList<Object>();
        row.add(dbsLabel);
        row.add(doc.getString("host"));
        row.add(collection.getNamespace().getDatabaseName());
        row.add(collection.getNamespace().getCollectionName());
        final String indexName = doc.getString("name");
        row.add(indexName);
        row.add(((Document)doc.get("key")).toJson());
        row.add(Boolean.toString(isTTL(indexesProperties, indexName)));
        final Object accesses = doc.get("accesses");
        if(accesses instanceof Document){
            final Document accDoc = (Document) accesses;
            row.add(accDoc.getLong("ops"));
            final Date date = accDoc.getDate("since");
            final LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            row.add(localDateTime.format(DATE_TIME_FORMATTER));
        }else{
            row.add(0L);
            row.add("");
        }

        result.addRow(row);

    }

    return result;
}
 
Example #15
Source File: MDbConnection.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static Boolean existsDatabase( MongoClient mongoClient,
           String dbName, Properties connProps ) 
       throws OdaException
   {
       if ( dbName == null )
	{
		return false;
	}
	try
	{
		MongoIterable<String> databaseNameIterable = mongoClient
				.listDatabaseNames( );
		for ( String databaseName : databaseNameIterable )
		{
			if ( dbName.equals( databaseName ) )
			{
				return true;
			}
		}
		return false;
	}
	catch ( MongoException ex )
	{
		MongoDBDriver.getLogger( ).log( Level.SEVERE,
				"Unable to connect host",
				ex ); // unable
						// to
						// get
						// db
						// names
		// user may not have permission for listDatabaseName, return true,
		// let the getDatabase() handle it.
		throw new OdaException( ex );
	}
}
 
Example #16
Source File: MongoHelper.java    From MongoExplorer with MIT License 5 votes vote down vote up
public static List<MongoCollectionRef> getCollectionNames(boolean includeSystemPrefs) {
MongoIterable<String> names = Database.listCollectionNames();
  	ArrayList<MongoCollectionRef> list = new ArrayList<>();
  	
  	for (String str : names)
  		if (includeSystemPrefs || !str.startsWith("system."))
  			list.add(new MongoCollectionRef(str));

return list;
  }
 
Example #17
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 #18
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 #19
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 #20
Source File: MongoDBClient.java    From redtorch with MIT License 5 votes vote down vote up
/**
 * 查询dbName下的所有表名
 * 
 * @param dbName
 * @return
 */
public List<String> getAllCollections(String dbName) {
	MongoIterable<String> cols = getDatabase(dbName).listCollectionNames();
	List<String> _list = new ArrayList<String>();
	for (String s : cols) {
		_list.add(s);
	}
	return _list;
}
 
Example #21
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 #22
Source File: StubbingCursor.java    From aws-athena-query-federation with Apache License 2.0 4 votes vote down vote up
public static <X> MongoIterable<X> iterate(Collection<X> result)
{
    return new MongoIterable<X>()
    {
        @Override
        public MongoCursor<X> iterator()
        {
            return new StubbingCursor<>(result.iterator());
        }

        @Override
        public X first()
        {
            throw new UnsupportedOperationException();
        }

        @Override
        public <U> MongoIterable<U> map(Function<X, U> function)
        {
            throw new UnsupportedOperationException();
        }

        @Override
        public void forEach(Block<? super X> block)
        {
            throw new UnsupportedOperationException();
        }

        @Override
        public <A extends Collection<? super X>> A into(A objects)
        {
            throw new UnsupportedOperationException();
        }

        @Override
        public MongoIterable<X> batchSize(int i)
        {
            return this;
        }
    };
}
 
Example #23
Source File: MDbOperation.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
   * Applies data set query properties and hints on DBCursor, except
   * for cursor limit.
   * @see #applyPropertiesToCursor(DBCursor,QueryProperties,boolean,boolean)
   */
  static void applyPropertiesToCursor( MongoIterable<Document> mongoIterable, QueryProperties queryProps, 
          boolean includeSortExpr )
  {
BasicDBObject sortExprObj = null;
      if( includeSortExpr )   // normally done only when executing a query to get full result set
      {
          
          try
          {
              sortExprObj = queryProps.getSortExprAsParsedObject();
          }
          catch( OdaException ex )
          {
              // log warning and ignore
              DriverUtil.getLogger().log( Level.WARNING, 
                      Messages.bind( "Unable to parse the user-defined Sort Expression: {0}", queryProps.getSortExpr() ),  //$NON-NLS-1$
                      ex );
          }
         
      }

// Map it to correct iterable object
FindIterable<Document> findIterable = null;
AggregateIterable<Document> aggregateIterable = null;
MapReduceIterable<Document> mapReduceIterable = null;
if ( mongoIterable instanceof FindIterable )
{
	findIterable = (FindIterable<Document>) mongoIterable;
}
else if ( mongoIterable instanceof AggregateIterable )
{
	aggregateIterable = (AggregateIterable<Document>) mongoIterable;
}
else if ( mongoIterable instanceof MapReduceIterable )
{
	mapReduceIterable = (MapReduceIterable<Document>) mongoIterable;
}
if ( findIterable == null
		&& aggregateIterable == null && mapReduceIterable == null )
{
	// Unknown type, return
}

if ( findIterable != null )
{
	if ( sortExprObj != null )
		findIterable.sort( sortExprObj );

	if ( queryProps.getBatchSize( ) > 0 )
		findIterable.batchSize( queryProps.getBatchSize( ) );

	if ( queryProps.getNumDocsToSkip( ) > 0 )
		findIterable.skip( queryProps.getNumDocsToSkip( ) );

	if ( queryProps.isPartialResultsOk( ) )
		findIterable.partial( true );
	// TODO: Remove hint from the UI
	// TODO: add Time out in the UI
	/*
	 * // hint is deprecated in 3.2 DBObject hintObj =
	 * queryProps.getIndexHintsAsParsedObject(); String hintValue =
	 * queryProps.getIndexHints(); if( hintObj != null )
	 * rowsCursor.hint( hintObj ); else // try to pass the hint string
	 * value as is { String hintValue = queryProps.getIndexHints(); if(
	 * ! hintValue.isEmpty() ) rowsCursor.hint( hintValue ); }
	 * findIterable.maxTime(Bytes.QUERYOPTION_NOTIMEOUT, arg1) if(
	 * queryProps.hasNoTimeOut() ) rowsCursor.addOption(
	 * Bytes.QUERYOPTION_NOTIMEOUT );
	 */
}
if ( aggregateIterable != null )
{
	if ( queryProps.getBatchSize( ) > 0 )
		aggregateIterable.batchSize( queryProps.getBatchSize( ) );

}
if ( mapReduceIterable != null )
{
	if ( sortExprObj != null )
		mapReduceIterable.sort( sortExprObj );

	if ( queryProps.getBatchSize( ) > 0 )
		mapReduceIterable.batchSize( queryProps.getBatchSize( ) );
}
  }
 
Example #24
Source File: MongoWrapper.java    From MongoDb-Sink-Connector with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieves all documents in a collection.
 */
public MongoIterable<Document> getDocuments(String topic) {
    return getCollection(topic).find();
}
 
Example #25
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();
}
 
Example #26
Source File: WrapMongoIterable.java    From jphp with Apache License 2.0 4 votes vote down vote up
public WrapMongoIterable(Environment env, MongoIterable wrappedObject) {
    super(env, wrappedObject);
}
 
Example #27
Source File: WrapMongoIterable.java    From jphp with Apache License 2.0 4 votes vote down vote up
@Signature
public WrapMongoIterable batchSize(Environment env, int size) {
    MongoIterable<Document> documents = getWrappedObject().batchSize(size);
    return new WrapMongoIterable(env, documents);
}
 
Example #28
Source File: MongoDb3TestTestRuleTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testAccess() {
    final MongoIterable<String> databaseNames = mongoDbTestRule.getMongoClient().listDatabaseNames();
    Assert.assertNotNull(databaseNames);
    Assert.assertNotNull(databaseNames.first());
}
 
Example #29
Source File: MongoDBClient.java    From redtorch with MIT License 2 votes vote down vote up
/**
 * 获取所有数据库名称列表
 * 
 * @return
 */
public MongoIterable<String> getAllDatabaseName() {
	MongoIterable<String> s = mongoClient.listDatabaseNames();
	return s;
}