Search in sources :

Example 6 with MapOrListWriterImpl

use of org.apache.drill.exec.vector.complex.impl.MapOrListWriterImpl in project drill by axbaretto.

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;
            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)

Example 7 with MapOrListWriterImpl

use of org.apache.drill.exec.vector.complex.impl.MapOrListWriterImpl in project drill by axbaretto.

the class BsonRecordReader method write.

public void write(ComplexWriter writer, BsonReader reader) throws IOException {
    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)

Example 8 with MapOrListWriterImpl

use of org.apache.drill.exec.vector.complex.impl.MapOrListWriterImpl in project drill by apache.

the class ProjectionPassthroughVectorWriter method writeDBDocument.

@Override
protected void writeDBDocument(VectorContainerWriter vectorWriter, DBDocumentReaderBase reader) throws SchemaChangeException {
    if (reader.next() != EventType.START_MAP) {
        throw dataReadError(logger, "The document did not start with START_MAP!");
    }
    MapOrListWriterImpl writer = new MapOrListWriterImpl(vectorWriter.rootAsMap());
    writer.start();
    MapOrListWriter documentMapWriter = writer.map(DBConstants.DOCUMENT_FIELD);
    documentMapWriter.start();
    // write _id field data
    if (includeId) {
        valueWriter.writeBinary(documentMapWriter, DocumentConstants.ID_KEY, reader.getIdData());
    }
    // write rest of the data buffers
    Map<Integer, ByteBuffer> dataMap = reader.getDataMap();
    for (Entry<Integer, ByteBuffer> familyData : dataMap.entrySet()) {
        valueWriter.writeBinary(documentMapWriter, String.valueOf(familyData.getKey()), familyData.getValue());
    }
    documentMapWriter.end();
    DocumentReaderWithProjection p = new DocumentReaderWithProjection(reader, projector);
    valueWriter.writeToListOrMap(writer, p);
}
Also used : MapOrListWriterImpl(org.apache.drill.exec.vector.complex.impl.MapOrListWriterImpl) DocumentReaderWithProjection(org.ojai.util.DocumentReaderWithProjection) MapOrListWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.MapOrListWriter) ByteBuffer(java.nio.ByteBuffer)

Example 9 with MapOrListWriterImpl

use of org.apache.drill.exec.vector.complex.impl.MapOrListWriterImpl 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

MapOrListWriterImpl (org.apache.drill.exec.vector.complex.impl.MapOrListWriterImpl)9 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)4 BsonType (org.bson.BsonType)4 HashMap (java.util.HashMap)2 Schema (org.apache.avro.Schema)2 Type (org.apache.avro.Schema.Type)2 GenericArray (org.apache.avro.generic.GenericArray)2 Stopwatch (com.google.common.base.Stopwatch)1 DBDocumentReaderBase (com.mapr.db.ojai.DBDocumentReaderBase)1 BigDecimal (java.math.BigDecimal)1 ByteBuffer (java.nio.ByteBuffer)1 UserException (org.apache.drill.common.exceptions.UserException)1 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)1 MapOrListWriter (org.apache.drill.exec.vector.complex.writer.BaseWriter.MapOrListWriter)1 DocumentReaderWithProjection (org.ojai.util.DocumentReaderWithProjection)1