Example usage for com.mongodb MongoClient MongoClient

List of usage examples for com.mongodb MongoClient MongoClient

Introduction

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

Prototype

public MongoClient(final MongoClientURI uri) 

Source Link

Document

Creates a Mongo described by a URI.

Usage

From source file:com.deliveronthego.CommonAPI.java

public MongoClient getConnection() {
    mongoclienturi = new MongoClientURI(
            "mongodb://deliveronthego:deliveronthego@ds037097.mongolab.com:37097/deliveronthego");
    mongoclient = new MongoClient(mongoclienturi);
    return mongoclient;
}

From source file:com.dianping.swallow.web.dao.SimMongoDbFactory.java

License:Apache License

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

From source file:com.dydabo.blackbox.mongodb.db.MongoDBConnectionManager.java

License:Apache License

/**
 * @param connString/*  w  w w  .  j a  v  a  2  s .com*/
 * @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  a v  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.edgytech.umongo.RouterPanel.java

License:Apache License

public void regenConfigDB(ButtonBase button) throws UnknownHostException {
    MongoClient cmongo = getRouterNode().getMongoClient();
    String servers = getStringFieldValue(Item.regenServers);
    final String db = getStringFieldValue(Item.regenDB);
    final String col = getStringFieldValue(Item.regenCollection);
    final String ns = db + "." + col;
    final DBObject shardKey = ((DocBuilderField) getBoundUnit(Item.regenShardKey)).getDBObject();
    final boolean unique = getBooleanFieldValue(Item.regenKeyUnique);
    final BasicDBObject result = new BasicDBObject();
    result.put("ns", ns);
    result.put("shardKey", shardKey);
    result.put("unique", unique);

    // create direct mongo for each replica set
    String[] serverList = servers.split("\n");
    List<ServerAddress> list = new ArrayList<ServerAddress>();
    String txt = "";
    String primaryShard = null;//from   ww  w  .  j a  v  a  2  s .co m
    final BasicDBObject shardList = new BasicDBObject();
    HashMap<MongoClient, String> mongoToShard = new HashMap<MongoClient, String>();
    sLoop: for (String server : serverList) {
        server = server.trim();
        String[] tokens = server.split("/");
        if (tokens.length != 2) {
            new InfoDialog(null, "Error", null, "Server format must be like 'hostname:port/shard', one by line")
                    .show();
            return;
        }
        server = tokens[0];
        String shard = tokens[1];
        if (primaryShard == null) {
            primaryShard = shard;
        }
        ServerAddress addr = new ServerAddress(server);

        // filter out if replset already exists
        for (MongoClient replset : mongoToShard.keySet()) {
            if (replset.getServerAddressList().contains(addr)) {
                continue sLoop;
            }
        }

        list.clear();
        list.add(addr);
        MongoClient mongo = new MongoClient(list);
        //            UMongo.instance.addMongoClient(mongo, null);
        // make request to force server detection
        mongo.getDatabaseNames();
        mongoToShard.put(mongo, shard);

        String desc = null;
        if (!mongo.getDatabaseNames().contains(db) || !mongo.getDB(db).getCollectionNames().contains(col)) {
            desc = "Collection not present!";
        } else {
            // try to see if shard key has index
            DBObject index = mongo.getDB(db).getCollection("system.indexes")
                    .findOne(new BasicDBObject("key", shardKey));
            if (index != null) {
                desc = "shard key found";
            } else {
                desc = "shard key NOT found!";
            }
        }
        txt += mongo.toString() + " shard=" + shard + " - " + desc + "\n";
        BasicDBObject shardObj = new BasicDBObject("servers", mongo.toString());
        shardObj.put("status", desc);
        if (shardList.containsField(shard)) {
            new InfoDialog(null, "Error", null, "Duplicate Shard name " + shard).show();
            return;
        }
        shardList.put(shard, shardObj);
    }
    result.put("shards", shardList);

    FormDialog dia = (FormDialog) getBoundUnit(Item.regenRSList);
    dia.setStringFieldValue(Item.regenRSListArea, txt);
    if (!dia.show()) {
        return;
    }

    DB config = cmongo.getDB("config");

    // add database record
    BasicDBObject doc = new BasicDBObject("_id", db);
    doc.put("partitioned", true);
    doc.put("primary", primaryShard);
    config.getCollection("databases").save(doc);

    // add collection record
    doc = new BasicDBObject("_id", ns);
    doc.put("lastmod", new Date());
    doc.put("dropped", false);
    doc.put("key", shardKey);
    doc.put("unique", unique);
    config.getCollection("collections").save(doc);

    final DBCollection chunks = config.getCollection("chunks");
    long count = chunks.count(new BasicDBObject("ns", ns));
    if (count > 0) {
        dia = (FormDialog) getBoundUnit(Item.regenDeleteChunks);
        if (dia.show()) {
            chunks.remove(new BasicDBObject("ns", ns));
        } else {
            return;
        }
    }

    // add temp collection to sort chunks with shard key
    final DBCollection tmpchunks = config.getCollection("_tmpchunks_" + col);
    tmpchunks.drop();
    // should be safe environment, and dup keys should be ignored
    tmpchunks.setWriteConcern(WriteConcern.NORMAL);
    // can use shardKey as unique _id
    //        tmpchunks.ensureIndex(shardKey, "shardKey", true);

    // create filter for shard fields
    final DBObject shardKeyFilter = new BasicDBObject();
    //        final DBObject shardKeyDescend = new BasicDBObject();
    boolean hasId = false;
    for (String key : shardKey.keySet()) {
        shardKeyFilter.put(key, 1);
        if (key.equals("_id")) {
            hasId = true;
        }
    }
    if (!hasId) {
        shardKeyFilter.put("_id", 0);
    }

    dia = (FormDialog) getBoundUnit(Item.regenConfirm);
    if (!dia.show()) {
        return;
    }

    // now fetch all records from each shard
    final AtomicInteger todo = new AtomicInteger(mongoToShard.size());
    for (Map.Entry<MongoClient, String> entry : mongoToShard.entrySet()) {
        final MongoClient mongo = entry.getKey();
        final String shard = entry.getValue();
        new DbJob() {

            @Override
            public Object doRun() throws Exception {
                BasicDBObject shardObj = (BasicDBObject) shardList.get(shard);
                long count = mongo.getDB(db).getCollection(col).count();
                shardObj.put("count", count);
                DBCursor cur = mongo.getDB(db).getCollection(col).find(new BasicDBObject(), shardKeyFilter);
                long i = 0;
                int inserted = 0;
                long start = System.currentTimeMillis();
                while (cur.hasNext() && !isCancelled()) {
                    BasicDBObject key = (BasicDBObject) cur.next();
                    setProgress((int) ((++i * 100.0f) / count));
                    try {
                        BasicDBObject entry = new BasicDBObject("_id", key);
                        entry.put("_shard", shard);
                        tmpchunks.insert(entry);
                        ++inserted;
                    } catch (Exception e) {
                        getLogger().log(Level.WARNING, e.getMessage(), e);
                    }
                }

                if (isCancelled()) {
                    shardObj.put("cancelled", true);
                }
                shardObj.put("inserted", inserted);
                shardObj.put("scanTime", System.currentTimeMillis() - start);
                todo.decrementAndGet();
                return null;
            }

            @Override
            public String getNS() {
                return tmpchunks.getFullName();
            }

            @Override
            public String getShortName() {
                return "Scanning " + shard;
            }

            @Override
            public boolean isDeterminate() {
                return true;
            }
        }.addJob();

    }

    new DbJob() {

        @Override
        public Object doRun() throws Exception {
            // wait for all shards to be done
            long start = System.currentTimeMillis();
            while (todo.get() > 0 && !isCancelled()) {
                Thread.sleep(2000);
            }

            if (isCancelled()) {
                result.put("cancelled", true);
                return result;
            }

            // find highest current timestamp
            DBCursor cur = chunks.find().sort(new BasicDBObject("lastmod", -1)).batchSize(-1);
            BasicDBObject chunk = (BasicDBObject) (cur.hasNext() ? cur.next() : null);
            BSONTimestamp ts = (BSONTimestamp) (chunk != null ? chunk.get("lastmod") : null);

            // now infer chunk ranges
            long count = tmpchunks.count();
            result.put("uniqueKeys", count);
            int numChunks = 0;
            cur = tmpchunks.find().sort(new BasicDBObject("_id", 1));
            BasicDBObject prev = (BasicDBObject) cur.next();
            BasicDBObject next = null;
            // snap prev to minkey
            BasicDBObject theid = (BasicDBObject) prev.get("_id");
            for (String key : shardKey.keySet()) {
                theid.put(key, new MinKey());
            }
            String currentShard = prev.getString("_shard");

            int i = 1;
            while (cur.hasNext()) {
                next = (BasicDBObject) cur.next();
                setProgress((int) ((++i * 100.0f) / count));
                String newShard = next.getString("_shard");
                if (newShard.equals(currentShard))
                    continue;

                // add chunk
                ts = getNextTimestamp(ts);
                chunk = getChunk(ns, shardKey, prev, next, ts);
                chunks.insert(chunk);
                prev = next;
                currentShard = prev.getString("_shard");
                ++numChunks;
            }

            // build max
            next = new BasicDBObject();
            for (String key : shardKey.keySet()) {
                next.put(key, new MaxKey());
            }
            next = new BasicDBObject("_id", next);
            ts = getNextTimestamp(ts);
            chunk = getChunk(ns, shardKey, prev, next, ts);
            chunks.insert(chunk);
            ++numChunks;
            result.put("numChunks", numChunks);
            result.put("totalTime", System.currentTimeMillis() - start);
            return result;
        }

        @Override
        public String getNS() {
            return chunks.getFullName();
        }

        @Override
        public String getShortName() {
            return "Creating Chunks";
        }

        @Override
        public boolean isDeterminate() {
            return true;
        }
    }.addJob();
}

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.MongoDbSinkTask.java

License:Apache License

@Override
public void start(Map<String, String> props) {
    logger.info("starting MongoDB sink task");

    sinkConfig = new MongoDbSinkConnectorConfig(props);

    MongoClientURI uri = sinkConfig.buildClientURI();
    mongoClient = new MongoClient(uri);
    database = mongoClient.getDatabase(uri.getDatabase());

    remainingRetries = sinkConfig.getInt(MongoDbSinkConnectorConfig.MONGODB_MAX_NUM_RETRIES_CONF);
    deferRetryMs = sinkConfig.getInt(MongoDbSinkConnectorConfig.MONGODB_RETRIES_DEFER_TIMEOUT_CONF);

    processorChain = sinkConfig.buildPostProcessorChain();

    if (sinkConfig.isUsingCdcHandler()) {
        cdcHandler = sinkConfig.getCdcHandler();
    }/*from  w  w w. j  a  v  a 2s. c o m*/
}

From source file:com.exorath.exodata.impl.IDataAPI.java

License:Apache License

public IDataAPI(String host) {
    this.client = new MongoClient(host);
}

From source file:com.exorath.simpleapi.impl.database.DBManager.java

License:Apache License

public void withCollectionConsumer(String dbName, String collectionName, MongoCollectionConsumer consumer) {
    try (MongoClient client = new MongoClient(host)) {
        consumer.consume(client.getDatabase(dbName).getCollection(collectionName));
    }//www. j av  a2 s.c o  m
}

From source file:com.exorath.simpleapi.impl.database.DBManager.java

License:Apache License

public void withDatabaseConsumer(String dbName, MongoDBConsumer consumer) {
    try (MongoClient client = new MongoClient(host)) {
        consumer.consume(client.getDatabase(dbName));
    }/*from   ww w .  jav  a2 s  . co m*/
}