Search in sources :

Example 1 with BsonType

use of org.bson.BsonType in project core-ng-project by neowu.

the class BsonReaderWrapper method startReadMap.

public Map<String, ?> startReadMap(String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType == BsonType.NULL) {
        reader.readNull();
        return null;
    }
    if (currentType != BsonType.DOCUMENT) {
        logger.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return null;
    }
    return new LinkedHashMap<>();
}
Also used : BsonType(org.bson.BsonType) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with BsonType

use of org.bson.BsonType in project debezium by debezium.

the class MongoDataConverter method convertFieldValue.

public static void convertFieldValue(Entry<String, BsonValue> keyvalueforStruct, Struct struct, Schema schema) {
    Object colValue = null;
    String key = keyvalueforStruct.getKey();
    BsonType type = keyvalueforStruct.getValue().getBsonType();
    switch(type) {
        case NULL:
            colValue = null;
            break;
        case STRING:
            colValue = keyvalueforStruct.getValue().asString().getValue().toString();
            break;
        case OBJECT_ID:
            colValue = keyvalueforStruct.getValue().asObjectId().getValue().toString();
            break;
        case DOUBLE:
            colValue = keyvalueforStruct.getValue().asDouble().getValue();
            break;
        case BINARY:
            colValue = keyvalueforStruct.getValue().asBinary().getData();
            break;
        case INT32:
            colValue = keyvalueforStruct.getValue().asInt32().getValue();
            break;
        case INT64:
            colValue = keyvalueforStruct.getValue().asInt64().getValue();
            break;
        case BOOLEAN:
            colValue = keyvalueforStruct.getValue().asBoolean().getValue();
            break;
        case DATE_TIME:
            colValue = keyvalueforStruct.getValue().asDateTime().getValue();
            break;
        case JAVASCRIPT:
            colValue = keyvalueforStruct.getValue().asJavaScript().getCode();
            break;
        case JAVASCRIPT_WITH_SCOPE:
            Struct jsStruct = new Struct(schema.field(keyvalueforStruct.getKey()).schema());
            Struct jsScopeStruct = new Struct(schema.field(keyvalueforStruct.getKey()).schema().field("scope").schema());
            jsStruct.put("code", keyvalueforStruct.getValue().asJavaScriptWithScope().getCode());
            BsonDocument jwsDoc = keyvalueforStruct.getValue().asJavaScriptWithScope().getScope().asDocument();
            for (Entry<String, BsonValue> jwsDocKey : jwsDoc.entrySet()) {
                convertFieldValue(jwsDocKey, jsScopeStruct, schema.field(keyvalueforStruct.getKey()).schema());
            }
            jsStruct.put("scope", jsScopeStruct);
            colValue = jsStruct;
            break;
        case REGULAR_EXPRESSION:
            Struct regexStruct = new Struct(schema.field(keyvalueforStruct.getKey()).schema());
            regexStruct.put("regex", keyvalueforStruct.getValue().asRegularExpression().getPattern());
            regexStruct.put("options", keyvalueforStruct.getValue().asRegularExpression().getOptions());
            colValue = regexStruct;
            break;
        case TIMESTAMP:
            colValue = keyvalueforStruct.getValue().asTimestamp().getTime();
            break;
        case DECIMAL128:
            colValue = keyvalueforStruct.getValue().asDecimal128().getValue().toString();
            break;
        case DOCUMENT:
            Schema documentSchema = schema.field(keyvalueforStruct.getKey()).schema();
            Struct documentStruct = new Struct(documentSchema);
            BsonDocument docs = keyvalueforStruct.getValue().asDocument();
            for (Entry<String, BsonValue> doc : docs.entrySet()) {
                convertFieldValue(doc, documentStruct, documentSchema);
            }
            colValue = documentStruct;
            break;
        case ARRAY:
            if (keyvalueforStruct.getValue().asArray().isEmpty()) {
                List<BsonValue> arrValues = keyvalueforStruct.getValue().asArray().getValues();
                ArrayList<String> list = new ArrayList<>();
                for (BsonValue value : arrValues) {
                    String temp = value.asString().getValue().toString();
                    list.add(temp);
                }
                colValue = list;
                break;
            } else {
                BsonType valueType = keyvalueforStruct.getValue().asArray().get(0).getBsonType();
                List<BsonValue> arrValues = keyvalueforStruct.getValue().asArray().getValues();
                ArrayList<Object> list = new ArrayList<>();
                arrValues.stream().forEach(arrValue -> {
                    if (arrValue.getBsonType() == BsonType.STRING && valueType == BsonType.STRING) {
                        String temp = arrValue.asString().getValue();
                        list.add(temp);
                    } else if (arrValue.getBsonType() == BsonType.JAVASCRIPT && valueType == BsonType.JAVASCRIPT) {
                        String temp = arrValue.asJavaScript().getCode();
                        list.add(temp);
                    } else if (arrValue.getBsonType() == BsonType.OBJECT_ID && valueType == BsonType.OBJECT_ID) {
                        String temp = arrValue.asObjectId().getValue().toString();
                        list.add(temp);
                    } else if (arrValue.getBsonType() == BsonType.DOUBLE && valueType == BsonType.DOUBLE) {
                        double temp = arrValue.asDouble().getValue();
                        list.add(temp);
                    } else if (arrValue.getBsonType() == BsonType.BINARY && valueType == BsonType.BINARY) {
                        byte[] temp = arrValue.asBinary().getData();
                        list.add(temp);
                    } else if (arrValue.getBsonType() == BsonType.INT32 && valueType == BsonType.INT32) {
                        int temp = arrValue.asInt32().getValue();
                        list.add(temp);
                    } else if (arrValue.getBsonType() == BsonType.INT64 && valueType == BsonType.INT64) {
                        long temp = arrValue.asInt64().getValue();
                        list.add(temp);
                    } else if (arrValue.getBsonType() == BsonType.DATE_TIME && valueType == BsonType.DATE_TIME) {
                        long temp = arrValue.asInt64().getValue();
                        list.add(temp);
                    } else if (arrValue.getBsonType() == BsonType.DECIMAL128 && valueType == BsonType.DECIMAL128) {
                        String temp = arrValue.asDecimal128().getValue().toString();
                        list.add(temp);
                    } else if (arrValue.getBsonType() == BsonType.TIMESTAMP && valueType == BsonType.TIMESTAMP) {
                        int temp = arrValue.asInt32().getValue();
                        list.add(temp);
                    } else if (arrValue.getBsonType() == BsonType.BOOLEAN && valueType == BsonType.BOOLEAN) {
                        boolean temp = arrValue.asBoolean().getValue();
                        list.add(temp);
                    } else if (arrValue.getBsonType() == BsonType.DOCUMENT && valueType == BsonType.DOCUMENT) {
                        Schema schema1 = schema.field(keyvalueforStruct.getKey()).schema().valueSchema();
                        Struct struct1 = new Struct(schema1);
                        for (Entry<String, BsonValue> entry9 : arrValue.asDocument().entrySet()) {
                            convertFieldValue(entry9, struct1, schema1);
                        }
                        list.add(struct1);
                    }
                });
                colValue = list;
            }
            break;
        default:
            break;
    }
    struct.put(key, keyvalueforStruct.getValue().isNull() ? null : colValue);
}
Also used : Schema(org.apache.kafka.connect.data.Schema) ArrayList(java.util.ArrayList) Struct(org.apache.kafka.connect.data.Struct) BsonType(org.bson.BsonType) Entry(java.util.Map.Entry) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue)

Example 3 with BsonType

use of org.bson.BsonType in project drill by apache.

the class BsonRecordReader method writeToListOrMap.

private void writeToListOrMap(BsonReader reader, final MapOrListWriterImpl writer, boolean isList, String fieldName) {
    writer.start();
    // writing
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        if (!isList) {
            fieldName = reader.readName();
        }
        BsonType currentBsonType = reader.getCurrentBsonType();
        switch(currentBsonType) {
            case INT32:
                int readInt32 = reader.readInt32();
                if (readNumbersAsDouble) {
                    writeDouble(readInt32, writer, fieldName, isList);
                } else {
                    writeInt32(readInt32, writer, fieldName, isList);
                }
                atLeastOneWrite = true;
                break;
            case INT64:
                long readInt64 = reader.readInt64();
                if (readNumbersAsDouble) {
                    writeDouble(readInt64, writer, fieldName, isList);
                } else {
                    writeInt64(readInt64, writer, fieldName, isList);
                }
                atLeastOneWrite = true;
                break;
            case ARRAY:
                reader.readStartArray();
                writeToListOrMap(reader, (MapOrListWriterImpl) writer.list(fieldName), true, fieldName);
                atLeastOneWrite = true;
                break;
            case BINARY:
                // handle types
                writeBinary(reader, writer, fieldName, isList);
                atLeastOneWrite = true;
                break;
            case BOOLEAN:
                boolean readBoolean = reader.readBoolean();
                writeBoolean(readBoolean, writer, fieldName, isList);
                atLeastOneWrite = true;
                break;
            case DATE_TIME:
                long readDateTime = reader.readDateTime();
                writeDateTime(readDateTime, writer, fieldName, isList);
                atLeastOneWrite = true;
                break;
            case DOCUMENT:
                reader.readStartDocument();
                // To handle nested Documents.
                MapOrListWriterImpl _writer = writer;
                if (!isList) {
                    _writer = (MapOrListWriterImpl) writer.map(fieldName);
                } else {
                    _writer = (MapOrListWriterImpl) writer.listoftmap(fieldName);
                }
                writeToListOrMap(reader, _writer, false, fieldName);
                atLeastOneWrite = true;
                break;
            case DOUBLE:
                double readDouble = reader.readDouble();
                writeDouble(readDouble, writer, fieldName, isList);
                atLeastOneWrite = true;
                break;
            case JAVASCRIPT:
                final String readJavaScript = reader.readJavaScript();
                writeString(readJavaScript, writer, fieldName, isList);
                atLeastOneWrite = true;
                break;
            case JAVASCRIPT_WITH_SCOPE:
                final String readJavaScriptWithScopeString = reader.readJavaScriptWithScope();
                writeString(readJavaScriptWithScopeString, writer, fieldName, isList);
                atLeastOneWrite = true;
                break;
            case NULL:
                // just read and ignore.
                reader.readNull();
                break;
            case OBJECT_ID:
                writeObjectId(reader, writer, fieldName, isList);
                atLeastOneWrite = true;
                break;
            case STRING:
                final String readString = reader.readString();
                writeString(readString, writer, fieldName, isList);
                atLeastOneWrite = true;
                break;
            case SYMBOL:
                final String readSymbol = reader.readSymbol();
                writeString(readSymbol, writer, fieldName, isList);
                atLeastOneWrite = true;
                break;
            case TIMESTAMP:
                int time = reader.readTimestamp().getTime();
                writeTimeStamp(time, writer, fieldName, isList);
                atLeastOneWrite = true;
                break;
            case DECIMAL128:
                BigDecimal readBigDecimalAsDecimal128 = reader.readDecimal128().bigDecimalValue();
                writeDecimal128(readBigDecimalAsDecimal128, writer, fieldName, isList);
                atLeastOneWrite = true;
                break;
            default:
                // Didn't handled REGULAR_EXPRESSION and DB_POINTER types
                throw new DrillRuntimeException("UnSupported Bson type: " + currentBsonType);
        }
    }
    if (!isList) {
        reader.readEndDocument();
    } else {
        reader.readEndArray();
    }
}
Also used : BsonType(org.bson.BsonType) MapOrListWriterImpl(org.apache.drill.exec.vector.complex.impl.MapOrListWriterImpl) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) BigDecimal(java.math.BigDecimal)

Example 4 with BsonType

use of org.bson.BsonType in project pinpoint by naver.

the class WriteContext method parseBsonValueObject.

private void parseBsonValueObject(BsonValue arg) {
    BsonType bsonType = arg.getBsonType();
    // write with same format of JsonWriter(JsonMode.STRICT)
    if (bsonType.equals(BsonType.DOUBLE)) {
        writeValue(arg.asDouble().getValue());
    } else if (bsonType.equals(BsonType.STRING)) {
        writeValue(arg.asString().getValue());
    } else if (bsonType.equals(BsonType.BINARY)) {
        String abbreviatedBinary = binaryAbbreviationForMongo(arg);
        bsonWriter.writeStartDocument();
        bsonWriter.writeName("$binary");
        writeValue(abbreviatedBinary);
        bsonWriter.writeName("$type");
        writeValue(String.valueOf(String.format("%02X", arg.asBinary().getType())));
        bsonWriter.writeEndDocument();
    } else if (bsonType.equals(BsonType.OBJECT_ID)) {
        bsonWriter.writeStartDocument();
        bsonWriter.writeName("$oid");
        writeValue(String.valueOf(arg.asObjectId().getValue()));
        bsonWriter.writeEndDocument();
    } else if (bsonType.equals(BsonType.BOOLEAN)) {
        writeValue(arg.asBoolean().getValue());
    } else if (bsonType.equals(BsonType.DATE_TIME)) {
        bsonWriter.writeStartDocument();
        bsonWriter.writeName("$date");
        writeValue(arg.asDateTime().getValue());
        bsonWriter.writeEndDocument();
    } else if (bsonType.equals(BsonType.REGULAR_EXPRESSION)) {
        bsonWriter.writeStartDocument();
        bsonWriter.writeName("$regex");
        writeValue(arg.asRegularExpression().getPattern());
        bsonWriter.writeName("$options");
        writeValue(arg.asRegularExpression().getOptions());
        bsonWriter.writeEndDocument();
    } else if (bsonType.equals(BsonType.DB_POINTER)) {
        bsonWriter.writeStartDocument();
        bsonWriter.writeName("$ref");
        writeValue(arg.asDBPointer().getNamespace());
        bsonWriter.writeName("$id");
        bsonWriter.writeStartDocument();
        bsonWriter.writeName("$oid");
        writeValue(String.valueOf(arg.asDBPointer().getId()));
        bsonWriter.writeEndDocument();
        bsonWriter.writeEndDocument();
    } else if (bsonType.equals(BsonType.JAVASCRIPT)) {
        bsonWriter.writeStartDocument();
        bsonWriter.writeName("$code");
        writeValue(arg.asJavaScript().getCode());
        bsonWriter.writeEndDocument();
    } else if (bsonType.equals(BsonType.SYMBOL)) {
        bsonWriter.writeStartDocument();
        bsonWriter.writeName("$symbol");
        writeValue(arg.asSymbol().getSymbol());
        bsonWriter.writeEndDocument();
    } else if (bsonType.equals(BsonType.JAVASCRIPT_WITH_SCOPE)) {
        bsonWriter.writeStartDocument();
        bsonWriter.writeName("$code");
        writeValue(arg.asJavaScriptWithScope().getCode());
        bsonWriter.writeName("$scope");
        writeValue(arg.asJavaScriptWithScope().getScope());
        bsonWriter.writeEndDocument();
    } else if (bsonType.equals(BsonType.INT32)) {
        writeValue(arg.asInt32().getValue());
    } else if (bsonType.equals(BsonType.TIMESTAMP)) {
        bsonWriter.writeStartDocument();
        bsonWriter.writeName("$timestamp");
        bsonWriter.writeStartDocument();
        bsonWriter.writeName("t");
        writeValue(arg.asTimestamp().getTime());
        bsonWriter.writeName("i");
        writeValue(arg.asTimestamp().getInc());
        bsonWriter.writeEndDocument();
        bsonWriter.writeEndDocument();
    } else if (bsonType.equals(BsonType.INT64)) {
        bsonWriter.writeStartDocument();
        bsonWriter.writeName("$numberLong");
        writeValue(String.valueOf(arg.asInt64().getValue()));
        bsonWriter.writeEndDocument();
    } else if (bsonType.equals(BsonType.UNDEFINED)) {
        bsonWriter.writeStartDocument();
        bsonWriter.writeName("$undefined");
        writeValue(true);
        bsonWriter.writeEndDocument();
    } else if (bsonType.equals(BsonType.NULL)) {
        writeValue(null);
    } else if (decimal128Enabled && bsonType.equals(BsonType.DECIMAL128)) {
        // since Mongo Java Driver 3.4
        bsonWriter.writeStartDocument();
        bsonWriter.writeName("$numberDecimal");
        writeValue(String.valueOf(arg.asDecimal128().getValue()));
        bsonWriter.writeEndDocument();
    }
// BsonType.DOCUMENT //taken care of in Bson
// BsonType.ARRAY //taken care of in collection
// BsonType.END_OF_DOCUMENT //do nothing
}
Also used : BsonType(org.bson.BsonType)

Example 5 with BsonType

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

the class DBObjectCodec method readValue.

@Nullable
private Object readValue(final BsonReader reader, final DecoderContext decoderContext, @Nullable final String fieldName, final List<String> path) {
    Object initialRetVal;
    BsonType bsonType = reader.getCurrentBsonType();
    if (bsonType.isContainer() && fieldName != null) {
        // if we got into some new context like nested document or array
        path.add(fieldName);
    }
    switch(bsonType) {
        case DOCUMENT:
            initialRetVal = verifyForDBRef(readDocument(reader, decoderContext, path));
            break;
        case ARRAY:
            initialRetVal = readArray(reader, decoderContext, path);
            break;
        case // custom for driver-compat types
        JAVASCRIPT_WITH_SCOPE:
            initialRetVal = readCodeWScope(reader, decoderContext, path);
            break;
        case // custom for driver-compat types
        DB_POINTER:
            BsonDbPointer dbPointer = reader.readDBPointer();
            initialRetVal = new DBRef(dbPointer.getNamespace(), dbPointer.getId());
            break;
        case BINARY:
            initialRetVal = readBinary(reader, decoderContext);
            break;
        case NULL:
            reader.readNull();
            initialRetVal = null;
            break;
        default:
            initialRetVal = bsonTypeCodecMap.get(bsonType).decode(reader, decoderContext);
    }
    if (bsonType.isContainer() && fieldName != null) {
        // step out of current context to a parent
        path.remove(fieldName);
    }
    return initialRetVal;
}
Also used : BsonType(org.bson.BsonType) BSONObject(org.bson.BSONObject) BsonDbPointer(org.bson.BsonDbPointer) Nullable(com.mongodb.lang.Nullable)

Aggregations

BsonType (org.bson.BsonType)20 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)4 MapOrListWriterImpl (org.apache.drill.exec.vector.complex.impl.MapOrListWriterImpl)4 BsonInvalidOperationException (org.bson.BsonInvalidOperationException)3 Decimal128 (org.bson.types.Decimal128)3 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 Schema (org.apache.kafka.connect.data.Schema)2 BsonDocument (org.bson.BsonDocument)2 BsonValue (org.bson.BsonValue)2 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 Nullable (com.mongodb.lang.Nullable)1 MappingException (dev.morphia.mapping.MappingException)1 Annotation (java.lang.annotation.Annotation)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 Entry (java.util.Map.Entry)1 UUID (java.util.UUID)1 SchemaBuilder (org.apache.kafka.connect.data.SchemaBuilder)1