Example usage for com.mongodb MongoClientOptions builder

List of usage examples for com.mongodb MongoClientOptions builder

Introduction

In this page you can find the example usage for com.mongodb MongoClientOptions builder.

Prototype

public static Builder builder() 

Source Link

Document

Creates a builder instance.

Usage

From source file:MongoCredentialsExample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    String server = args[0];/*from w  w w . j  a  v  a2  s  . c o m*/
    String user = args[1];
    String password = args[2];
    String databaseName = args[3];

    System.out.println("server: " + server);
    System.out.println("user: " + user);
    System.out.println("database: " + databaseName);

    System.out.println();

    MongoClient mongoClient = new MongoClient(new ServerAddress(server),
            Arrays.asList(MongoCredential.createMongoCRCredential(user, "test", password.toCharArray())),
            new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
    DB testDB = mongoClient.getDB(databaseName);

    System.out.println("Count: " + testDB.getCollection("test").count());

    System.out.println("Insert result: " + testDB.getCollection("test").insert(new BasicDBObject()));

}

From source file:NegotiatedAuthenticationProtocolExample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException, InterruptedException {
    String server = args[0];/*from w  ww.j  a v a2 s  .c o  m*/
    String user = args[1];
    String pwd = args[2];
    String db = args[3];

    MongoCredential credentials = new MongoCredential(user, pwd.toCharArray(),
            MongoAuthenticationProtocol.NEGOTIATE, db);

    MongoClient mongoClient = new MongoClient(new ServerAddress(server), Arrays.asList(credentials),
            new MongoClientOptions.Builder().build());

    DB testDB = mongoClient.getDB(db);
    testDB.getCollection("test").insert(new BasicDBObject());
    System.out.println("Count: " + testDB.getCollection("test").count());
}

From source file:GSSAPICredentialsExample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException, InterruptedException {
    // Set this property to avoid the default behavior where the program prompts on the command line for username/password
    //        Security.setProperty("auth.login.defaultCallbackHandler", "DefaultSecurityCallbackHandler");

    String server = args[0];//  w  w  w.  jav  a 2s.  c  o  m
    String user = args[1];
    String databaseName = args[2];

    System.out.println("javax.security.auth.useSubjectCredsOnly: "
            + System.getProperty("javax.security.auth.useSubjectCredsOnly"));
    System.out.println("java.security.krb5.realm: " + System.getProperty("java.security.krb5.realm"));
    System.out.println("java.security.krb5.kdc: " + System.getProperty("java.security.krb5.kdc"));
    System.out.println(
            "auth.login.defaultCallbackHandler: " + Security.getProperty("auth.login.defaultCallbackHandler"));
    System.out.println("login.configuration.provider: " + Security.getProperty("login.configuration.provider"));
    System.out.println(
            "java.security.auth.login.config: " + Security.getProperty("java.security.auth.login.config"));
    System.out.println("login.config.url.1: " + Security.getProperty("login.config.url.1"));
    System.out.println("login.config.url.2: " + Security.getProperty("login.config.url.2"));
    System.out.println("login.config.url.3: " + Security.getProperty("login.config.url.3"));

    System.out.println("server: " + server);
    System.out.println("user: " + user);
    System.out.println("database: " + databaseName);

    System.out.println();

    MongoClient mongoClient = new MongoClient(new ServerAddress(server),
            Arrays.asList(MongoCredential.createGSSAPICredential(user)),
            new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
    DB testDB = mongoClient.getDB(databaseName);

    System.out.println("Insert result: " + testDB.getCollection("test").insert(new BasicDBObject()));
    System.out.println("Count: " + testDB.getCollection("test").count());
}

From source file:CramMd5CredentialsExample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException, InterruptedException {
    String server = args[0];//from  w w  w .ja  v a 2 s.co  m
    String user = args[1];
    String pwd = args[2];
    String db = args[3];
    MongoClient mongo = new MongoClient(
            new MongoClientAuthority(new ServerAddress(server),
                    new MongoClientCredentials(user, pwd.toCharArray(),
                            MongoClientCredentials.CRAM_MD5_MECHANISM, db)),
            new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
    DB testDB = mongo.getDB(db);
    System.out.println("Find     one: " + testDB.getCollection("test").findOne());
    System.out.println("Count: " + testDB.getCollection("test").count());
    WriteResult writeResult = testDB.getCollection("test").insert(new BasicDBObject());
    System.out.println("Write result: " + writeResult);

    System.out.println();

    System.out.println("Count: " + testDB.getCollection("test").count());

}

From source file:PlainCredentialsExample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    String server = args[0];//from   ww w .jav a2  s  .  c  o m
    String user = args[1];
    String password = args[2];
    String source = args[3];

    System.out.println("server: " + server);
    System.out.println("user: " + user);
    System.out.println("source: " + source);

    System.out.println();

    MongoClient mongoClient = new MongoClient(new ServerAddress(server),
            Arrays.asList(MongoCredential.createPlainCredential(user, source, password.toCharArray())),
            new MongoClientOptions.Builder().build());
    DB testDB = mongoClient.getDB("test");

    System.out.println("Count: " + testDB.getCollection("test").count());

    System.out.println("Insert result: " + testDB.getCollection("test").insert(new BasicDBObject()));
}

From source file:biopolis.headless.BiopolisPersistencyLayer.java

public BiopolisPersistencyLayer(BiopolisProperties props) throws BiopolisGeneralException, SQLException {
    org.neo4j.jdbc.Driver driver = new org.neo4j.jdbc.Driver();
    this.connNeo4j = driver.connect("jdbc:neo4j://" + props.neo4j_endpoint, new Properties());
    try {//from www.j a  v  a  2  s  .c  o  m
        System.out.println(props.mongo_host);
        MongoClientOptions mco = new MongoClientOptions.Builder().connectionsPerHost(10)
                .threadsAllowedToBlockForConnectionMultiplier(10).build();
        mongo = new MongoClient(new ServerAddress("localhost"), mco); //addresses is a pre-populated List 
    } catch (MongoException e) {
        e.printStackTrace();
        throw new BiopolisGeneralException("Mongo exception problem");
    } catch (UnknownHostException e) {
        e.printStackTrace();
        throw new BiopolisGeneralException("Mongo connection problem");
    }
    DB dbo = mongo.getDB(BiopolisPersistencyLayer.dbName);
    if (dbo.collectionExists(BiopolisPersistencyLayer.collectionNamePlaces)) {
        this.dbcolPlaces = dbo.getCollection(BiopolisPersistencyLayer.collectionNamePlaces);

    } else {
        this.dbcolPlaces = dbo.createCollection(BiopolisPersistencyLayer.collectionNamePlaces, null);
        this.dbcolPlaces.createIndex(new BasicDBObject("loc", "2dsphere"));
    }

    if (dbo.collectionExists(collectionNameSearches)) {
        this.dbcolSearches = dbo.getCollection(collectionNameSearches);

    } else {
        this.dbcolSearches = dbo.createCollection(collectionNameSearches, null);
        this.dbcolSearches.createIndex(new BasicDBObject("biopolisttl", 1),
                new BasicDBObject("expireAfterSeconds", props.expire_search));
    }
    this.segSZ = props.segment_size;
}

From source file:br.inpe.lac.projetoalunoseorientadoresinpe2016.controller.Controller.java

public String getAllPersons() throws Exception {
    MongoClientOptions options = MongoClientOptions.builder().connectionsPerHost(100).build();
    MongoClient mongoClient = new MongoClient(new ServerAddress(), options);
    MongoDatabase my_db = mongoClient.getDatabase("alunos_e_orientadores_inpe");
    MongoCollection<Document> pessoas = my_db.getCollection("pessoas");
    List<Document> list = pessoas.find().into(new ArrayList<Document>());
    return new Gson().toJson(list);
}

From source file:br.inpe.lac.projetoalunoseorientadoresinpe2016.mongo.Mongo.java

public Mongo() {
    options = MongoClientOptions.builder().connectionsPerHost(100).build();
    mongoClient = new MongoClient(new ServerAddress(), options);//MongoClient mongoClient = new MongoClient(new MongoClientURI("mongod://localhost:27017"));
    my_db = mongoClient.getDatabase("alunos_orientadores_inpe");
    alunos = my_db.getCollection("alunos");
    orientadores = my_db.getCollection("orientadores");
}

From source file:cloud.simple.RuleEngineApplication.java

@Bean
public MongoDatabase dataSource() {
    String servers = env.getProperty("spring.data.mongodb.custom.service");
    String databaseName = env.getProperty("spring.data.mongodb.database");

    List<ServerAddress> seeds = new ArrayList<ServerAddress>();
    String[] servers1 = servers.split(",");
    for (String server : servers1) {
        String[] server1 = server.split(":");
        seeds.add(new ServerAddress(server1[0], Integer.parseInt(server1[1])));
    }/*  w w  w.j a  v a2  s. c  o  m*/

    Builder builder = MongoClientOptions.builder();
    builder.socketKeepAlive(true);
    builder.readPreference(ReadPreference.secondaryPreferred());
    MongoClientOptions options = builder.build();

    @SuppressWarnings("resource")
    MongoClient mongoClient = new MongoClient(seeds, options);
    return mongoClient.getDatabase(databaseName);
}

From source file:cn.cnic.bigdatalab.flume.sink.mongodb.MongoSink.java

License:Apache License

/**
 * {@inheritDoc}//w  w  w .  j a  v  a 2 s . co m
 *
 * @param context
 */
@Override
public void configure(Context context) {
    try {
        if (!"INJECTED".equals(context.getString(CONF_URI))) {
            this.mongoClientURI = new MongoClientURI(context.getString(CONF_URI),
                    MongoClientOptions.builder().writeConcern(WriteConcern.SAFE));
            this.mongoClient = new MongoClient(mongoClientURI);
            if (mongoClientURI.getDatabase() != null) {
                this.mongoDefaultDb = mongoClient.getDB(mongoClientURI.getDatabase());
            }
            if (mongoClientURI.getCollection() != null) {
                this.mongoDefaultCollection = mongoDefaultDb.getCollection(mongoClientURI.getCollection());
            }
        }
        final String mappingFilename = context.getString(CONF_MAPPING_FILE);
        this.eventParser = (mappingFilename == null) ? new EventParser()
                : new EventParser(MappingDefinition.load(mappingFilename));

        this.isDynamicMode = context.getBoolean(CONF_DYNAMIC, DEFAULT_DYNAMIC);
        if (!isDynamicMode && mongoDefaultCollection == null) {
            throw new MongoSinkException(
                    "Default MongoDB collection must be specified unless dynamic mode is enabled");
        }
        this.dynamicDBField = context.getString(CONF_DYNAMIC_DB_FIELD, DEFAULT_DYNAMIC_DB_FIELD);
        this.dynamicCollectionField = context.getString(CONF_DYNAMIC_COLLECTION_FIELD,
                DEFAULT_DYNAMIC_COLLECTION_FIELD);

        this.sinkCounter = new SinkCounter(this.getName());
        this.batchSize = context.getInteger(CONF_BATCH_SIZE, DEFAULT_BATCH_SIZE);

        this.updateInsteadReplace = context.getBoolean(CONF_UPDATE_INSTEAD_REPLACE,
                DEFAULT_UPDATE_INSTEAD_REPLACE);

    } catch (IOException ex) {
        throw new MongoSinkException(ex);
    }
}