Search in sources :

Example 46 with MongoClientURI

use of com.mongodb.MongoClientURI in project mongo-java-driver by mongodb.

the class Decimal128QuickTour method main.

/**
     * Run this main method to see the output of this quick example.
     *
     * @param args takes an optional single argument for the connection string
     */
public static void main(final String[] args) {
    MongoClient mongoClient;
    if (args.length == 0) {
        // connect to the local database server
        mongoClient = new MongoClient();
    } else {
        mongoClient = new MongoClient(new MongoClientURI(args[0]));
    }
    // get handle to "mydb" database
    MongoDatabase database = mongoClient.getDatabase("mydb");
    // get a handle to the "test" collection
    MongoCollection<Document> collection = database.getCollection("test");
    // drop all the data in it
    collection.drop();
    // make a document and insert it
    Document doc = new Document("name", "MongoDB").append("amount1", Decimal128.parse(".10")).append("amount2", new Decimal128(42L)).append("amount3", new Decimal128(new BigDecimal(".200")));
    collection.insertOne(doc);
    Document first = collection.find().filter(Filters.eq("amount1", new Decimal128(new BigDecimal(".10")))).first();
    Decimal128 amount3 = (Decimal128) first.get("amount3");
    BigDecimal amount2AsBigDecimal = amount3.bigDecimalValue();
    System.out.println(amount3.toString());
    System.out.println(amount2AsBigDecimal.toString());
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientURI(com.mongodb.MongoClientURI) Decimal128(org.bson.types.Decimal128) Document(org.bson.Document) BigDecimal(java.math.BigDecimal) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 47 with MongoClientURI

use of com.mongodb.MongoClientURI in project mongo-hadoop by mongodb.

the class GridFSSplit method readFields.

@Override
public void readFields(final DataInput in) throws IOException {
    inputURI = new MongoClientURI(in.readUTF());
    byte[] oidBytes = new byte[12];
    in.readFully(oidBytes);
    fileId = new ObjectId(oidBytes);
    chunkId = in.readInt();
    fileLength = in.readLong();
    chunkSize = in.readInt();
}
Also used : ObjectId(org.bson.types.ObjectId) MongoClientURI(com.mongodb.MongoClientURI)

Example 48 with MongoClientURI

use of com.mongodb.MongoClientURI in project mongo-hadoop by mongodb.

the class MongoInputSplit method readFields.

@Override
public void readFields(final DataInput in) throws IOException {
    BSONCallback cb = new BasicBSONCallback();
    BSONObject spec;
    byte[] l = new byte[4];
    in.readFully(l);
    int dataLen = org.bson.io.Bits.readInt(l);
    byte[] data = new byte[dataLen + 4];
    System.arraycopy(l, 0, data, 0, 4);
    in.readFully(data, 4, dataLen - 4);
    _bsonDecoder.decode(data, cb);
    spec = (BSONObject) cb.get();
    setInputURI(new MongoClientURI((String) spec.get("inputURI")));
    if (spec.get("authURI") != null) {
        setAuthURI(new MongoClientURI((String) spec.get("authURI")));
    } else {
        setAuthURI((MongoClientURI) null);
    }
    setKeyField((String) spec.get("keyField"));
    BSONObject temp = (BSONObject) spec.get("fields");
    setFields(temp != null ? new BasicDBObject(temp.toMap()) : null);
    temp = (BSONObject) spec.get("query");
    setQuery(temp != null ? new BasicDBObject(temp.toMap()) : null);
    temp = (BSONObject) spec.get("sort");
    setSort(temp != null ? new BasicDBObject(temp.toMap()) : null);
    temp = (BSONObject) spec.get("min");
    setMin(temp != null ? new BasicDBObject(temp.toMap()) : null);
    temp = (BSONObject) spec.get("max");
    setMax(temp != null ? new BasicDBObject(temp.toMap()) : null);
    setLimit((Integer) spec.get("limit"));
    setSkip((Integer) spec.get("skip"));
    setNoTimeout((Boolean) spec.get("notimeout"));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicBSONCallback(org.bson.BasicBSONCallback) MongoClientURI(com.mongodb.MongoClientURI) BSONObject(org.bson.BSONObject) BSONCallback(org.bson.BSONCallback) BasicBSONCallback(org.bson.BasicBSONCallback)

Example 49 with MongoClientURI

use of com.mongodb.MongoClientURI in project mongo-hadoop by mongodb.

the class MongoCollectionSplitter method getConfigDB.

protected DB getConfigDB() {
    Mongo mongo;
    MongoClientURI inputURI = MongoConfigUtil.getInputURI(getConfiguration());
    MongoClientURI authURI = MongoConfigUtil.getAuthURI(getConfiguration());
    final DBCollection inputCollection;
    if (authURI != null && authURI.getUsername() != null && authURI.getPassword() != null) {
        inputCollection = MongoConfigUtil.getCollectionWithAuth(inputURI, authURI);
    } else {
        inputCollection = MongoConfigUtil.getCollection(inputURI);
    }
    DB db = inputCollection.getDB();
    mongo = db.getMongo();
    if (authURI != null) {
        if (authURI.getUsername() != null && authURI.getPassword() != null) {
            authDB = mongo.getDB(authURI.getDatabase());
        }
    }
    return mongo.getDB("config");
}
Also used : DBCollection(com.mongodb.DBCollection) Mongo(com.mongodb.Mongo) MongoClientURI(com.mongodb.MongoClientURI) DB(com.mongodb.DB)

Example 50 with MongoClientURI

use of com.mongodb.MongoClientURI in project mongo-hadoop by mongodb.

the class ShardChunkMongoSplitter method calculateSplitsFromChunks.

/**
     * Get a list of InputSplits based on a list of MongoDB shard chunks, the shard key, and a
     * mapping of shard names to host names. This is used internally by {@link #calculateSplits()}.
     *
     * @param chunks Chunk documents from the config.chunks collection.
     * @param shardsMap A map of shard name -> an array of hostnames.
     * @return A list of InputSplits.
     */
List<InputSplit> calculateSplitsFromChunks(final List<DBObject> chunks, final Map<String, List<String>> shardsMap) throws SplitFailedException {
    boolean targetShards = MongoConfigUtil.canReadSplitsFromShards(getConfiguration());
    List<String> mongosHostNames = MongoConfigUtil.getInputMongosHosts(getConfiguration());
    MongoClientURI inputURI = MongoConfigUtil.getInputURI(getConfiguration());
    if (targetShards && mongosHostNames.size() > 0) {
        throw new SplitFailedException("Setting both mongo.input.split.read_from_shards and mongo.input.mongos_hosts" + " does not make sense. ");
    }
    Map<String, String> mongosMap = null;
    if (mongosHostNames.size() > 0) {
        // Build a map of host -> mongos host string (incl. port)
        mongosMap = new HashMap<String, String>();
        for (String mongosHostName : mongosHostNames) {
            String[] hostAndPort = mongosHostName.split(":");
            mongosMap.put(hostAndPort[0], mongosHostName);
        }
    }
    List<InputSplit> splits = new ArrayList<InputSplit>(chunks.size());
    for (DBObject chunk : chunks) {
        BasicDBObject chunkLowerBound = (BasicDBObject) chunk.get("min");
        BasicDBObject chunkUpperBound = (BasicDBObject) chunk.get("max");
        MongoInputSplit chunkSplit = createSplitFromBounds(chunkLowerBound, chunkUpperBound);
        chunkSplit.setInputURI(inputURI);
        String shard = (String) chunk.get("shard");
        if (targetShards) {
            //The job is configured to target shards, so replace the
            //mongos hostname with the host of the shard's servers
            List<String> shardHosts = shardsMap.get(shard);
            if (shardHosts == null) {
                throw new SplitFailedException("Couldn't find shard ID: " + shard + " in config.shards.");
            }
            MongoClientURI newURI = rewriteURI(inputURI, shardHosts);
            chunkSplit.setInputURI(newURI);
        } else if (mongosMap != null) {
            // Try to use a mongos collocated with one of the shard hosts for the input
            // split. If the user has their Hadoop/MongoDB clusters configured correctly,
            // this will allow for reading without having to transfer data over a network.
            // Note that MongoInputSplit.getLocations() just returns the hostnames from its
            // input URI.
            List<String> chunkHosts = shardsMap.get(shard);
            String mongosHost = null;
            for (String chunkHost : chunkHosts) {
                String[] hostAndPort = chunkHost.split(":");
                mongosHost = mongosMap.get(hostAndPort[0]);
                if (mongosHost != null) {
                    break;
                }
            }
            if (null == mongosHost) {
                // Fall back just to using the given input URI.
                chunkSplit.setInputURI(inputURI);
            } else {
                LOG.info("Will read split " + chunkSplit + " from mongos " + mongosHost);
                chunkSplit.setInputURI(rewriteURI(inputURI, mongosHost));
            }
        }
        // Add this split to the list for the current shard.
        chunkSplit.setKeyField(MongoConfigUtil.getInputKey(getConfiguration()));
        splits.add(chunkSplit);
    }
    if (MongoConfigUtil.isFilterEmptySplitsEnabled(getConfiguration())) {
        return filterEmptySplits(splits);
    }
    return splits;
}
Also used : MongoInputSplit(com.mongodb.hadoop.input.MongoInputSplit) MongoClientURI(com.mongodb.MongoClientURI) ArrayList(java.util.ArrayList) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ArrayList(java.util.ArrayList) List(java.util.List) InputSplit(org.apache.hadoop.mapreduce.InputSplit) MongoInputSplit(com.mongodb.hadoop.input.MongoInputSplit)

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