List of usage examples for com.mongodb MongoClient MongoClient
public MongoClient(final MongoClientURI uri, final MongoDriverInformation mongoDriverInformation)
From source file:com.garyclayburg.persistence.config.LocalMongoClientConfig.java
License:Open Source License
@Override @Bean/*from w w w. j ava 2s . c o m*/ public Mongo mongo() throws Exception { log.info("configuring local mongo bean: " + mongoHost + ":" + mongoPort); MongoClient mongoClient = null; try { log.debug("mongoHost: " + mongoHost); log.debug("mongoPort: " + mongoPort); log.debug("mongoUser: " + mongoUser); log.debug("mongoDatabase: " + mongoDatabase); if (mongoUser != null) { log.debug("using user/password authentication to mongodb"); MongoCredential credential = MongoCredential.createCredential(mongoUser, getDatabaseName(), mongoPassword.toCharArray()); List<MongoCredential> credList = new ArrayList<MongoCredential>(); credList.add(credential); mongoClient = new MongoClient(new ServerAddress(mongoHost, mongoPort), credList); } else { log.debug("attempting no authentication to mongodb"); mongoClient = new MongoClient(mongoHost, mongoPort); } } catch (UnknownHostException e) { log.warn("kaboom", e); } return mongoClient; }
From source file:com.gdn.x.ui.function.Permute.java
static void permute(java.util.List<Integer> arr, int k) { for (int i = k; i < arr.size(); i++) { java.util.Collections.swap(arr, i, k); permute(arr, k + 1);//from w w w. j ava 2 s . c o m java.util.Collections.swap(arr, k, i); } if (k == arr.size() - 1) { MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("tool_data"); DBCollection coll = db.getCollection("combination"); BasicDBObject doc = new BasicDBObject(); doc.put("weight", java.util.Arrays.toString(arr.toArray())); coll.insert(doc); System.out.println(java.util.Arrays.toString(arr.toArray())); } }
From source file:com.github.camellabs.iot.cloudlet.document.driver.DriverDocumentCloudlet.java
License:Apache License
@Bean @ConditionalOnProperty(value = "camel.labs.iot.cloudlet.document.driver.mongodb.springbootconfig", matchIfMissing = true, havingValue = "false") Mongo mongo() throws UnknownHostException { String mongodbKubernetesHost = System.getenv("MONGODB_SERVICE_HOST"); String mongodbKubernetesPort = System.getenv("MONGODB_SERVICE_PORT"); if (mongodbKubernetesHost != null && mongodbKubernetesPort != null) { LOG.info("Kubernetes MongoDB service detected - {}:{}. Connecting...", mongodbKubernetesHost, mongodbKubernetesPort);//from w ww . j av a 2 s . c om return new MongoClient(mongodbKubernetesHost, parseInt(mongodbKubernetesPort)); } else { LOG.info("Can't find MongoDB Kubernetes service."); LOG.debug("Environment variables: {}", getenv()); } try { LOG.info("Attempting to connect to the MongoDB server at mongodb:27017."); Mongo mongo = new MongoClient("mongodb"); mongo.getDatabaseNames(); return mongo; } catch (MongoTimeoutException e) { LOG.info("Can't connect to the MongoDB server at mongodb:27017. Falling back to the localhost:27017."); return new MongoClient(); } }
From source file:com.github.danzx.zekke.mongo.config.MongoClientConfig.java
License:Apache License
@Bean public MongoClient mongoClient(MongoDbSettings mongoSettings) { return mongoSettings.getCredential().isPresent() ? new MongoClient(mongoSettings.getAddress(), singletonList(mongoSettings.getCredential().get())) : new MongoClient(mongoSettings.getAddress()); }
From source file:com.github.danzx.zekke.test.mongo.EmbeddedMongo.java
License:Apache License
@PostConstruct public void start() throws UnknownHostException, IOException { mongodExe = MONGO_STARTER.prepare(new MongodConfigBuilder().version(Version.Main.V3_4) .net(new Net(bindIp, port, Network.localhostIsIPv6())).build()); mongod = mongodExe.start();//from ww w .jav a 2s. c o m mongo = new MongoClient(bindIp, port); }
From source file:com.github.frapontillo.pulse.crowd.data.repository.DataLayer.java
License:Apache License
/** * Reads from a {@code database.properties} file and sets up the DB client and connection, * mapping all of the entity classes.//from ww w .jav a 2 s . co m * <p/> * You can optionally specify a target DB name that will override the one in the properties * file. * <p/> * The configuration file must have the following: * <ul> * <li>{@code database.host}, the server host of the DB instance</li> * <li>{@code database.port}, the server port of the DB instance</li> * <li>{@code database.name}, the name of the DB collection</li> * <li>{@code database.username}, the username with access to the collection</li> * <li>{@code database.password}, the password of the user with access to the collection</li> * </ul> * * @param db The target database to connect to. */ public DataLayer(String db) { // the input target overrides the DB name in the `database.properties` file DBConfig config = new DBConfig(getClass(), db); MongoClient client = new MongoClient(config.getServerAddress(), config.getCredentials()); // map all Morphia classes Morphia morphia = new Morphia(); morphia.mapPackageFromClass(Message.class); // create and/or get the datastore datastore = morphia.createDatastore(client, config.getDBName()); }
From source file:com.github.frapontillo.pulse.crowd.data.repository.Repository.java
License:Apache License
/** * Create a new Repository using the default configuration in `database.properties` and * overriding the db name//w w w . j a va2s . c o m * with the one in input. * * @param db The database name to use for this Repository instance. */ @SuppressWarnings({ "unchecked", "deprecation" }) public Repository(String db) { DBConfig config = new DBConfig(getClass(), db); MongoClient client = new MongoClient(config.getServerAddress(), config.getCredentials()); // map all Morphia classes morphia = new Morphia(); morphia.mapPackageFromClass(Message.class); ClusterSettings clusterSettings = ClusterSettings.builder() .hosts(Collections.singletonList(config.getServerAddress())).build(); MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings) .credentialList(config.getCredentials()).build(); com.mongodb.reactivestreams.client.MongoClient rxClient = MongoClients.create(settings); // create and/or get the datastore datastore = morphia.createDatastore(client, config.getDBName()); // init the DAO initDAO(datastore); ensureIndexes(); // create the reactive database rxDatastore = rxClient.getDatabase(config.getDBName()); }
From source file:com.github.joelittlejohn.embedmongo.MongoScriptsMojo.java
License:Apache License
DB connectToMongoAndGetDatabase() throws MojoExecutionException { if (databaseName == null || databaseName.trim().length() == 0) { throw new MojoExecutionException("Database name is missing"); }//www . j av a 2s. c o m MongoClient mongoClient; try { mongoClient = new MongoClient("localhost", getPort()); } catch (UnknownHostException e) { throw new MojoExecutionException("Unable to connect to mongo instance", e); } getLog().info("Connected to MongoDB"); return mongoClient.getDB(databaseName); }
From source file:com.github.maasdi.mongo.wrapper.NoAuthMongoClientWrapper.java
License:Apache License
protected MongoClient getClient(MongoDbMeta meta, VariableSpace vars, LogChannelInterface log, List<ServerAddress> repSet, boolean useAllReplicaSetMembers, MongoClientOptions opts) throws KettleException { try {/*from www . ja v a2s . c om*/ // Mongo's java driver will discover all replica set or shard // members (Mongos) automatically when MongoClient is constructed // using a list of ServerAddresses. The javadocs state that MongoClient // should be constructed using a SingleServer address instance (rather // than a list) when connecting to a stand-alone host - this is why // we differentiate here between a list containing one ServerAddress // and a single ServerAddress instance via the useAllReplicaSetMembers // flag. return (repSet.size() > 1 || (useAllReplicaSetMembers && repSet.size() >= 1) ? new MongoClient(repSet, opts) : (repSet.size() == 1 ? new MongoClient(repSet.get(0), opts) : new MongoClient(new ServerAddress("localhost"), opts))); //$NON-NLS-1$ } catch (UnknownHostException u) { throw new KettleException(u); } }
From source file:com.github.sakserv.storm.bolt.MongoBolt.java
License:Apache License
@Override public void prepare(@SuppressWarnings("rawtypes") Map stormConf, TopologyContext context, OutputCollector collector) {/*from w w w .ja v a 2 s . c om*/ this.collector = collector; try { this.mongoDB = new MongoClient(mongoHost, mongoPort).getDB(mongoDbName); } catch (Exception e) { throw new RuntimeException(e); } }