Search in sources :

Example 6 with DocumentCodec

use of org.bson.codecs.DocumentCodec in project drill by apache.

the class MongoGroupScan method getScanStats.

@Override
public ScanStats getScanStats() {
    try {
        MongoClient client = storagePlugin.getClient();
        MongoDatabase db = client.getDatabase(scanSpec.getDbName());
        MongoCollection<Document> collection = db.getCollection(scanSpec.getCollectionName());
        long numDocs = collection.count();
        float approxDiskCost = 0;
        if (numDocs != 0) {
            //toJson should use client's codec, otherwise toJson could fail on
            // some types not known to DocumentCodec, e.g. DBRef.
            final DocumentCodec codec = new DocumentCodec(client.getMongoClientOptions().getCodecRegistry(), new BsonTypeClassMap());
            String json = collection.find().first().toJson(codec);
            approxDiskCost = json.getBytes().length * numDocs;
        }
        return new ScanStats(GroupScanProperty.EXACT_ROW_COUNT, numDocs, 1, approxDiskCost);
    } catch (Exception e) {
        throw new DrillRuntimeException(e.getMessage(), e);
    }
}
Also used : MongoClient(com.mongodb.MongoClient) DocumentCodec(org.bson.codecs.DocumentCodec) BsonTypeClassMap(org.bson.codecs.BsonTypeClassMap) Document(org.bson.Document) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) PhysicalOperatorSetupException(org.apache.drill.exec.physical.PhysicalOperatorSetupException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) ExecutionSetupException(org.apache.drill.common.exceptions.ExecutionSetupException) IOException(java.io.IOException) MongoDatabase(com.mongodb.client.MongoDatabase) ScanStats(org.apache.drill.exec.physical.base.ScanStats)

Aggregations

Document (org.bson.Document)6 DocumentCodec (org.bson.codecs.DocumentCodec)6 BsonDocument (org.bson.BsonDocument)2 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)2 Test (org.junit.Test)2 MongoClient (com.mongodb.MongoClient)1 MongoNamespace (com.mongodb.MongoNamespace)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 CollectionHelper (com.mongodb.client.test.CollectionHelper)1 ServerVersion (com.mongodb.connection.ServerVersion)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Converter (org.apache.camel.Converter)1 IOConverter (org.apache.camel.converter.IOConverter)1 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)1 ExecutionSetupException (org.apache.drill.common.exceptions.ExecutionSetupException)1 PhysicalOperatorSetupException (org.apache.drill.exec.physical.PhysicalOperatorSetupException)1 ScanStats (org.apache.drill.exec.physical.base.ScanStats)1 BsonArray (org.bson.BsonArray)1