Search in sources :

Example 11 with MaxKey

use of org.bson.types.MaxKey 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 12 with MaxKey

use of org.bson.types.MaxKey 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)

Example 13 with MaxKey

use of org.bson.types.MaxKey in project spring-data-mongodb by spring-projects.

the class ParameterBindingJsonReader 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) || "GUID".equals(value) || "CSUUID".equals(value) || "CSGUID".equals(value) || "JUUID".equals(value) || "JGUID".equals(value) || "PYUUID".equals(value) || "PYGUID".equals(value)) {
        currentValue = visitUUIDConstructor(value);
        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) String(java.lang.String) JsonParseException(org.bson.json.JsonParseException)

Example 14 with MaxKey

use of org.bson.types.MaxKey in project spring-data-mongodb by spring-projects.

the class ParameterBindingJsonReader method readBsonType.

// CHECKSTYLE:OFF
@Override
public BsonType readBsonType() {
    if (isClosed()) {
        throw new IllegalStateException("This instance has been closed");
    }
    if (getState() == State.INITIAL || getState() == State.DONE || getState() == State.SCOPE_DOCUMENT) {
        // in JSON the top level value can be of any type so fall through
        setState(State.TYPE);
    }
    if (getState() != State.TYPE) {
        throwInvalidState("readBSONType", State.TYPE);
    }
    if (getContext().getContextType() == BsonContextType.DOCUMENT) {
        JsonToken nameToken = popToken();
        switch(nameToken.getType()) {
            case STRING:
            case UNQUOTED_STRING:
                // Spring Data Customization START
                setCurrentName(bindableValueFor(nameToken).getValue().toString());
                // Spring Data Customization END
                break;
            case END_OBJECT:
                setState(State.END_OF_DOCUMENT);
                return BsonType.END_OF_DOCUMENT;
            default:
                throw new JsonParseException("JSON reader was expecting a name but found '%s'.", nameToken.getValue());
        }
        JsonToken colonToken = popToken();
        if (colonToken.getType() != JsonTokenType.COLON) {
            throw new JsonParseException("JSON reader was expecting ':' but found '%s'.", colonToken.getValue());
        }
    }
    JsonToken token = popToken();
    if (getContext().getContextType() == BsonContextType.ARRAY && token.getType() == JsonTokenType.END_ARRAY) {
        setState(State.END_OF_ARRAY);
        return BsonType.END_OF_DOCUMENT;
    }
    // Spring Data Customization START
    boolean noValueFound = false;
    BindableValue bindableValue = null;
    switch(token.getType()) {
        case BEGIN_ARRAY:
            setCurrentBsonType(BsonType.ARRAY);
            break;
        case BEGIN_OBJECT:
            visitExtendedJSON();
            break;
        case DOUBLE:
            setCurrentBsonType(BsonType.DOUBLE);
            currentValue = token.getValue();
            break;
        case END_OF_FILE:
            setCurrentBsonType(BsonType.END_OF_DOCUMENT);
            break;
        case INT32:
            setCurrentBsonType(BsonType.INT32);
            currentValue = token.getValue();
            break;
        case INT64:
            setCurrentBsonType(BsonType.INT64);
            currentValue = token.getValue();
            break;
        case REGULAR_EXPRESSION:
            setCurrentBsonType(BsonType.REGULAR_EXPRESSION);
            currentValue = bindableValueFor(token).getValue();
            break;
        case STRING:
            setCurrentBsonType(BsonType.STRING);
            currentValue = bindableValueFor(token).getValue().toString();
            break;
        case UNQUOTED_STRING:
            String value = token.getValue(String.class);
            if ("false".equals(value) || "true".equals(value)) {
                setCurrentBsonType(BsonType.BOOLEAN);
                currentValue = Boolean.parseBoolean(value);
            } else if ("Infinity".equals(value)) {
                setCurrentBsonType(BsonType.DOUBLE);
                currentValue = Double.POSITIVE_INFINITY;
            } else if ("NaN".equals(value)) {
                setCurrentBsonType(BsonType.DOUBLE);
                currentValue = Double.NaN;
            } else if ("null".equals(value)) {
                setCurrentBsonType(BsonType.NULL);
            } else if ("undefined".equals(value)) {
                setCurrentBsonType(BsonType.UNDEFINED);
            } else 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)) {
                setCurrentBsonType(BsonType.BINARY);
                currentValue = visitBinDataConstructor();
            } else if ("Date".equals(value)) {
                currentValue = visitDateTimeConstructorWithOutNew();
                setCurrentBsonType(BsonType.STRING);
            } else if ("HexData".equals(value)) {
                setCurrentBsonType(BsonType.BINARY);
                currentValue = visitHexDataConstructor();
            } else if ("ISODate".equals(value)) {
                setCurrentBsonType(BsonType.DATE_TIME);
                currentValue = visitISODateTimeConstructor();
            } else if ("NumberInt".equals(value)) {
                setCurrentBsonType(BsonType.INT32);
                currentValue = visitNumberIntConstructor();
            } else if ("NumberLong".equals(value)) {
                setCurrentBsonType(BsonType.INT64);
                currentValue = visitNumberLongConstructor();
            } else if ("NumberDecimal".equals(value)) {
                setCurrentBsonType(BsonType.DECIMAL128);
                currentValue = visitNumberDecimalConstructor();
            } else if ("ObjectId".equals(value)) {
                setCurrentBsonType(BsonType.OBJECT_ID);
                currentValue = visitObjectIdConstructor();
            } else if ("Timestamp".equals(value)) {
                setCurrentBsonType(BsonType.TIMESTAMP);
                currentValue = visitTimestampConstructor();
            } else if ("RegExp".equals(value)) {
                setCurrentBsonType(BsonType.REGULAR_EXPRESSION);
                currentValue = visitRegularExpressionConstructor();
            } else if ("DBPointer".equals(value)) {
                setCurrentBsonType(BsonType.DB_POINTER);
                currentValue = visitDBPointerConstructor();
            } else if ("UUID".equals(value) || "GUID".equals(value) || "CSUUID".equals(value) || "CSGUID".equals(value) || "JUUID".equals(value) || "JGUID".equals(value) || "PYUUID".equals(value) || "PYGUID".equals(value)) {
                setCurrentBsonType(BsonType.BINARY);
                currentValue = visitUUIDConstructor(value);
            } else if ("new".equals(value)) {
                visitNew();
            } else {
                bindableValue = bindableValueFor(token);
                if (bindableValue != null) {
                    if (bindableValue.getIndex() != -1) {
                        setCurrentBsonType(bindableValue.getType());
                    } else {
                        setCurrentBsonType(BsonType.STRING);
                    }
                    currentValue = bindableValue.getValue();
                } else {
                    noValueFound = true;
                }
            }
            break;
        default:
            noValueFound = true;
            break;
    }
    if (noValueFound) {
        throw new JsonParseException("JSON reader was expecting a value but found '%s'.", token.getValue());
    }
    if (getContext().getContextType() == BsonContextType.ARRAY || getContext().getContextType() == BsonContextType.DOCUMENT) {
        JsonToken commaToken = popToken();
        if (commaToken.getType() != JsonTokenType.COMMA) {
            pushToken(commaToken);
        }
    }
    switch(getContext().getContextType()) {
        case DOCUMENT:
        case SCOPE_DOCUMENT:
        default:
            setState(State.NAME);
            break;
        case ARRAY:
        case JAVASCRIPT_WITH_SCOPE:
        case TOP_LEVEL:
            setState(State.VALUE);
            break;
    }
    return getCurrentBsonType();
}
Also used : MinKey(org.bson.types.MinKey) MaxKey(org.bson.types.MaxKey) String(java.lang.String) JsonParseException(org.bson.json.JsonParseException)

Example 15 with MaxKey

use of org.bson.types.MaxKey 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)

Aggregations

MaxKey (org.bson.types.MaxKey)16 MinKey (org.bson.types.MinKey)16 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)6 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 String (java.lang.String)2 SimpleDateFormat (java.text.SimpleDateFormat)2