Java Code Examples for com.mongodb.client.MongoDatabase#runCommand()

The following examples show how to use com.mongodb.client.MongoDatabase#runCommand() . 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: MongoDocumentStorage.java    From lumongo with Apache License 2.0 6 votes vote down vote up
public MongoDocumentStorage(MongoClient mongoClient, String indexName, String dbName, String rawCollectionName, boolean sharded) {
	this.mongoClient = mongoClient;
	this.indexName = indexName;
	this.database = dbName;
	this.rawCollectionName = rawCollectionName;

	MongoDatabase storageDb = mongoClient.getDatabase(database);
	MongoCollection<Document> coll = storageDb.getCollection(ASSOCIATED_FILES + "." + FILES);
	coll.createIndex(new Document(ASSOCIATED_METADATA + "." + DOCUMENT_UNIQUE_ID_KEY, 1));
	coll.createIndex(new Document(ASSOCIATED_METADATA + "." + FILE_UNIQUE_ID_KEY, 1));

	if (sharded) {

		MongoDatabase adminDb = mongoClient.getDatabase(MongoConstants.StandardDBs.ADMIN);
		Document enableCommand = new Document();
		enableCommand.put(MongoConstants.Commands.ENABLE_SHARDING, database);
		adminDb.runCommand(enableCommand);

		shardCollection(storageDb, adminDb, rawCollectionName);
		shardCollection(storageDb, adminDb, ASSOCIATED_FILES + "." + CHUNKS);
	}
}
 
Example 2
Source File: MDbConnection.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
static void authenticateDB( MongoDatabase mongoDb, Properties connProps )
    throws OdaException
{
    
    try
    {
        mongoDb.runCommand( new Document( "ping", 1 ) );
    }
    catch( Exception ex )
    {
        OdaException odaEx = new OdaException( ex );
        String username = MongoDBDriver.getUserName( connProps );   
        
        MongoDBDriver.getLogger().info( 
                Messages.bind( "Unable to authenticate user (${0}) in database (${1}).\n ${2}",  //$NON-NLS-1$
                        new Object[]{ username, mongoDb, odaEx.getCause().getMessage() } ));
        throw odaEx;
    }
}
 
Example 3
Source File: MongoCollectionStats.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings( "rawtypes" )
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	MongoDatabase	db	= getMongoDatabase( _session, argStruct );
	
	String collection	= getNamedStringParam(argStruct, "collection", null);
	if ( collection == null )
		throwException(_session, "please specify a collection");
	
	try{
		Document result	= db.runCommand( new Document("collection",collection).append( "verbose", true ) );
		return tagUtils.convertToCfData((Map)result);	 	
	} catch (MongoException me){
		throwException(_session, me.getMessage());
		return null;
	}

}
 
Example 4
Source File: MongoDatabaseRunCmd.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings( "rawtypes" )
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	MongoDatabase db	= getMongoDatabase( _session, argStruct );
	cfData	cmdData	= getNamedParam(argStruct, "cmd", null );
	if ( cmdData == null )
		throwException(_session, "please specify the cmd parameter");
	
	try{
		Document cmr;
		
		if ( cmdData.getDataType() == cfData.CFSTRUCTDATA )
			cmr	= db.runCommand( getDocument( (cfStructData) cmdData ) );
		else
			cmr	= db.runCommand( new Document( cmdData.getString(), true ) );
		
		return tagUtils.convertToCfData((Map)cmr);
	} catch (MongoException me){
		throwException(_session, me.getMessage());
		return null;
	}
}
 
Example 5
Source File: MDbOperation.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
static Iterable<Document> callDBCommand( MongoDatabase connectedDB,
         QueryProperties queryProps ) throws OdaException
 {
     if( ! queryProps.hasRunCommand() )
         return null;
     DBObject command = queryProps.getOperationExprAsParsedObject( false );
     if( command == null )
         return null;
     
     try
     {
         Document documentCommand = QueryProperties
		.getDocument( (BasicDBObject) command );
Document result = connectedDB.runCommand( documentCommand );
List<Document> iterable = new ArrayList<Document>( );
iterable.add( result );
return iterable;
     }
     catch( RuntimeException ex )
     {
         OdaException odaEx = new OdaException( 
                 Messages.bind( Messages.mDbOp_dbCmdFailed, queryProps.getOperationExpression() ) );
         odaEx.initCause( ex );
         throw odaEx;
     }
 }
 
Example 6
Source File: MongoDirectory.java    From lumongo with Apache License 2.0 5 votes vote down vote up
public MongoDirectory(MongoClient mongo, String ddName, String indexName, boolean sharded, int blockSize) throws MongoException, IOException {

		this.mongo = mongo;
		this.dbname = ddName;
		this.indexName = indexName;
		this.blockSize = blockSize;

		synchronized (MongoDirectory.class) {
			//get back a index number to use instead of the string
			//this is not a persisted number and is just in memory
			String key = dbname + "-" + indexName;
			Short indexNumber = indexNameToNumberMap.get(key);
			if (indexNumber == null) {
				indexNameToNumberMap.put(key, indexCount);
				indexNumber = indexCount;
				indexCount++;
			}
			this.indexNumber = indexNumber;
		}

		getFilesCollection().createIndex(new Document(FILE_NUMBER, 1));

		Document indexes = new Document();
		indexes.put(FILE_NUMBER, 1);
		indexes.put(BLOCK_NUMBER, 1);
		getBlocksCollection().createIndex(indexes);

		if (sharded) {
			String blockCollectionName = getBlocksCollection().getNamespace().getFullName();
			MongoDatabase db = mongo.getDatabase(MongoConstants.StandardDBs.ADMIN);
			Document shardCommand = new Document();
			shardCommand.put(MongoConstants.Commands.SHARD_COLLECTION, blockCollectionName);
			shardCommand.put(MongoConstants.Commands.SHARD_KEY, indexes);
			db.runCommand(shardCommand);
		}

		nameToFileMap = new ConcurrentHashMap<>();

		fetchInitialContents();
	}
 
Example 7
Source File: MongoWithReplicasTestBase.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static void initializeReplicaSet(final List<IMongodConfig> mongodConfigList) throws UnknownHostException {
    final String arbitrerAddress = "mongodb://" + mongodConfigList.get(0).net().getServerAddress().getHostName() + ":"
            + mongodConfigList.get(0).net().getPort();
    final MongoClientSettings mo = MongoClientSettings.builder()
            .applyConnectionString(new ConnectionString(arbitrerAddress)).build();

    try (MongoClient mongo = MongoClients.create(mo)) {
        final MongoDatabase mongoAdminDB = mongo.getDatabase("admin");

        Document cr = mongoAdminDB.runCommand(new Document("isMaster", 1));
        LOGGER.infof("isMaster: %s", cr);

        // Build replica set configuration settings
        final Document rsConfiguration = buildReplicaSetConfiguration(mongodConfigList);
        LOGGER.infof("replSetSettings: %s", rsConfiguration);

        // Initialize replica set
        cr = mongoAdminDB.runCommand(new Document("replSetInitiate", rsConfiguration));
        LOGGER.infof("replSetInitiate: %s", cr);

        // Check replica set status before to proceed
        await()
                .pollInterval(100, TimeUnit.MILLISECONDS)
                .atMost(1, TimeUnit.MINUTES)
                .until(() -> {
                    Document result = mongoAdminDB.runCommand(new Document("replSetGetStatus", 1));
                    LOGGER.infof("replSetGetStatus: %s", result);
                    return !isReplicaSetStarted(result);
                });
    }
}
 
Example 8
Source File: MongoAdminService.java    From cloudfoundry-service-broker with Apache License 2.0 5 votes vote down vote up
public void deleteUser(String database, String username) throws MongoServiceException {
	try {
		MongoDatabase db = client.getDatabase(database);
		Document result = db.runCommand(new BasicDBObject("dropUser", username));
		if (result.getDouble("ok") != 1.0d) {
			throw handleException(new MongoServiceException(result.toString()));
		}
	} catch (MongoException e) {
		throw handleException(e);
	}
}
 
Example 9
Source File: MongoDatabaseStats.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings( "rawtypes" )
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	MongoDatabase	db	= getMongoDatabase( _session, argStruct );

	try{
		Document result	= db.runCommand( new Document("dbStats",1) );
		return tagUtils.convertToCfData((Map)result);	 	
	} catch (MongoException me){
		throwException(_session, me.getMessage());
		return null;
	}
}
 
Example 10
Source File: MongoPool.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public int getCurrConnection() {
	MongoDatabase db = mongoClient.getDatabase("admin");
	Document serverStatus = db.runCommand(new Document("serverStatus", 1));
	Map connMap = (Map)serverStatus.get("connections");
	
	return (Integer)connMap.get("current");
}
 
Example 11
Source File: MongoDocumentStorage.java    From lumongo with Apache License 2.0 5 votes vote down vote up
private void shardCollection(MongoDatabase db, MongoDatabase adminDb, String collectionName) {
	Document shardCommand = new Document();
	MongoCollection<Document> collection = db.getCollection(collectionName);
	shardCommand.put(MongoConstants.Commands.SHARD_COLLECTION, collection.getNamespace().getFullName());
	shardCommand.put(MongoConstants.Commands.SHARD_KEY, new BasicDBObject(MongoConstants.StandardFields._ID, 1));
	adminDb.runCommand(shardCommand);
}
 
Example 12
Source File: MongoDbIO.java    From beam with Apache License 2.0 5 votes vote down vote up
private long getEstimatedSizeBytes(
    MongoClient mongoClient, String database, String collection) {
  MongoDatabase mongoDatabase = mongoClient.getDatabase(database);

  // get the Mongo collStats object
  // it gives the size for the entire collection
  BasicDBObject stat = new BasicDBObject();
  stat.append("collStats", collection);
  Document stats = mongoDatabase.runCommand(stat);

  return stats.get("size", Number.class).longValue();
}
 
Example 13
Source File: MongoDbIO.java    From beam with Apache License 2.0 5 votes vote down vote up
private long getDocumentCount(MongoClient mongoClient, String database, String collection) {
  MongoDatabase mongoDatabase = mongoClient.getDatabase(database);

  // get the Mongo collStats object
  // it gives the size for the entire collection
  BasicDBObject stat = new BasicDBObject();
  stat.append("collStats", collection);
  Document stats = mongoDatabase.runCommand(stat);

  return stats.get("count", Number.class).longValue();
}
 
Example 14
Source File: MongodManager.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private void initializeReplicaSet(final List<IMongodConfig> mongodConfigList) throws UnknownHostException, InterruptedException {
    Thread.sleep(1000);
    final MongoClientOptions mo = MongoClientOptions.builder().connectTimeout(10).build();
    final ServerAddress arbitrerAddress = new ServerAddress(mongodConfigList.get(0).net().getServerAddress().getHostName(),
            mongodConfigList.get(0).net().getPort());

    try (MongoClient mongo = new MongoClient(arbitrerAddress, mo)) {
        final MongoDatabase mongoAdminDB = mongo.getDatabase("admin");

        Document cr = mongoAdminDB.runCommand(new Document("isMaster", 1));
        LOGGER.info("isMaster: {}", cr);

        // Build replica set configuration settings
        final Document rsConfiguration = buildReplicaSetConfiguration(mongodConfigList);
        LOGGER.info("replSetSettings: {}", rsConfiguration);

        // Initialize replica set
        cr = mongoAdminDB.runCommand(new Document("replSetInitiate", rsConfiguration));
        LOGGER.info("replSetInitiate: {}", cr);

        // Check replica set status before to proceed
        int maxWaitRounds = 10;
        do {
            LOGGER.info("Waiting for 1 second...");
            Thread.sleep(1000);
            cr = mongoAdminDB.runCommand(new Document("replSetGetStatus", 1));
            LOGGER.info("replSetGetStatus: {}", cr);
            maxWaitRounds--;
        } while (!isReplicaSetStarted(cr) || maxWaitRounds != 0);

        if (!isReplicaSetStarted(cr) && maxWaitRounds == 0) {
            throw new RuntimeException("Could not initialize replica set");
        }
    }
}
 
Example 15
Source File: MongoDbFeatureExecutor.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private void executeScript(final String script, final MongoDatabase connection) {
    if (script.isEmpty()) {
        return;
    }
    final BasicDBObject command = new BasicDBObject();
    command.append("$eval", script);
    connection.runCommand(command);
}
 
Example 16
Source File: CmdCollectionStats.java    From mongodb-slow-operations-profiler with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public TableDto runCommand(ProfilingReader profilingReader, MongoDbAccessor mongoDbAccessor) {
    final TableDto table = new TableDto();

    try{
        final Document commandResultDoc = mongoDbAccessor.runCommand("admin", new BasicDBObject("hostInfo", 1));
        final Object system = commandResultDoc.get("system");
        String hostname = "";
        if (system instanceof Document) {
            final Document systemDoc = (Document) system;
            hostname = systemDoc.getString("hostname");
        }

        final ArrayList<String> collNames = getCollectionNames(mongoDbAccessor, profilingReader.getDatabase());
        final MongoDatabase db = mongoDbAccessor.getMongoDatabase(profilingReader.getDatabase());
        for(String collName : collNames){
            Document collStats = db.runCommand(new Document("collStats", collName));
            final List<Object> row = Lists.newArrayList();
            row.add(profilingReader.getProfiledServerDto().getLabel());
            row.add(hostname);
            row.add(profilingReader.getDatabase());
            row.add(collName);
            row.add(collStats.getBoolean("sharded"));
            row.add(Util.getNumber(collStats, "size",0));
            row.add(collStats.getInteger("count"));
            row.add(Util.getNumber(collStats, "avgObjSize",0));
            row.add(Util.getNumber(collStats, "storageSize",0));
            row.add(collStats.getBoolean("capped"));
            row.add(collStats.getInteger("nindexes"));
            row.add(Util.getNumber(collStats, "totalIndexSize",0));
            row.add(((Document)collStats.get("indexSizes")).toJson());
            table.addRow(row);
        }
}catch (Exception e){
    LOG.warn("Exception while running command", e);
}

    return table;
}
 
Example 17
Source File: MongoDbReadWriteIT.java    From beam with Apache License 2.0 4 votes vote down vote up
@Before
public void init() {
  sqlEnv = BeamSqlEnv.inMemory(new MongoDbTableProvider());
  MongoDatabase db = client.getDatabase(database);
  db.runCommand(new BasicDBObject().append("profile", 2));
}
 
Example 18
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());
		}
	}

}
 
Example 19
Source File: MongodbSourceTaskTest.java    From kafka-connect-mongodb with Apache License 2.0 4 votes vote down vote up
@Override
    public void setUp() {
        offsets = new HashMap<>();
        totalWrittenDocuments = 0;
        try {
            super.setUp();
            mongodStarter = MongodStarter.getDefaultInstance();
            mongodConfig = new MongodConfigBuilder()
                    .version(Version.Main.V3_2)
                    .replication(new Storage(REPLICATION_PATH, "rs0", 1024))
                    .net(new Net(12345, Network.localhostIsIPv6()))
                    .build();
            mongodExecutable = mongodStarter.prepare(mongodConfig);
            mongod = mongodExecutable.start();
            mongoClient = new MongoClient(new ServerAddress("localhost", 12345));
            MongoDatabase adminDatabase = mongoClient.getDatabase("admin");

            BasicDBObject replicaSetSetting = new BasicDBObject();
            replicaSetSetting.put("_id", "rs0");
            BasicDBList members = new BasicDBList();
            DBObject host = new BasicDBObject();
            host.put("_id", 0);
            host.put("host", "127.0.0.1:12345");
            members.add(host);
            replicaSetSetting.put("members", members);
            adminDatabase.runCommand(new BasicDBObject("isMaster", 1));
            adminDatabase.runCommand(new BasicDBObject("replSetInitiate", replicaSetSetting));
            MongoDatabase db = mongoClient.getDatabase("mydb");
            db.createCollection("test1");
            db.createCollection("test2");
            db.createCollection("test3");
        } catch (Exception e) {
//                Assert.assertTrue(false);
        }

        task = new MongodbSourceTask();

        offsetStorageReader = PowerMock.createMock(OffsetStorageReader.class);
        context = PowerMock.createMock(SourceTaskContext.class);
        task.initialize(context);

        sourceProperties = new HashMap<>();
        sourceProperties.put("host", "localhost");
        sourceProperties.put("port", Integer.toString(12345));
        sourceProperties.put("batch.size", Integer.toString(100));
        sourceProperties.put("schema.name", "schema");
        sourceProperties.put("topic.prefix", "prefix");
        sourceProperties.put("databases", "mydb.test1,mydb.test2,mydb.test3");

    }
 
Example 20
Source File: MongodbSourceUriTaskTest.java    From kafka-connect-mongodb with Apache License 2.0 4 votes vote down vote up
@Override
    public void setUp() {
        offsets = new HashMap<>();
        totalWrittenDocuments = 0;
        try {
            super.setUp();
            mongodStarter = MongodStarter.getDefaultInstance();
            mongodConfig = new MongodConfigBuilder()
                    .version(Version.Main.V3_2)
                    .replication(new Storage(REPLICATION_PATH, "rs0", 1024))
                    .net(new Net(12345, Network.localhostIsIPv6()))
                    .build();
            mongodExecutable = mongodStarter.prepare(mongodConfig);
            mongod = mongodExecutable.start();
            mongoClient = new MongoClient(new ServerAddress("localhost", 12345));
            MongoDatabase adminDatabase = mongoClient.getDatabase("admin");

            BasicDBObject replicaSetSetting = new BasicDBObject();
            replicaSetSetting.put("_id", "rs0");
            BasicDBList members = new BasicDBList();
            DBObject host = new BasicDBObject();
            host.put("_id", 0);
            host.put("host", "127.0.0.1:12345");
            members.add(host);
            replicaSetSetting.put("members", members);
            adminDatabase.runCommand(new BasicDBObject("isMaster", 1));
            adminDatabase.runCommand(new BasicDBObject("replSetInitiate", replicaSetSetting));
            MongoDatabase db = mongoClient.getDatabase("mydb");
            db.createCollection("test1");
            db.createCollection("test2");
            db.createCollection("test3");
        } catch (Exception e) {
//                Assert.assertTrue(false);
        }

        task = new MongodbSourceTask();

        offsetStorageReader = PowerMock.createMock(OffsetStorageReader.class);
        context = PowerMock.createMock(SourceTaskContext.class);
        task.initialize(context);

        sourceProperties = new HashMap<>();
        sourceProperties.put("uri", "mongodb://localhost:12345");
        sourceProperties.put("batch.size", Integer.toString(100));
        sourceProperties.put("schema.name", "schema");
        sourceProperties.put("topic.prefix", "prefix");
        sourceProperties.put("databases", "mydb.test1,mydb.test2,mydb.test3");

    }