Example usage for com.mongodb MongoClient getDatabase

List of usage examples for com.mongodb MongoClient getDatabase

Introduction

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

Prototype

public MongoDatabase getDatabase(final String databaseName) 

Source Link

Usage

From source file:org.apache.rya.forwardchain.strategy.MongoPipelineStrategy.java

License:Apache License

/**
 * Initialize based on a configuration.//  w  ww  .  ja va 2s .  c om
 * @param mongoConf Should contain database information; cannot be null. If
 *      passed a stateful configuration, uses the existing mongo client,
 *      otherwise creates one.
 */
public MongoPipelineStrategy(MongoDBRdfConfiguration mongoConf) throws ForwardChainException {
    Preconditions.checkNotNull(mongoConf);
    final String mongoDBName = mongoConf.getMongoDBName();
    final String collectionName = mongoConf.getTriplesCollectionName();
    mongoConf.setFlush(false);
    final StatefulMongoDBRdfConfiguration statefulConf;
    try {
        if (mongoConf instanceof StatefulMongoDBRdfConfiguration) {
            statefulConf = (StatefulMongoDBRdfConfiguration) mongoConf;
            this.dao = new MongoDBRyaDAO();
            this.dao.setConf(statefulConf);
            this.dao.init();
        } else {
            this.dao = RyaSailFactory.getMongoDAO(mongoConf);
            statefulConf = this.dao.getConf();
        }
    } catch (RyaDAOException e) {
        throw new ForwardChainException("Can't connect to Rya.", e);
    }
    final MongoClient mongoClient = statefulConf.getMongoClient();
    final MongoDatabase mongoDB = mongoClient.getDatabase(mongoDBName);
    this.baseCollection = mongoDB.getCollection(collectionName);
    this.pipelineVisitor = new SparqlToPipelineTransformVisitor(this.baseCollection);
    this.engine = this.dao.getQueryEngine();
    this.backup = new SailExecutionStrategy(statefulConf);
    final MongoDbBatchWriterConfig writerConfig = MongoDbBatchWriterUtils
            .getMongoDbBatchWriterConfig(statefulConf);
    final CollectionType<Document> ct = new MongoCollectionType(baseCollection);
    this.batchWriter = new MongoDbBatchWriter<>(ct, writerConfig);
    try {
        this.batchWriter.start();
    } catch (final MongoDbBatchWriterException e) {
        throw new ForwardChainException("Error starting MongoDB batch writer", e);
    }
}

From source file:org.apache.rya.indexing.external.PcjIntegrationTestingUtil.java

License:Apache License

public static void deleteIndexDocuments(final MongoClient client, final String instance) {
    client.getDatabase(instance).getCollection(MongoPcjDocuments.PCJ_COLLECTION_NAME).drop();
}

From source file:org.apache.rya.indexing.external.PcjIntegrationTestingUtil.java

License:Apache License

public static void deleteCoreRyaTables(final MongoClient client, final String instance, final String collName) {
    final boolean bool = client.isLocked();
    client.getDatabase(instance).getCollection(collName).drop();
}

From source file:org.apache.rya.indexing.pcj.storage.mongo.MongoPcjDocuments.java

License:Apache License

/**
 * Creates a new {@link MongoPcjDocuments}.
 * @param client - The {@link MongoClient} to use to connect to mongo.
 * @param ryaInstanceName - The rya instance to connect to.
 *//*from w  w  w  .j av  a  2  s  .  c om*/
public MongoPcjDocuments(final MongoClient client, final String ryaInstanceName) {
    requireNonNull(client);
    requireNonNull(ryaInstanceName);
    pcjCollection = client.getDatabase(ryaInstanceName).getCollection(PCJ_COLLECTION_NAME);
}

From source file:org.apache.rya.mongodb.aggregation.SparqlToPipelineTransformVisitor.java

License:Apache License

/**
 * Instantiate a visitor from a {@link MongoDBRdfConfiguration}.
 * @param conf Contains database connection information.
 *//*  www . ja va2 s.  co m*/
public SparqlToPipelineTransformVisitor(StatefulMongoDBRdfConfiguration conf) {
    Preconditions.checkNotNull(conf);
    MongoClient mongo = conf.getMongoClient();
    MongoDatabase db = mongo.getDatabase(conf.getMongoDBName());
    this.inputCollection = db.getCollection(conf.getTriplesCollectionName());
}

From source file:org.apache.sling.nosql.mongodb.resourceprovider.impl.MongoDBNoSqlAdapter.java

License:Apache License

/**
 * @param mongoClient MongoDB client/*from  www  .ja va2s. c  o m*/
 * @param database MongoDB database
 * @param collection MongoDB collection
 */
public MongoDBNoSqlAdapter(MongoClient mongoClient, String database, String collection) {
    MongoDatabase db = mongoClient.getDatabase(database);
    this.collection = db.getCollection(collection);
}

From source file:org.auraframework.test.perf.PerfResultsUtil.java

License:Apache License

public static void writeToDb(PerfMetrics metrics, String test) {
    try {/*from   w  ww  .jav  a2  s.c o m*/
        MongoClient mongo = getMongoClient();
        if (mongo != null) {
            MongoDatabase db = mongo.getDatabase("performance");
            MongoCollection<Document> runs = db.getCollection("testRun");
            JSONObject json = metrics.toJSONObject();
            Document doc = Document.parse(json.toString());
            doc.append("testName", test);
            doc.append("run", RUN_TIME);
            runs.insertOne(doc);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.auraframework.test.perf.util.PerfResultsUtil.java

License:Apache License

public static void writeToDb(PerfExecutorTestCase test, String testName, String dbURI, PerfMetrics metrics,
        String traceLog) {/*from w  w w . j a v a  2s . com*/
    try {
        MongoClient mongo = getMongoClient(dbURI);
        if (mongo != null) {
            LOG.info("Writing perf results into mongo db at: " + mongo.getAddress());
            MongoDatabase db = mongo.getDatabase("performance");
            MongoCollection<Document> runs = db.getCollection("testRun");
            JSONObject json = metrics.toJSONObject();
            Document doc = Document.parse(json.toString());

            doc.append("timeline", traceLog);
            doc.append("testName", testName);
            doc.append("transaction", Document.parse((metrics.getMetricsServiceTransaction()).toString()));
            doc.append("commonMetrics", Document.parse((metrics.getCommonMetrics()).toString()));
            doc.append("customMetrics", Document.parse((metrics.getCustomMetrics()).toString()));
            doc.append("run", RUN_TIME);
            runs.insertOne(doc);
            exportToCsv(test, doc);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.axonframework.mongo.AbstractMongoTemplate.java

License:Apache License

/**
 * Initializes the MongoTemplate to connect using the given {@code mongo} instance and the database with given
 * {@code databaseName}. The given {@code userName} and {@code password}, when not {@code null},
 * are used to authenticate against the database.
 *
 * @param mongo        The Mongo instance configured to connect to the Mongo Server
 * @param databaseName The name of the database containing the data
 *///ww w . ja  v  a2s . com
protected AbstractMongoTemplate(MongoClient mongo, String databaseName) { // NOSONAR
    database = mongo.getDatabase(databaseName);
}

From source file:org.codinjutsu.tools.mongo.logic.MongoManager.java

License:Apache License

public void connect(ServerConfiguration configuration) {
    MongoClient mongo = null;
    try {/*from   w ww. ja va 2 s. c  o  m*/
        String userDatabase = configuration.getUserDatabase();
        mongo = createMongoClient(configuration);

        com.mongodb.client.MongoDatabase databaseForTesting;
        if (StringUtils.isNotEmpty(userDatabase)) {
            databaseForTesting = mongo.getDatabase(userDatabase);
        } else {
            databaseForTesting = mongo.getDatabase("test");
        }
    } catch (IOException ex) {
        throw new MongoConnectionException(ex);
    } catch (MongoException ex) {
        LOG.error("Error when accessing Mongo server", ex);
        throw new MongoConnectionException(ex.getMessage());
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}