Search in sources :

Example 16 with BsonDocumentReader

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

the class TestBsonRecordReader method testObjectIdType.

@Test
public void testObjectIdType() throws IOException {
    BsonDocument bsonDoc = new BsonDocument();
    BsonObjectId value = new BsonObjectId(new ObjectId());
    bsonDoc.append("_idKey", value);
    writer.reset();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
    byte[] readByteArray = mapReader.reader("_idKey").readByteArray();
    assertArrayEquals(value.getValue().toByteArray(), readByteArray);
}
Also used : BsonDocument(org.bson.BsonDocument) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonDocumentReader(org.bson.BsonDocumentReader) BsonObjectId(org.bson.BsonObjectId) BaseTest(org.apache.drill.test.BaseTest) Test(org.junit.Test)

Example 17 with BsonDocumentReader

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

the class TestBsonRecordReader method testArrayOfDocumentType.

@Test
public void testArrayOfDocumentType() throws IOException {
    BsonDocument bsonDoc = new BsonDocument();
    BsonWriter bw = new BsonDocumentWriter(bsonDoc);
    bw.writeStartDocument();
    bw.writeName("a");
    bw.writeString("MongoDB");
    bw.writeName("b");
    bw.writeStartArray();
    bw.writeStartDocument();
    bw.writeName("c");
    bw.writeInt32(1);
    bw.writeEndDocument();
    bw.writeEndArray();
    bw.writeEndDocument();
    bw.flush();
    writer.reset();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    FieldReader reader = writer.getMapVector().getReader();
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) reader;
    FieldReader reader3 = mapReader.reader("b");
    assertEquals("MongoDB", mapReader.reader("a").readText().toString());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonWriter(org.bson.BsonWriter) BsonDocumentReader(org.bson.BsonDocumentReader) FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader) BaseTest(org.apache.drill.test.BaseTest) Test(org.junit.Test)

Example 18 with BsonDocumentReader

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

the class TestBsonRecordReader method testBooleanType.

@Test
public void testBooleanType() throws IOException {
    BsonDocument bsonDoc = new BsonDocument();
    bsonDoc.append("booleanKey", new BsonBoolean(true));
    writer.reset();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
    assertTrue(mapReader.reader("booleanKey").readBoolean());
}
Also used : BsonDocument(org.bson.BsonDocument) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonDocumentReader(org.bson.BsonDocumentReader) BsonBoolean(org.bson.BsonBoolean) BaseTest(org.apache.drill.test.BaseTest) Test(org.junit.Test)

Example 19 with BsonDocumentReader

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

the class TestBsonRecordReader method testTimeStampType.

@Test
public void testTimeStampType() throws IOException {
    BsonDocument bsonDoc = new BsonDocument();
    bsonDoc.append("ts_small", new BsonTimestamp(1000, 10));
    bsonDoc.append("ts_large", new BsonTimestamp(1000000000, 10));
    writer.reset();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
    assertEquals(1000000L, mapReader.reader("ts_small").readLocalDateTime().atZone(ZoneOffset.systemDefault()).toInstant().toEpochMilli());
    assertEquals(1000000000000L, mapReader.reader("ts_large").readLocalDateTime().atZone(ZoneOffset.systemDefault()).toInstant().toEpochMilli());
}
Also used : BsonDocument(org.bson.BsonDocument) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonDocumentReader(org.bson.BsonDocumentReader) BsonTimestamp(org.bson.BsonTimestamp) BaseTest(org.apache.drill.test.BaseTest) Test(org.junit.Test)

Example 20 with BsonDocumentReader

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

the class MongoRecordReader method next.

@Override
public int next() {
    if (cursor == null) {
        logger.debug("Filters Applied : " + filters);
        logger.debug("Fields Selected :" + fields);
        MongoIterable<BsonDocument> projection;
        if (CollectionUtils.isNotEmpty(operations)) {
            List<Bson> operations = new ArrayList<>(this.operations);
            if (!fields.isEmpty()) {
                operations.add(Aggregates.project(fields));
            }
            if (plugin.getConfig().allowDiskUse()) {
                projection = collection.aggregate(operations).allowDiskUse(true);
            } else {
                projection = collection.aggregate(operations);
            }
        } else {
            projection = collection.find(filters).projection(fields);
        }
        cursor = projection.batchSize(plugin.getConfig().getBatchSize()).iterator();
    }
    writer.allocate();
    writer.reset();
    int docCount = 0;
    Stopwatch watch = Stopwatch.createStarted();
    try {
        while (docCount < BaseValueVector.INITIAL_VALUE_ALLOCATION && cursor.hasNext()) {
            writer.setPosition(docCount);
            if (isBsonRecordReader) {
                BsonDocument bsonDocument = cursor.next();
                bsonReader.write(writer, new BsonDocumentReader(bsonDocument));
            } else {
                String doc = cursor.next().toJson();
                jsonReader.setSource(doc.getBytes(Charsets.UTF_8));
                jsonReader.write(writer);
            }
            docCount++;
        }
        if (isBsonRecordReader) {
            bsonReader.ensureAtLeastOneField(writer);
        } else {
            jsonReader.ensureAtLeastOneField(writer);
        }
        writer.setValueCount(docCount);
        logger.debug("Took {} ms to get {} records", watch.elapsed(TimeUnit.MILLISECONDS), docCount);
        return docCount;
    } catch (IOException e) {
        String msg = "Failure while reading document. - Parser was at record: " + (docCount + 1);
        logger.error(msg, e);
        throw new DrillRuntimeException(msg, e);
    }
}
Also used : BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) Stopwatch(org.apache.drill.shaded.guava.com.google.common.base.Stopwatch) BsonDocumentReader(org.bson.BsonDocumentReader) IOException(java.io.IOException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) Bson(org.bson.conversions.Bson)

Aggregations

BsonDocumentReader (org.bson.BsonDocumentReader)43 BsonDocument (org.bson.BsonDocument)42 Test (org.junit.Test)30 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)29 BaseTest (org.apache.drill.test.BaseTest)15 BsonString (org.bson.BsonString)9 FieldReader (org.apache.drill.exec.vector.complex.reader.FieldReader)6 BsonDocumentWriter (org.bson.BsonDocumentWriter)6 BsonWriter (org.bson.BsonWriter)6 Test (org.junit.jupiter.api.Test)5 ArrayList (java.util.ArrayList)3 BsonBoolean (org.bson.BsonBoolean)3 BsonDateTime (org.bson.BsonDateTime)3 BsonDouble (org.bson.BsonDouble)3 BsonInt32 (org.bson.BsonInt32)3 BsonInt64 (org.bson.BsonInt64)3 BsonNull (org.bson.BsonNull)3 ProjectedTuple (org.immutables.criteria.backend.ProjectedTuple)3 Path (org.immutables.criteria.expression.Path)3 Query (org.immutables.criteria.expression.Query)3