Example usage for com.mongodb MongoClient getDB

List of usage examples for com.mongodb MongoClient getDB

Introduction

In this page you can find the example usage for com.mongodb MongoClient getDB.

Prototype

@Deprecated 
public DB getDB(final String dbName) 

Source Link

Document

Gets a database object.

Usage

From source file:com.sqm.dashboard.dao.impl.UserDaoImpl.java

License:Apache License

public boolean addUser(String firstname) throws UnknownHostException {

    // String passwordHash = makePasswordHash(password,
    // Integer.toString(random.nextInt()));

    BasicDBObject user = new BasicDBObject();
    System.out.println("In USerDAO: " + firstname);
    user.append("firstname", firstname);

    try {/*w w  w  . ja  va2 s . co  m*/
        final DBCollection usersCollection;

        final MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost"));
        final DB blogDatabase = mongoClient.getDB("blog");
        usersCollection = blogDatabase.getCollection("user");

        usersCollection.insert(user);
        return true;
    } catch (MongoException.DuplicateKey e) {
        System.out.println("Username already in use: " + firstname);
        return false;
    }
}

From source file:com.strategicgains.docussandra.controller.perf.remote.mongo.MongoLoader.java

License:Apache License

public static void loadMongoData(MongoClientURI uri, final int NUM_WORKERS, Database database,
        final int numDocs, final PerfTestParent clazz) {
    logger.info("------------Loading Data into: " + database.name() + " with MONGO!------------");
    try {//from  w w w  .ja v a2 s  .c  o m
        try {
            MongoClient mongoClient = new MongoClient(uri);
            mongoClient.setWriteConcern(WriteConcern.MAJORITY);
            DB db = mongoClient.getDB(database.name());
            final DBCollection coll = db.getCollection(database.name());
            ArrayList<Thread> workers = new ArrayList<>(NUM_WORKERS + 1);
            int docsPerWorker = numDocs / NUM_WORKERS;
            try {
                List<Document> docs = clazz.getDocumentsFromFS();
                ArrayList<List<Document>> documentQueues = new ArrayList<>(NUM_WORKERS + 1);
                int numDocsAssigned = 0;
                while ((numDocsAssigned + 1) < numDocs) {
                    int start = numDocsAssigned;
                    int end = numDocsAssigned + docsPerWorker;
                    if (end > numDocs) {
                        end = numDocs - 1;
                    }
                    documentQueues.add(new ArrayList(docs.subList(start, end)));
                    numDocsAssigned = end;
                }
                for (final List<Document> queue : documentQueues) {
                    workers.add(new Thread() {
                        @Override
                        public void run() {
                            for (Document d : queue) {
                                DBObject o = (DBObject) JSON.parse(d.object());
                                coll.save(o);
                            }
                            logger.info("Thread " + Thread.currentThread().getName() + " is done. It processed "
                                    + queue.size() + " documents.");
                        }
                    });
                }
            } catch (UnsupportedOperationException e)//we can't read everything in at once
            {
                //all we need to do in this block is find a way to set "workers"
                for (int i = 0; i < NUM_WORKERS; i++) {
                    workers.add(new Thread() {
                        private final int chunk = (int) (Math.random() * 100) + 150;//pick a random chunk so we are not going back to the FS all at the same time and potentially causing a bottle neck

                        @Override
                        public void run() {
                            ThreadLocal<Integer> counter = new ThreadLocal<>();
                            counter.set(new Integer(0));
                            try {
                                List<Document> docs = clazz.getDocumentsFromFS(chunk);//grab a handful of documents
                                while (docs.size() > 0) {
                                    for (Document d : docs)//process the documents we grabbed
                                    {
                                        DBObject o = (DBObject) JSON.parse(d.object());
                                        coll.save(o);
                                        counter.set(counter.get() + 1);
                                    }
                                    docs = clazz.getDocumentsFromFS(chunk);//grab another handful of documents
                                }
                                logger.info("Thread " + Thread.currentThread().getName()
                                        + " is done. It processed " + counter.get() + " documents.");
                            } catch (IOException | ParseException e) {
                                logger.error("Couldn't read from document", e);
                            }
                        }
                    });
                }
            }

            long start = new Date().getTime();
            //start your threads!
            for (Thread t : workers) {
                t.start();
            }
            logger.info("All threads started, waiting for completion.");
            boolean allDone = false;
            boolean first = true;
            while (!allDone || first) {
                first = false;
                boolean done = true;
                for (Thread t : workers) {
                    if (t.isAlive()) {
                        done = false;
                        logger.info("Thread " + t.getName() + " is still running.");
                        break;
                    }
                }
                if (done) {
                    allDone = true;
                } else {
                    logger.info("We still have workers running...");
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                    }
                }
            }
            long end = new Date().getTime();
            long miliseconds = end - start;
            double seconds = (double) miliseconds / 1000d;
            output.info("Done loading data using: " + NUM_WORKERS + ". Took: " + seconds + " seconds");
            double tpms = (double) numDocs / (double) miliseconds;
            double tps = tpms * 1000;
            double transactionTime = (double) miliseconds / (double) numDocs;
            output.info(database.name() + " Mongo Average Transactions Per Second: " + tps);
            output.info(
                    database.name() + " Mongo Average Transactions Time (in miliseconds): " + transactionTime);

        } catch (UnknownHostException e) {
            logger.error("Couldn't connect to Mongo Server", e);
        }
    } catch (IOException | ParseException e) {
        logger.error("Couldn't read data.", e);
    }
}

From source file:com.stratio.connector.mongodb.core.engine.metadata.ShardUtils.java

License:Apache License

/**
 * Shard a collection.//from   www  .ja va 2s.  c  o m
 *
 * @param mongoClient
 *            the mongo client
 * @param tableMetadata
 *            the table metadata
 * @throws ExecutionException
 *             if an error exist when sharding the collection
 */
public static void shardCollection(MongoClient mongoClient, TableMetadata tableMetadata)
        throws ExecutionException {

    final String catalogName = tableMetadata.getName().getCatalogName().getName();
    enableSharding(mongoClient, catalogName);

    DBObject shardKey = new BasicDBObject();
    Map<String, Selector> options = SelectorOptionsUtils.processOptions(tableMetadata.getOptions());

    ShardKeyType shardKeyType = getShardKeyType(options);
    String[] shardKeyFields = getShardKeyFields(options, shardKeyType);

    switch (shardKeyType) {
    case ASC:
        for (String field : shardKeyFields) {
            shardKey.put(field, 1);
        }
        break;
    case HASHED:
        shardKey.put(shardKeyFields[0], "hashed");
        break;
    }

    // shard the collection with the key
    final DBObject cmd = new BasicDBObject("shardCollection",
            catalogName + "." + tableMetadata.getName().getName());
    cmd.put("key", shardKey);

    CommandResult result = mongoClient.getDB("admin").command(cmd);
    if (!result.ok()) {
        LOGGER.error("Error executing" + cmd + " :" + result.getErrorMessage());
        throw new ExecutionException(result.getErrorMessage());
    }

}

From source file:com.stratio.connector.mongodb.core.engine.metadata.ShardUtils.java

License:Apache License

/**
 * Enable sharding in the database.//  w w w  .  ja  va 2s .  c  om
 *
 * @param mongoClient
 *            the mongo client
 * @param databaseName
 *            the database name
 * @throws ExecutionException
 *             if an error exist when enabling sharding
 */
private static void enableSharding(MongoClient mongoClient, String databaseName) throws ExecutionException {

    DB db;

    db = mongoClient.getDB("admin");
    DBObject enableShardingCommand = new BasicDBObject("enableSharding", databaseName);
    CommandResult result = db.command(enableShardingCommand);
    if (!result.ok() && !result.getErrorMessage().equals("already enabled")) {
        LOGGER.error("Error executing" + enableShardingCommand + " :" + result.getErrorMessage());
        throw new ExecutionException(result.getErrorMessage());
    }
}

From source file:com.stratio.connector.mongodb.core.engine.MongoStorageEngine.java

License:Apache License

/**
 * Inserts a document in MongoDB.//from w  w w.ja v  a2  s . co  m
 *
 * @param targetTable
 *            the table metadata
 * @param row
 *            the row. it will be the MongoDB document
 * @param isNotExists
 *            Insert only if primary key doesn't exist yet.
 * @param connection
 *            the connection
 * @throws MongoInsertException
 *             if an error exist when inserting data
 * @throws MongoValidationException
 *             if the specified operation is not supported
 */
@Override
protected void insert(TableMetadata targetTable, Row row, boolean isNotExists,
        Connection<MongoClient> connection) throws MongoInsertException, MongoValidationException {

    MongoClient mongoClient = connection.getNativeConnection();
    DBCollection collection = mongoClient.getDB(targetTable.getName().getCatalogName().getName())
            .getCollection(targetTable.getName().getName());

    MongoInsertHandler insertHandler = new MongoInsertHandler(collection);

    insertRow(targetTable, row, isNotExists, insertHandler);

}

From source file:com.stratio.connector.mongodb.core.engine.MongoStorageEngine.java

License:Apache License

/**
 * Inserts a collection of documents in MongoDB.
 *
 * @param targetTable// ww  w .ja va 2 s.c o m
 *            the table metadata.
 * @param rows
 *            the set of rows.
 * @param isNotExists
 *            Insert only if primary key doesn't exist yet.
 * @param connection
 *            the connection
 * @throws MongoInsertException
 *             if an error exist when inserting data
 * @throws MongoValidationException
 *             if the specified operation is not supported
 */
@Override
protected void insert(TableMetadata targetTable, Collection<Row> rows, boolean isNotExists,
        Connection<MongoClient> connection) throws MongoInsertException, MongoValidationException {

    MongoClient mongoClient = connection.getNativeConnection();
    DBCollection collection = mongoClient.getDB(targetTable.getName().getCatalogName().getName())
            .getCollection(targetTable.getName().getName());

    MongoInsertHandler insertHandler = new MongoInsertHandler(collection);
    insertHandler.startBatch();

    for (Row row : rows) {
        insertRow(targetTable, row, isNotExists, insertHandler);
    }
    insertHandler.executeBatch();

}

From source file:com.stratio.connector.mongodb.core.engine.query.AggregationLogicalWorkflowExecutor.java

License:Apache License

/**
 * Execute an aggregation query./*from ww  w  .  j  a  va  2s.  c o  m*/
 *
 * @param mongoClient
 *            the MongoDB client.
 * @return the Crossdata ResultSet.
 * @throws ExecutionException
 *             if the execution fails.
 */
@Override
public ResultSet executeQuery(MongoClient mongoClient) throws ExecutionException {

    DB db = mongoClient.getDB(logicalWorkflowData.getProject().getCatalogName());
    DBCollection collection = db.getCollection(logicalWorkflowData.getProject().getTableName().getName());
    ResultSet resultSet = new ResultSet();
    resultSet.setColumnMetadata(
            MetaResultUtils.createMetadata(logicalWorkflowData.getProject(), logicalWorkflowData.getSelect()));

    // AggregationOptions aggOptions = AggregationOptions.builder()
    // .allowDiskUse(true)
    // .batchSize(size)
    // pipeline,aggOptions => dbcursor
    try {
        int stage = 1;
        for (DBObject aggregationStage : query) {
            logger.debug("Aggregate framework stage (" + (stage++) + ") : " + aggregationStage);
        }
        AggregationOutput aggOutput = collection.aggregate(query);
        for (DBObject result : aggOutput.results()) {
            if (logger.isDebugEnabled()) {
                logger.debug("AggResult: " + result);
            }
            resultSet.add(MetaResultUtils.createRowWithAlias(result, logicalWorkflowData.getSelect()));
        }
    } catch (MongoException mongoException) {
        logger.error("Error executing an aggregation query:" + mongoException.getMessage());
        throw new MongoExecutionException(mongoException.getMessage(), mongoException);
    }

    return resultSet;
}

From source file:com.stratio.connector.mongodb.core.engine.query.BasicLogicalWorkflowExecutor.java

License:Apache License

/**
 * Execute an usual query./*  w w w .  j a  v a2  s .  c o m*/
 *
 * @param mongoClient
 *            the MongoDB client.
 * @return the Crossdata ResultSet.
 * @throws MongoValidationException .
 * @throws ExecutionException
 *             if the execution fails or the query specified in the logical workflow is not supported.
 */
public ResultSet executeQuery(MongoClient mongoClient) throws ExecutionException {

    DB db = mongoClient.getDB(logicalWorkflowData.getProject().getCatalogName());
    DBCollection collection = db.getCollection(logicalWorkflowData.getProject().getTableName().getName());
    ResultSet resultSet = new ResultSet();
    resultSet.setColumnMetadata(
            MetaResultUtils.createMetadata(logicalWorkflowData.getProject(), logicalWorkflowData.getSelect()));

    if (logger.isDebugEnabled()) {
        logger.debug("Executing MongoQuery: " + query.get(0) + ", with fields: " + buildProject(false));
    }

    DBCursor cursor = collection.find(query.get(0), buildProject(false));

    if (logicalWorkflowData.getOrderBy() != null) {
        cursor = cursor.sort(buildOrderBy(false));
    }

    if (logicalWorkflowData.getLimit() != null) {
        cursor = cursor.limit(logicalWorkflowData.getLimit().getLimit());
    }

    DBObject rowDBObject;
    try {
        while (cursor.hasNext()) {
            rowDBObject = cursor.next();
            if (logger.isDebugEnabled()) {
                logger.debug("BResult: " + rowDBObject);
            }
            resultSet.add(MetaResultUtils.createRowWithAlias(rowDBObject, logicalWorkflowData.getSelect()));
        }
    } catch (MongoException e) {
        logger.error("Error executing a basic query :" + query.get(0) + ", with fields: " + buildProject(false)
                + "\n Error:" + e.getMessage());
        throw new MongoExecutionException(e.getMessage(), e);
    } finally {
        cursor.close();
    }

    return resultSet;
}

From source file:com.stratio.deep.mongodb.extractor.MongoNativeExtractor.java

License:Apache License

@Override
public Partition[] getPartitions(S config) {
    MongoClient mongoClient = null;

    try {/*from ww  w. j a va 2s. c o  m*/

        mongoDeepJobConfig = initConfig(config, mongoDeepJobConfig);

        DBCollection collection;
        ServerAddress address = new ServerAddress(mongoDeepJobConfig.getHost());

        List<ServerAddress> addressList = new ArrayList<>();
        addressList.add(address);
        mongoClient = new MongoClient(addressList);

        mongoClient.setReadPreference(ReadPreference.nearest());
        DB db = mongoClient.getDB(mongoDeepJobConfig.getDatabase());
        collection = db.getCollection(mongoDeepJobConfig.getCollection());
        return isShardedCollection(collection) ? calculateShardChunks(collection) : calculateSplits(collection);
    } catch (UnknownHostException e) {

        throw new DeepGenericException(e);
    } finally {
        if (mongoClient != null) {
            mongoClient.close();
        }

    }
}

From source file:com.stratio.deep.mongodb.extractor.MongoNativeExtractor.java

License:Apache License

/**
 * Gets split data collection shard enviroment.
 *
 * @param shards         the shards/*from w w w . java 2s  .  c  o  m*/
 * @param dbName         the db name
 * @param collectionName the collection name
 * @return the split data collection shard enviroment
 */
private Pair<BasicDBList, List<ServerAddress>> getSplitDataCollectionShardEnviroment(
        Map<String, String[]> shards, String dbName, String collectionName) {
    MongoClient mongoClient = null;
    try {
        Set<String> keys = shards.keySet();

        for (String key : keys) {

            List<ServerAddress> addressList = getServerAddressList(Arrays.asList(shards.get(key)));

            mongoClient = new MongoClient(addressList);

            BasicDBList dbList = getSplitData(mongoClient.getDB(dbName).getCollection(collectionName));

            if (dbList != null) {
                return Pair.create(dbList, addressList);
            }
        }
    } catch (UnknownHostException e) {
        throw new DeepGenericException(e);
    } finally {
        if (mongoClient != null) {
            mongoClient.close();
        }

    }

    return null;

}