List of usage examples for com.mongodb MongoClientURI MongoClientURI
public MongoClientURI(final String uri)
From source file:com.dydabo.blackbox.mongodb.db.MongoDBConnectionManager.java
License:Apache License
/** * @param connString/*from ww w. jav a2s. c om*/ * @param database * @param collection * @return */ public static MongoCollection<Document> getMongoDBCollection(String connString, String database, String collection) { if (mongoClient == null) { if (DyDaBoUtils.isBlankOrNull(connString)) { connString = "com.dydabo.com.dydabo.blackbox.blackbox.mongodb.mongodb://localhost:27017"; } synchronized (lockObj) { mongoClient = new MongoClient(new MongoClientURI(connString)); } } return mongoClient.getDatabase(database).getCollection(collection); }
From source file:com.edgytech.umongo.MainMenu.java
License:Apache License
public void connect() { try {//ww w. j av a2 s . c o m ConnectDialog dialog = (ConnectDialog) getBoundUnit(Item.connectDialog); ProgressDialog progress = (ProgressDialog) getBoundUnit(Item.connectProgressDialog); MongoClient mongo = null; List<String> dbs = new ArrayList<String>(); String uri = dialog.getStringFieldValue(ConnectDialog.Item.uri); if (!uri.trim().isEmpty()) { if (!uri.startsWith(MongoURI.MONGODB_PREFIX)) { uri = MongoURI.MONGODB_PREFIX + uri; } MongoClientURI muri = new MongoClientURI(uri); mongo = new MongoClient(muri); String db = muri.getDatabase(); if (db != null && !db.trim().isEmpty()) { dbs.add(db.trim()); } } else { String servers = dialog.getStringFieldValue(ConnectDialog.Item.servers); if (servers.trim().isEmpty()) { return; } String[] serverList = servers.split(","); ArrayList<ServerAddress> addrs = new ArrayList<ServerAddress>(); for (String server : serverList) { String[] tmp = server.split(":"); if (tmp.length > 1) { addrs.add(new ServerAddress(tmp[0], Integer.valueOf(tmp[1]).intValue())); } else { addrs.add(new ServerAddress(tmp[0])); } } if ("Direct".equals(dialog.getStringFieldValue(ConnectDialog.Item.connectionMode))) mongo = new MongoClient(addrs.get(0), dialog.getMongoClientOptions()); else mongo = new MongoClient(addrs, dialog.getMongoClientOptions()); String sdbs = dialog.getStringFieldValue(ConnectDialog.Item.databases); if (!sdbs.trim().isEmpty()) { for (String db : sdbs.split(",")) { dbs.add(db.trim()); } } } if (dbs.size() == 0) { dbs = null; } String user = dialog.getStringFieldValue(ConnectDialog.Item.user).trim(); String password = dialog.getStringFieldValue(ConnectDialog.Item.password); if (!user.isEmpty()) { // authenticate against all dbs if (dbs != null) { for (String db : dbs) { mongo.getDB(db).authenticate(user, password.toCharArray()); } } else { mongo.getDB("admin").authenticate(user, password.toCharArray()); } } final MongoClient fmongo = mongo; final List<String> fdbs = dbs; // doing in background can mean concurrent modification, but dialog is modal so unlikely progress.show(new ProgressDialogWorker(progress) { @Override protected void finished() { } @Override protected Object doInBackground() throws Exception { UMongo.instance.addMongoClient(fmongo, fdbs); return null; } }); } catch (Exception ex) { UMongo.instance.showError(id, ex); } }
From source file:com.everydots.kafka.connect.mongodb.end2end.MinimumViableIT.java
License:Apache License
@BeforeAll public static void setup() throws IOException { CONTAINER_ENV.starting(Description.EMPTY); MONGODB_CLIENT_URI = new MongoClientURI("mongodb://" + MONGODB + ":" + MONGODB_PORT + "/kafkaconnect"); MONGO_CLIENT = new MongoClient(MONGODB_CLIENT_URI); MONGO_DATABASE = MONGO_CLIENT.getDatabase(MONGODB_CLIENT_URI.getDatabase()); Properties props = new Properties(); props.put("bootstrap.servers", KAFKA_BROKER + ":" + KAFKA_BROKER_PORT); props.put("key.serializer", "io.confluent.kafka.serializers.KafkaAvroSerializer"); props.put("value.serializer", "io.confluent.kafka.serializers.KafkaAvroSerializer"); props.put("schema.registry.url", "http://" + SCHEMA_REGISTRY + ":" + SCHEMA_REGISTRY_PORT); PRODUCER = new KafkaProducer<>(props); String config = new String(Files.readAllBytes(Paths.get(SINK_CONNECTOR_CONFIG))); deferExecutionToWaitForDataPropagation(Duration.ofMinutes(3), "wait some time so that all container processes become available"); registerMongoDBSinkConnector(config); }
From source file:com.everydots.kafka.connect.mongodb.MongoDbSinkConnectorConfig.java
License:Apache License
public MongoClientURI buildClientURI() { return new MongoClientURI(getString(MONGODB_CONNECTION_URI_CONF)); }
From source file:com.ga.model.abstractclasses.ModelAbstract.java
@Override public void init(String dbName, String collectionName) { try {//from ww w.java 2s . c o m // ModelAbstract.conn = new MongoClient(new MongoClientURI("mongodb://116.193.163.66:27017/")); ModelAbstract.conn = new MongoClient(new MongoClientURI("mongodb://localhost:27017/")); if (ModelAbstract.conn != null) { ModelAbstract.db = ModelAbstract.conn.getDB(dbName); if (ModelAbstract.db != null) { ModelAbstract.collection = ModelAbstract.db.getCollection(collectionName); if (ModelAbstract.collection == null) { Exception exCollection = new Exception( "Collection \'" + collectionName + "\' does not exist in \'" + dbName + "\'."); throw exCollection; } } else { Exception exDatabase = new Exception("Database \'" + dbName + "\' not found."); throw exDatabase; } } else { ex = new MongoException("Failed to connect with database server."); throw ex; } } catch (UnknownHostException e) { Logger.getLogger(ModelAbstract.class.getName()).log(Level.SEVERE, null, e); } catch (Exception exCollection) { Logger.getLogger(ModelAbstract.class.getName()).log(Level.SEVERE, null, exCollection); } }
From source file:com.geeksanon.AppController.java
License:Open Source License
/** * Method to make a connection to the database, with the URI. * /* w ww . ja v a 2 s . c om*/ * @param URIString * URI passed other than the localhost * @throws IOException * when the page is not found. */ public AppController(String URIString) throws IOException { MongoClient mongoClient = new MongoClient(new MongoClientURI(URIString)); DB database = mongoClient.getDB("askme"); questionDAO = new QuestionDAO(database); userDAO = new UserDAO(database); sessionDAO = new SessionDAO(database); configuration = loadTemplateConfiguration(); Spark.setPort(5115); intialiseRoutes(); }
From source file:com.github.achatain.catalog.module.CatalogConfig.java
License:Open Source License
CatalogConfig() {
properties = loadProperties();
mongoClient = new MongoClient(new MongoClientURI(properties.getProperty("mongodb.url")));
}
From source file:com.globocom.grou.configurations.MongoConfiguration.java
License:Open Source License
private synchronized MongoClientURI getMongoClientUri() { if (mongoClientUri == null && !"".equals(MONGO_URI.getValue())) { mongoClientUri = new MongoClientURI(MONGO_URI.getValue()); }/*from w w w . ja v a 2s .co m*/ return mongoClientUri; }
From source file:com.google.api.ads.adwords.awreporting.model.persistence.mongodb.MongoEntityPersister.java
License:Open Source License
/** * Constructor that opens MongoDB client from URL, and retrives specified database. * * @param mongoConnectionUrl the Mongo connection url * @param mongoDataBaseName the Mongo database name *///from w w w .j a v a2 s. c om public MongoEntityPersister(String mongoConnectionUrl, String mongoDataBaseName) throws UnknownHostException, MongoException { mongoClient = new MongoClient(new MongoClientURI(mongoConnectionUrl)); db = mongoClient.getDB(mongoDataBaseName); }
From source file:com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.mongodb.MongoEntityPersister.java
License:Open Source License
protected MongoEntityPersister(String mongoConnectionUrl, String mongoDataBaseName) throws UnknownHostException, MongoException { mongoClient = new MongoClient(new MongoClientURI(mongoConnectionUrl)); db = mongoClient.getDB(mongoDataBaseName); }