Search in sources :

Example 66 with MongoClientURI

use of com.mongodb.MongoClientURI in project jackrabbit-oak by apache.

the class NodeCollectionProvider method prepareClientForHostname.

private MongoClient prepareClientForHostname(String hostname) throws UnknownHostException {
    ServerAddress address;
    if (hostname.contains(":")) {
        String[] hostSplit = hostname.split(":");
        if (hostSplit.length != 2) {
            throw new IllegalArgumentException("Not a valid hostname: " + hostname);
        }
        address = new ServerAddress(hostSplit[0], Integer.parseInt(hostSplit[1]));
    } else {
        address = new ServerAddress(hostname);
    }
    MongoClientURI originalUri = new MongoClientURI(originalMongoUri);
    List<MongoCredential> credentialList = new ArrayList<MongoCredential>(1);
    if (originalUri.getCredentials() != null) {
        credentialList.add(originalUri.getCredentials());
    }
    return new MongoClient(address, credentialList, originalUri.getOptions());
}
Also used : MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) MongoClientURI(com.mongodb.MongoClientURI) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList)

Example 67 with MongoClientURI

use of com.mongodb.MongoClientURI in project jackrabbit-oak by apache.

the class NodeCollectionProvider method get.

@SuppressWarnings("deprecation")
public DBCollection get(String hostname) throws UnknownHostException {
    if (collections.containsKey(hostname)) {
        return collections.get(hostname);
    }
    MongoClient client;
    if (originalMongoUri == null) {
        MongoClientURI uri = new MongoClientURI("mongodb://" + hostname);
        client = new MongoClient(uri);
    } else {
        client = prepareClientForHostname(hostname);
    }
    DB db = client.getDB(dbName);
    db.getMongo().slaveOk();
    DBCollection collection = db.getCollection(Collection.NODES.toString());
    collections.put(hostname, collection);
    return collection;
}
Also used : MongoClient(com.mongodb.MongoClient) DBCollection(com.mongodb.DBCollection) MongoClientURI(com.mongodb.MongoClientURI) DB(com.mongodb.DB)

Example 68 with MongoClientURI

use of com.mongodb.MongoClientURI in project jackrabbit-oak by apache.

the class DocumentMongoFixture method getDb.

protected DB getDb(String suffix) throws UnknownHostException {
    String dbName = new MongoClientURI(uri).getDatabase();
    MongoConnection connection = new MongoConnection(uri);
    return connection.getDB(dbName + "-" + suffix);
}
Also used : MongoClientURI(com.mongodb.MongoClientURI) MongoConnection(org.apache.jackrabbit.oak.plugins.document.util.MongoConnection)

Example 69 with MongoClientURI

use of com.mongodb.MongoClientURI in project GeoGig by boundlessgeo.

the class MongoGraphDatabaseTest method createDatabase.

@Override
protected MongoGraphDatabase createDatabase(Platform platform) throws Exception {
    final IniMongoProperties properties = new IniMongoProperties();
    final String uri = properties.get("mongodb.uri", String.class).or("mongodb://localhost:27017/");
    final String database = properties.get("mongodb.database", String.class).or("geogig");
    MongoClient client = new MongoClient(new MongoClientURI(uri));
    DB db = client.getDB(database);
    db.dropDatabase();
    MongoConnectionManager manager = new MongoConnectionManager();
    ConfigDatabase config = new TestConfigDatabase(platform);
    MongoGraphDatabase mongoGraphDatabase = new MongoGraphDatabase(manager, config);
    return mongoGraphDatabase;
}
Also used : MongoClient(com.mongodb.MongoClient) MongoGraphDatabase(org.locationtech.geogig.storage.mongo.MongoGraphDatabase) ConfigDatabase(org.locationtech.geogig.storage.ConfigDatabase) MongoClientURI(com.mongodb.MongoClientURI) MongoConnectionManager(org.locationtech.geogig.storage.mongo.MongoConnectionManager) DB(com.mongodb.DB)

Example 70 with MongoClientURI

use of com.mongodb.MongoClientURI in project cas by apereo.

the class MongoDbConnectionFactory method mongoDbFactory.

private MongoDbFactory mongoDbFactory(final Mongo mongo, final BaseMongoDbProperties props) {
    final String dbName;
    final String authDbName;
    if (StringUtils.isNotBlank(props.getClientUri())) {
        final MongoClientURI uri = buildMongoClientURI(props.getClientUri(), buildMongoDbClientOptions(props));
        authDbName = uri.getCredentials().getSource();
        dbName = uri.getDatabase();
        LOGGER.debug("Using database [{}] from the connection client URI", dbName);
    } else {
        authDbName = props.getAuthenticationDatabaseName();
        dbName = props.getDatabaseName();
        LOGGER.debug("Using database [{}] from individual settings", dbName);
    }
    if (StringUtils.isBlank(dbName)) {
        LOGGER.error("Database name cannot be undefined. It must be specified as part of the client URI connection string if used, or " + "as an individual setting for the MongoDb connection");
    }
    return new SimpleMongoDbFactory(mongo, dbName, null, authDbName);
}
Also used : SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) MongoClientURI(com.mongodb.MongoClientURI)

Aggregations

MongoClientURI (com.mongodb.MongoClientURI)73 MongoClient (com.mongodb.MongoClient)29 DBCollection (com.mongodb.DBCollection)12 Test (org.junit.Test)11 BasicDBObject (com.mongodb.BasicDBObject)10 MongoClientURIBuilder (com.mongodb.hadoop.util.MongoClientURIBuilder)8 List (java.util.List)8 Configuration (org.apache.hadoop.conf.Configuration)8 DBObject (com.mongodb.DBObject)7 ArrayList (java.util.ArrayList)7 MongoDatabase (com.mongodb.client.MongoDatabase)6 InputSplit (org.apache.hadoop.mapreduce.InputSplit)6 Document (org.bson.Document)6 DB (com.mongodb.DB)5 MongoInputSplit (com.mongodb.hadoop.input.MongoInputSplit)5 IOException (java.io.IOException)5 MongoConnection (org.apache.jackrabbit.oak.plugins.document.util.MongoConnection)5 OptionParser (joptsimple.OptionParser)4 OptionSet (joptsimple.OptionSet)4 DocumentNodeStore (org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore)4