Search in sources :

Example 16 with BsonType

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

the class BsonReaderWrapper method startReadEntity.

public boolean startReadEntity(String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType != null && currentType == BsonType.NULL) {
        reader.readNull();
        return false;
    }
    if (currentType != null && currentType != BsonType.DOCUMENT) {
        logger.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return false;
    }
    return true;
}
Also used : BsonType(org.bson.BsonType)

Example 17 with BsonType

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

the class BsonReaderWrapper method startReadList.

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

Example 18 with BsonType

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

the class MongoDataConverter method addFieldSchema.

public static void addFieldSchema(Entry<String, BsonValue> keyValuesforSchema, SchemaBuilder builder) {
    String key = keyValuesforSchema.getKey();
    BsonType type = keyValuesforSchema.getValue().getBsonType();
    switch(type) {
        case NULL:
            LOG.warn("Data type {} not currently supported", type);
            break;
        case STRING:
        case JAVASCRIPT:
        case OBJECT_ID:
        case DECIMAL128:
            builder.field(key, Schema.OPTIONAL_STRING_SCHEMA);
            break;
        case DOUBLE:
            builder.field(key, Schema.OPTIONAL_FLOAT64_SCHEMA);
            break;
        case BINARY:
            builder.field(key, Schema.OPTIONAL_BYTES_SCHEMA);
            break;
        case INT32:
        case TIMESTAMP:
            builder.field(key, Schema.OPTIONAL_INT32_SCHEMA);
            break;
        case INT64:
        case DATE_TIME:
            builder.field(key, Schema.OPTIONAL_INT64_SCHEMA);
            break;
        case BOOLEAN:
            builder.field(key, Schema.OPTIONAL_BOOLEAN_SCHEMA);
            break;
        case JAVASCRIPT_WITH_SCOPE:
            SchemaBuilder jswithscope = SchemaBuilder.struct();
            jswithscope.field("code", Schema.OPTIONAL_STRING_SCHEMA);
            SchemaBuilder scope = SchemaBuilder.struct();
            BsonDocument jwsDocument = keyValuesforSchema.getValue().asJavaScriptWithScope().getScope().asDocument();
            for (Entry<String, BsonValue> jwsDocumentKey : jwsDocument.entrySet()) {
                addFieldSchema(jwsDocumentKey, scope);
            }
            Schema scopeBuild = scope.build();
            jswithscope.field("scope", scopeBuild).build();
            builder.field(key, jswithscope);
            break;
        case REGULAR_EXPRESSION:
            SchemaBuilder regexwop = SchemaBuilder.struct();
            regexwop.field("regex", Schema.OPTIONAL_STRING_SCHEMA);
            regexwop.field("options", Schema.OPTIONAL_STRING_SCHEMA);
            builder.field(key, regexwop.build());
            break;
        case DOCUMENT:
            SchemaBuilder builderDoc = SchemaBuilder.struct();
            BsonDocument docs = keyValuesforSchema.getValue().asDocument();
            for (Entry<String, BsonValue> doc : docs.entrySet()) {
                addFieldSchema(doc, builderDoc);
            }
            builder.field(key, builderDoc.build());
            break;
        case ARRAY:
            if (keyValuesforSchema.getValue().asArray().isEmpty()) {
                builder.field(key, SchemaBuilder.array(Schema.OPTIONAL_STRING_SCHEMA).build());
                break;
            } else {
                BsonType valueType = keyValuesforSchema.getValue().asArray().get(0).getBsonType();
                switch(valueType) {
                    case NULL:
                        LOG.warn("Data type {} not currently supported", valueType);
                        break;
                    case STRING:
                    case JAVASCRIPT:
                    case OBJECT_ID:
                    case DECIMAL128:
                        builder.field(key, SchemaBuilder.array(Schema.OPTIONAL_STRING_SCHEMA).build());
                        break;
                    case DOUBLE:
                        builder.field(key, SchemaBuilder.array(Schema.OPTIONAL_FLOAT64_SCHEMA).build());
                        break;
                    case BINARY:
                        builder.field(key, SchemaBuilder.array(Schema.OPTIONAL_BYTES_SCHEMA).build());
                        break;
                    case INT32:
                    case TIMESTAMP:
                        builder.field(key, SchemaBuilder.array(Schema.OPTIONAL_INT32_SCHEMA).build());
                        break;
                    case INT64:
                    case DATE_TIME:
                        builder.field(key, SchemaBuilder.array(Schema.OPTIONAL_INT64_SCHEMA).build());
                        break;
                    case BOOLEAN:
                        builder.field(key, SchemaBuilder.array(Schema.OPTIONAL_BOOLEAN_SCHEMA).build());
                        break;
                    case DOCUMENT:
                        SchemaBuilder documentSchemaBuilder = SchemaBuilder.struct();
                        BsonDocument arrayDocs = keyValuesforSchema.getValue().asArray().get(0).asDocument();
                        for (Entry<String, BsonValue> arrayDoc : arrayDocs.entrySet()) {
                            addFieldSchema(arrayDoc, documentSchemaBuilder);
                        }
                        Schema build = documentSchemaBuilder.build();
                        builder.field(key, SchemaBuilder.array(build).build());
                        break;
                    default:
                        break;
                }
                break;
            }
        default:
            break;
    }
}
Also used : BsonType(org.bson.BsonType) BsonDocument(org.bson.BsonDocument) Schema(org.apache.kafka.connect.data.Schema) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) BsonValue(org.bson.BsonValue)

Example 19 with BsonType

use of org.bson.BsonType in project morphia by mongodb.

the class ObjectCodec method decode.

@Override
public Object decode(BsonReader reader, DecoderContext decoderContext) {
    BsonType bsonType = reader.getCurrentBsonType();
    Class<?> clazz;
    if (bsonType == BsonType.DOCUMENT) {
        clazz = Document.class;
        String discriminatorField = datastore.getMapper().getOptions().getDiscriminatorKey();
        BsonReaderMark mark = reader.getMark();
        reader.readStartDocument();
        while (clazz.equals(Document.class) && reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            if (reader.readName().equals(discriminatorField)) {
                try {
                    clazz = datastore.getMapper().getClass(reader.readString());
                } catch (CodecConfigurationException e) {
                    throw new MappingException(e.getMessage(), e);
                }
            } else {
                reader.skipValue();
            }
        }
        mark.reset();
    } else {
        clazz = bsonTypeClassMap.get(bsonType);
    }
    return datastore.getCodecRegistry().get(clazz).decode(reader, decoderContext);
}
Also used : BsonType(org.bson.BsonType) BsonReaderMark(org.bson.BsonReaderMark) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) Document(org.bson.Document) MappingException(dev.morphia.mapping.MappingException)

Example 20 with BsonType

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

the class BsonRecordReader method write.

public void write(ComplexWriter writer, BsonReader reader) throws IOException {
    this.reader = reader;
    reader.readStartDocument();
    BsonType readBsonType = reader.getCurrentBsonType();
    switch(readBsonType) {
        case DOCUMENT:
            writeToListOrMap(reader, new MapOrListWriterImpl(writer.rootAsMap()), false, null);
            break;
        default:
            throw new DrillRuntimeException("Root object must be DOCUMENT type. Found: " + readBsonType);
    }
}
Also used : BsonType(org.bson.BsonType) MapOrListWriterImpl(org.apache.drill.exec.vector.complex.impl.MapOrListWriterImpl) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException)

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