Search in sources :

Example 11 with MinKey

use of org.bson.types.MinKey in project mongo-java-driver by mongodb.

the class JSONSerializersTest method testLegacySerialization.

@Test
public void testLegacySerialization() {
    ObjectSerializer serializer = JSONSerializers.getLegacy();
    BasicDBObject testObj = new BasicDBObject();
    // test  ARRAY
    BasicDBObject[] a = { new BasicDBObject("object1", "value1"), new BasicDBObject("object2", "value2") };
    testObj.put("array", a);
    StringBuilder buf = new StringBuilder();
    serializer.serialize(a, buf);
    assertEquals("[ { \"object1\" : \"value1\"} , { \"object2\" : \"value2\"}]", buf.toString());
    // test  BINARY
    byte[] b = { 1, 2, 3, 4 };
    testObj = new BasicDBObject("binary", new org.bson.types.Binary(b));
    buf = new StringBuilder();
    serializer.serialize(testObj, buf);
    assertEquals("{ \"binary\" : <Binary Data>}", buf.toString());
    // test  BOOLEAN
    testObj = new BasicDBObject("boolean", new Boolean(true));
    buf = new StringBuilder();
    serializer.serialize(testObj, buf);
    assertEquals(buf.toString(), "{ \"boolean\" : true}");
    // test  BSON_TIMESTAMP,
    testObj = new BasicDBObject("timestamp", new BSONTimestamp());
    buf = new StringBuilder();
    serializer.serialize(testObj, buf);
    assertEquals("{ \"timestamp\" : { \"$ts\" : 0 , \"$inc\" : 0}}", buf.toString());
    // test  BYTE_ARRAY
    testObj = new BasicDBObject("byte_array", b);
    buf = new StringBuilder();
    serializer.serialize(testObj, buf);
    assertEquals("{ \"byte_array\" : <Binary Data>}", buf.toString());
    // test  CODE
    testObj = new BasicDBObject("code", new Code("test code"));
    buf = new StringBuilder();
    serializer.serialize(testObj, buf);
    assertEquals("{ \"code\" : { \"$code\" : \"test code\"}}", buf.toString());
    // test  CODE_W_SCOPE
    testObj = new BasicDBObject("scope", "scope of code");
    CodeWScope codewscope = new CodeWScope("test code", testObj);
    buf = new StringBuilder();
    serializer.serialize(codewscope, buf);
    assertEquals("{ \"$code\" : \"test code\" , \"$scope\" : { \"scope\" : \"scope of code\"}}", buf.toString());
    // test  DATE
    Date d = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
    buf = new StringBuilder();
    serializer.serialize(d, buf);
    assertEquals("{ \"$date\" : \"" + format.format(d) + "\"}", buf.toString());
    // test  DB_OBJECT implicit in preceding tests
    // test  DB_REF_BASE
    DBRef dbref = new com.mongodb.DBRef("test.test", "4d83ab59a39562db9c1ae2af");
    buf = new StringBuilder();
    serializer.serialize(dbref, buf);
    assertEquals("{ \"$ref\" : \"test.test\" , \"$id\" : \"4d83ab59a39562db9c1ae2af\"}", buf.toString());
    DBRef dbrefWithDatabaseName = new com.mongodb.DBRef("mydb", "test.test", "4d83ab59a39562db9c1ae2af");
    buf = new StringBuilder();
    serializer.serialize(dbrefWithDatabaseName, buf);
    assertEquals("{ \"$ref\" : \"test.test\" , \"$id\" : \"4d83ab59a39562db9c1ae2af\" , \"$db\" : \"mydb\"}", buf.toString());
    // test  ITERABLE
    BasicBSONList testList = new BasicBSONList();
    testList.add(new BasicDBObject("key1", "val1"));
    testList.add(new BasicDBObject("key2", "val2"));
    buf = new StringBuilder();
    serializer.serialize(testList, buf);
    assertEquals("[ { \"key1\" : \"val1\"} , { \"key2\" : \"val2\"}]", buf.toString());
    // test  MAP
    Map<String, String> testMap = new TreeMap<String, String>();
    testMap.put("key1", "val1");
    testMap.put("key2", "val2");
    buf = new StringBuilder();
    serializer.serialize(testMap, buf);
    assertEquals("{ \"key1\" : \"val1\" , \"key2\" : \"val2\"}", buf.toString());
    // test  MAXKEY
    buf = new StringBuilder();
    serializer.serialize(new MaxKey(), buf);
    assertEquals("{ \"$maxKey\" : 1}", buf.toString());
    // test  MINKEY
    buf = new StringBuilder();
    serializer.serialize(new MinKey(), buf);
    assertEquals("{ \"$minKey\" : 1}", buf.toString());
    // test  NULL
    buf = new StringBuilder();
    serializer.serialize(null, buf);
    assertEquals(" null ", buf.toString());
    // test  NUMBER
    Random rand = new Random();
    long val = rand.nextLong();
    Long longVal = new Long(val);
    buf = new StringBuilder();
    serializer.serialize(longVal, buf);
    assertEquals(String.valueOf(val), buf.toString());
    // test  OBJECT_ID
    buf = new StringBuilder();
    serializer.serialize(new ObjectId("4d83ab3ea39562db9c1ae2ae"), buf);
    assertEquals("{ \"$oid\" : \"4d83ab3ea39562db9c1ae2ae\"}", buf.toString());
    // test  PATTERN
    buf = new StringBuilder();
    serializer.serialize(Pattern.compile("test"), buf);
    assertEquals("{ \"$regex\" : \"test\"}", buf.toString());
    // test  STRING
    buf = new StringBuilder();
    serializer.serialize("test string", buf);
    assertEquals("\"test string\"", buf.toString());
    // test  UUID;
    UUID uuid = UUID.randomUUID();
    buf = new StringBuilder();
    serializer.serialize(uuid, buf);
    assertEquals("{ \"$uuid\" : \"" + uuid.toString() + "\"}", buf.toString());
    // test Decimal128
    Decimal128 decimal128 = Decimal128.parse("3.140");
    buf = new StringBuilder();
    serializer.serialize(decimal128, buf);
    assertEquals("{ \"$numberDecimal\" : \"3.140\"}", buf.toString());
}
Also used : MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) BasicDBObject(com.mongodb.BasicDBObject) MinKey(org.bson.types.MinKey) Random(java.util.Random) SimpleTimeZone(java.util.SimpleTimeZone) UUID(java.util.UUID) ObjectId(org.bson.types.ObjectId) BasicBSONList(org.bson.types.BasicBSONList) GregorianCalendar(java.util.GregorianCalendar) DBRef(com.mongodb.DBRef) Decimal128(org.bson.types.Decimal128) TreeMap(java.util.TreeMap) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) Binary(org.bson.types.Binary) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 12 with MinKey

use of org.bson.types.MinKey in project drill by axbaretto.

the class MongoGroupScan method init.

@SuppressWarnings({ "rawtypes" })
private void init() throws IOException {
    List<String> h = storagePluginConfig.getHosts();
    List<ServerAddress> addresses = Lists.newArrayList();
    for (String host : h) {
        addresses.add(new ServerAddress(host));
    }
    MongoClient client = storagePlugin.getClient();
    chunksMapping = Maps.newHashMap();
    chunksInverseMapping = Maps.newLinkedHashMap();
    if (isShardedCluster(client)) {
        MongoDatabase db = client.getDatabase(CONFIG);
        MongoCollection<Document> chunksCollection = db.getCollection(CHUNKS);
        Document filter = new Document();
        filter.put(NS, this.scanSpec.getDbName() + "." + this.scanSpec.getCollectionName());
        Document projection = new Document();
        projection.put(SHARD, select);
        projection.put(MIN, select);
        projection.put(MAX, select);
        FindIterable<Document> chunkCursor = chunksCollection.find(filter).projection(projection);
        MongoCursor<Document> iterator = chunkCursor.iterator();
        MongoCollection<Document> shardsCollection = db.getCollection(SHARDS);
        projection = new Document();
        projection.put(HOST, select);
        boolean hasChunks = false;
        while (iterator.hasNext()) {
            Document chunkObj = iterator.next();
            String shardName = (String) chunkObj.get(SHARD);
            String chunkId = (String) chunkObj.get(ID);
            filter = new Document(ID, shardName);
            FindIterable<Document> hostCursor = shardsCollection.find(filter).projection(projection);
            MongoCursor<Document> hostIterator = hostCursor.iterator();
            while (hostIterator.hasNext()) {
                Document hostObj = hostIterator.next();
                String hostEntry = (String) hostObj.get(HOST);
                String[] tagAndHost = StringUtils.split(hostEntry, '/');
                String[] hosts = tagAndHost.length > 1 ? StringUtils.split(tagAndHost[1], ',') : StringUtils.split(tagAndHost[0], ',');
                List<String> chunkHosts = Arrays.asList(hosts);
                Set<ServerAddress> addressList = getPreferredHosts(storagePlugin.getClient(addresses), chunkHosts);
                if (addressList == null) {
                    addressList = Sets.newHashSet();
                    for (String host : chunkHosts) {
                        addressList.add(new ServerAddress(host));
                    }
                }
                chunksMapping.put(chunkId, addressList);
                ServerAddress address = addressList.iterator().next();
                List<ChunkInfo> chunkList = chunksInverseMapping.get(address.getHost());
                if (chunkList == null) {
                    chunkList = Lists.newArrayList();
                    chunksInverseMapping.put(address.getHost(), chunkList);
                }
                List<String> chunkHostsList = new ArrayList<String>();
                for (ServerAddress serverAddr : addressList) {
                    chunkHostsList.add(serverAddr.toString());
                }
                ChunkInfo chunkInfo = new ChunkInfo(chunkHostsList, chunkId);
                Document minMap = (Document) chunkObj.get(MIN);
                Map<String, Object> minFilters = Maps.newHashMap();
                Set keySet = minMap.keySet();
                for (Object keyObj : keySet) {
                    Object object = minMap.get(keyObj);
                    if (!(object instanceof MinKey)) {
                        minFilters.put(keyObj.toString(), object);
                    }
                }
                chunkInfo.setMinFilters(minFilters);
                Map<String, Object> maxFilters = Maps.newHashMap();
                Map maxMap = (Document) chunkObj.get(MAX);
                keySet = maxMap.keySet();
                for (Object keyObj : keySet) {
                    Object object = maxMap.get(keyObj);
                    if (!(object instanceof MaxKey)) {
                        maxFilters.put(keyObj.toString(), object);
                    }
                }
                chunkInfo.setMaxFilters(maxFilters);
                chunkList.add(chunkInfo);
            }
            hasChunks = true;
        }
        // unsharded collection and it will be stored in the primary shard of that database.
        if (!hasChunks) {
            handleUnshardedCollection(getPrimaryShardInfo(client));
        }
    } else {
        handleUnshardedCollection(storagePluginConfig.getHosts());
    }
}
Also used : ChunkInfo(org.apache.drill.exec.store.mongo.common.ChunkInfo) Set(java.util.Set) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList) MaxKey(org.bson.types.MaxKey) Document(org.bson.Document) MongoClient(com.mongodb.MongoClient) MinKey(org.bson.types.MinKey) Map(java.util.Map) BsonTypeClassMap(org.bson.codecs.BsonTypeClassMap) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 13 with MinKey

use of org.bson.types.MinKey in project mongo-hadoop by mongodb.

the class ShardChunkMongoSplitterTest method testSplitPreferredLocations.

@Test
public void testSplitPreferredLocations() throws SplitFailedException, IOException, InterruptedException {
    // Create list of chunks.
    List<DBObject> chunksList = new ArrayList<DBObject>() {

        {
            add(createChunk("i", new MinKey(), 500, "sh01"));
            add(createChunk("i", 500, new MaxKey(), "sh02"));
        }
    };
    // Create shards map.
    Map<String, List<String>> shardsMap = new HashMap<String, List<String>>() {

        {
            put("sh01", Arrays.asList("mongo.sh01.dc1:27017", "mongo.sh01.dc2:27017"));
            put("sh02", Arrays.asList("mongo.sh02.dc1:27027", "mongo.sh02.dc2:27027"));
        }
    };
    Configuration conf = new Configuration();
    MongoConfigUtil.setInputMongosHosts(conf, Arrays.asList("mongo.sh01.dc1:27018", "mongo.sh02.dc2:27018"));
    MongoConfigUtil.setInputURI(conf, new MongoClientURI("mongodb://mongo.dc1:27018,mongo.dc2:27018/hadoop.test"));
    splitter.setConfiguration(conf);
    List<InputSplit> splits = splitter.calculateSplitsFromChunks(chunksList, shardsMap);
    assertEquals("mongo.sh01.dc1:27018", splits.get(0).getLocations()[0]);
    assertEquals("mongo.sh02.dc2:27018", splits.get(1).getLocations()[0]);
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) HashMap(java.util.HashMap) MongoClientURI(com.mongodb.MongoClientURI) ArrayList(java.util.ArrayList) MaxKey(org.bson.types.MaxKey) DBObject(com.mongodb.DBObject) MinKey(org.bson.types.MinKey) ArrayList(java.util.ArrayList) List(java.util.List) InputSplit(org.apache.hadoop.mapreduce.InputSplit) Test(org.junit.Test) BaseHadoopTest(com.mongodb.hadoop.testutils.BaseHadoopTest)

Example 14 with MinKey

use of org.bson.types.MinKey in project mongo-hadoop by mongodb.

the class UDFTest method testMinMaxKey.

@Test
public void testMinMaxKey() throws IOException, ParseException {
    PigTest.runScript("/pig/genminmaxkeys.pig");
    for (Document doc : OUTPUT_COLLECTION.find()) {
        assertTrue(doc.get("newMin") instanceof MinKey);
        assertTrue(doc.get("newMax") instanceof MaxKey);
    }
}
Also used : MinKey(org.bson.types.MinKey) MaxKey(org.bson.types.MaxKey) Document(org.bson.Document) Test(org.junit.Test)

Example 15 with MinKey

use of org.bson.types.MinKey in project mongo-java-driver by mongodb.

the class JsonReader method visitNew.

private void visitNew() {
    JsonToken typeToken = popToken();
    if (typeToken.getType() != JsonTokenType.UNQUOTED_STRING) {
        throw new JsonParseException("JSON reader expected a type name but found '%s'.", typeToken.getValue());
    }
    String value = typeToken.getValue(String.class);
    if ("MinKey".equals(value)) {
        visitEmptyConstructor();
        setCurrentBsonType(BsonType.MIN_KEY);
        currentValue = new MinKey();
    } else if ("MaxKey".equals(value)) {
        visitEmptyConstructor();
        setCurrentBsonType(BsonType.MAX_KEY);
        currentValue = new MaxKey();
    } else if ("BinData".equals(value)) {
        currentValue = visitBinDataConstructor();
        setCurrentBsonType(BsonType.BINARY);
    } else if ("Date".equals(value)) {
        currentValue = visitDateTimeConstructor();
        setCurrentBsonType(BsonType.DATE_TIME);
    } else if ("HexData".equals(value)) {
        currentValue = visitHexDataConstructor();
        setCurrentBsonType(BsonType.BINARY);
    } else if ("ISODate".equals(value)) {
        currentValue = visitISODateTimeConstructor();
        setCurrentBsonType(BsonType.DATE_TIME);
    } else if ("NumberInt".equals(value)) {
        currentValue = visitNumberIntConstructor();
        setCurrentBsonType(BsonType.INT32);
    } else if ("NumberLong".equals(value)) {
        currentValue = visitNumberLongConstructor();
        setCurrentBsonType(BsonType.INT64);
    } else if ("NumberDecimal".equals(value)) {
        currentValue = visitNumberDecimalConstructor();
        setCurrentBsonType(BsonType.DECIMAL128);
    } else if ("ObjectId".equals(value)) {
        currentValue = visitObjectIdConstructor();
        setCurrentBsonType(BsonType.OBJECT_ID);
    } else if ("RegExp".equals(value)) {
        currentValue = visitRegularExpressionConstructor();
        setCurrentBsonType(BsonType.REGULAR_EXPRESSION);
    } else if ("DBPointer".equals(value)) {
        currentValue = visitDBPointerConstructor();
        setCurrentBsonType(BsonType.DB_POINTER);
    } else if ("UUID".equals(value)) {
        currentValue = visitUUIDConstructor();
        setCurrentBsonType(BsonType.BINARY);
    } else {
        throw new JsonParseException("JSON reader expected a type name but found '%s'.", value);
    }
}
Also used : MinKey(org.bson.types.MinKey) MaxKey(org.bson.types.MaxKey)

Aggregations

MinKey (org.bson.types.MinKey)15 MaxKey (org.bson.types.MaxKey)14 Binary (org.bson.types.Binary)8 Date (java.util.Date)7 Document (org.bson.Document)7 Code (org.bson.types.Code)7 ObjectId (org.bson.types.ObjectId)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)5 BSONTimestamp (org.bson.types.BSONTimestamp)5 CodeWScope (org.bson.types.CodeWScope)5 DBRef (com.mongodb.DBRef)4 List (java.util.List)4 Map (java.util.Map)3 BasicDBObject (com.mongodb.BasicDBObject)2 DBObject (com.mongodb.DBObject)2 ServerAddress (com.mongodb.ServerAddress)2 MongoDatabase (com.mongodb.client.MongoDatabase)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Arrays.asList (java.util.Arrays.asList)2