Example usage for com.mongodb MongoClientURI getDatabase

List of usage examples for com.mongodb MongoClientURI getDatabase

Introduction

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

Prototype

@Nullable
public String getDatabase() 

Source Link

Document

Gets the database name

Usage

From source file:org.sevenup.LokiMongoDbFactory.java

License:Apache License

/**
 * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClientURI}.
 * /*from   w  w  w  .j  av  a2  s. c  om*/
 * @param uri must not be {@literal null}.
 * @throws UnknownHostException
 * @since 1.7
 */
public LokiMongoDbFactory(MongoClientURI uri) throws UnknownHostException {
    this(new MongoClient(uri), uri.getDatabase(), true);
}

From source file:org.springframework.data.mongodb.core.SimpleMongoDbFactory.java

License:Apache License

/**
 * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClientURI}.
 * //from  w  ww. jav  a  2s  . com
 * @param uri must not be {@literal null}.
 * @throws UnknownHostException
 * @since 1.7
 */
public SimpleMongoDbFactory(MongoClientURI uri) throws UnknownHostException {
    this(new MongoClient(uri), uri.getDatabase(), true);
}

From source file:org.springframework.data.mongodb.microbenchmark.MongoResultsWriter.java

License:Apache License

@Override
public void write(Collection<RunResult> results) {

    Date now = new Date();
    StandardEnvironment env = new StandardEnvironment();

    String projectVersion = env.getProperty("project.version", "unknown");
    String gitBranch = env.getProperty("git.branch", "unknown");
    String gitDirty = env.getProperty("git.dirty", "no");
    String gitCommitId = env.getProperty("git.commit.id", "unknown");

    MongoClientURI uri = new MongoClientURI(this.uri);
    MongoClient client = new MongoClient(uri);

    String dbName = StringUtils.hasText(uri.getDatabase()) ? uri.getDatabase()
            : "spring-data-mongodb-benchmarks";
    MongoDatabase db = client.getDatabase(dbName);

    for (BasicDBObject dbo : (List<BasicDBObject>) JSON.parse(ResultsWriter.jsonifyResults(results))) {

        String collectionName = extractClass(dbo.get("benchmark").toString());

        Document sink = new Document();
        sink.append("_version", projectVersion);
        sink.append("_branch", gitBranch);
        sink.append("_commit", gitCommitId);
        sink.append("_dirty", gitDirty);
        sink.append("_method", extractBenchmarkName(dbo.get("benchmark").toString()));
        sink.append("_date", now);
        sink.append("_snapshot", projectVersion.toLowerCase().contains("snapshot"));

        sink.putAll(dbo);/*from  ww w . j  ava 2 s .c  om*/

        db.getCollection(collectionName).insertOne(fixDocumentKeys(sink));
    }

    client.close();
}

From source file:org.teiid.resource.adapter.mongodb.MongoDBConnectionImpl.java

License:Open Source License

public MongoDBConnectionImpl(String database, MongoClientURI uri) throws UnknownHostException {
    this.database = database;
    if (uri.getDatabase() != null) {
        this.database = database;
    }//  www .j a  v a 2 s  . co m
    this.client = new MongoClient(uri);
}

From source file:rapture.mongodb.MongoDBFactory.java

License:Open Source License

private Mongo getMongoFromLocalConfig(String instanceName) {
    String mongoHost = MultiValueConfigLoader.getConfig("MONGODB-" + instanceName);
    log.info("Host is " + mongoHost);
    if (StringUtils.isBlank(mongoHost)) {
        throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST,
                mongoMsgCatalog.getMessage("NoHost"));
    }/*from  w  ww . j  a va2 s  .  c  om*/
    MongoClientURI uri = new MongoClientURI(mongoHost);
    log.info("Username is " + uri.getUsername());
    log.info("Host is " + uri.getHosts().toString());
    log.info("DBName is " + uri.getDatabase());
    log.info("Collection is " + uri.getCollection());
    try {
        MongoClient mongo = new MongoClient(uri);
        mongoDBs.put(instanceName, mongo.getDB(uri.getDatabase()));
        mongoDatabases.put(instanceName, mongo.getDatabase(uri.getDatabase()));
        mongoInstances.put(instanceName, mongo);
        return mongo;
    } catch (MongoException e) {
        throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST, new ExceptionToString(e));
    }
}

From source file:rapture.repo.integration.LocalTestSetup.java

License:Open Source License

public static void createUser() throws IOException, InterruptedException {
    String mongoHost = MultiValueConfigLoader.getConfig("MONGODB-integrationTest");
    log.info("Host is " + mongoHost);
    if (mongoHost != null) {
        MongoClientURI uri = new MongoClientURI(mongoHost);
        List<String> hosts = uri.getHosts();
        for (String host : hosts) {
            String[] cmdarray = createSetupCommand(host, uri.getDatabase(), uri.getUsername(),
                    new String(uri.getPassword()));
            Process process = Runtime.getRuntime().exec(cmdarray);
            int retVal = process.waitFor();
            log.info(String.format("retVal=%s", retVal));
            log.info("output is " + IOUtils.toString(process.getInputStream()));
            if (retVal != 0) {
                log.info("error is " + IOUtils.toString(process.getErrorStream()));
            }/*www  .  j  av  a2s  .com*/
        }
    } else {
        log.error("mongo host is not defined!");
    }
}

From source file:thermostatapplication.TemperaturePersisterTimerTask.java

public synchronized void persistDataOnMongolab() {
    //disable console logging
    //Logger mongoLogger = Logger.getLogger("org.mongodb.driver"); 
    //mongoLogger.setLevel(Level.SEVERE);

    iStoredTemperatures = iTemperatureStore.getTemperatures();
    if (iStoredTemperatures.isEmpty()) {
        logger.info("Nothing to persist. Exiting");
        return;//w  ww. ja  v a2 s.  co m
    }
    logger.info("Prepairing to persist [{}] Temps in the cloud", iStoredTemperatures.size());
    MongoCollection<Document> mongoCollection = null;
    MongoClient client = null;
    List<Document> documents = new ArrayList<>();

    for (TemperatureMeasure tTemp : iStoredTemperatures) { //Exception in thread "Timer-2" java.util.ConcurrentModificationException
        Document doc = new Document();
        doc.put("Location", tTemp.getLocation()); //Location
        doc.put("Group", tTemp.getGroup()); //Group
        doc.put("Date", Helper.getDateAsString(tTemp.getDate())); //Date
        doc.put("Day", Helper.getDayAsString(tTemp.getDate()));
        doc.put("Time", Helper.getTimeAsString(tTemp.getDate()));
        doc.put("Temp", Helper.getTempAsString(tTemp.getTemp())); //Temp
        documents.add(doc);
        iPersistedTemperatures.add(tTemp);
    }

    try {
        MongoClientURI uri = new MongoClientURI(ThermostatProperties.ML_URL);
        client = new MongoClient(uri);
        MongoDatabase database = (MongoDatabase) client.getDatabase(uri.getDatabase());
        mongoCollection = database.getCollection("dailytemps");
        mongoCollection.insertMany(documents);
        //eliminate stored Temps from the collection
        iTemperatureStore.removeAll(iPersistedTemperatures);
        client.close();
        logger.info("Temperatures persisted on mongolab: [{}]. Exiting.", iPersistedTemperatures.size());
        iPersistedTemperatures.clear();
    } catch (Throwable e) {
        logger.error("Failed to store Temps in the cloud. Stacktrace: [{}]. Exiting.", e);
        iPersistedTemperatures.clear();
        e.printStackTrace();
    } finally {
        if (client != null) {
            client.close();
        }
        iPersistedTemperatures.clear();
    }
}

From source file:xyz.lexteam.minecraft.staple.db.Database.java

License:MIT License

public Database(Configuration configuration) {
    MongoClientURI clientURI = new MongoClientURI(configuration.getDatabase().getUri());
    this.mongoClient = new MongoClient(clientURI);
    this.mongoDatabase = this.mongoClient.getDatabase(clientURI.getDatabase());
}