Search in sources :

Example 6 with BasicDBObjectBuilder

use of com.mongodb.BasicDBObjectBuilder in project incubator-rya by apache.

the class TimestampPolicyMongoRyaStatementStore method fetchStatements.

@Override
public Iterator<RyaStatement> fetchStatements() throws FetchStatementException {
    final DBObject timeObj = new BasicDBObjectBuilder().add(SimpleMongoDBStorageStrategy.TIMESTAMP, new BasicDBObjectBuilder().add("$gte", timestamp.getTime()).get()).get();
    final Cursor cur = db.getCollection(TRIPLES_COLLECTION).find(timeObj).sort(new BasicDBObject(TIMESTAMP, 1));
    final List<RyaStatement> statements = new ArrayList<>();
    while (cur.hasNext()) {
        final RyaStatement statement = adapter.deserializeDBObject(cur.next());
        statements.add(statement);
    }
    return statements.iterator();
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) ArrayList(java.util.ArrayList) RyaStatement(org.apache.rya.api.domain.RyaStatement) Cursor(com.mongodb.Cursor) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 7 with BasicDBObjectBuilder

use of com.mongodb.BasicDBObjectBuilder in project mongo-hadoop by mongodb.

the class HiveMongoInputFormatTest method testTranslateConjoinedQuery.

@Test
public void testTranslateConjoinedQuery() {
    // i < 50
    GenericUDFOPLessThan lt = new GenericUDFOPLessThan();
    ExprNodeDesc[] iLt50Children = { new ExprNodeColumnDesc(new SimpleMockColumnInfo("i")), new ExprNodeConstantDesc(50) };
    ExprNodeGenericFuncDesc iLt50 = new ExprNodeGenericFuncDesc(TypeInfoFactory.booleanTypeInfo, lt, Arrays.asList(iLt50Children));
    // j > 20
    GenericUDFOPGreaterThan gt = new GenericUDFOPGreaterThan();
    ExprNodeDesc[] jGt20Children = { new ExprNodeColumnDesc(new SimpleMockColumnInfo("j")), new ExprNodeConstantDesc(20) };
    ExprNodeGenericFuncDesc jGt20 = new ExprNodeGenericFuncDesc(TypeInfoFactory.booleanTypeInfo, gt, Arrays.asList(jGt20Children));
    // i < 50 AND j > 20
    ExprNodeDesc[] andExprChildren = { iLt50, jGt20 };
    ExprNodeGenericFuncDesc expr = new ExprNodeGenericFuncDesc(TypeInfoFactory.booleanTypeInfo, new GenericUDFOPAnd(), Arrays.asList(andExprChildren));
    assertEquals(// {"$and": [{"i": {"$lt": 50}}, {"j": {"$gt": 20}}]}
    new BasicDBObjectBuilder().push("mongo_i").add("$lt", 50).pop().push("mongo_j").add("$gt", 20).pop().get(), filterForExpr(expr));
}
Also used : GenericUDFOPGreaterThan(org.apache.hadoop.hive.ql.udf.generic.GenericUDFOPGreaterThan) ExprNodeConstantDesc(org.apache.hadoop.hive.ql.plan.ExprNodeConstantDesc) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) ExprNodeColumnDesc(org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc) ExprNodeGenericFuncDesc(org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc) ExprNodeDesc(org.apache.hadoop.hive.ql.plan.ExprNodeDesc) GenericUDFOPLessThan(org.apache.hadoop.hive.ql.udf.generic.GenericUDFOPLessThan) GenericUDFOPAnd(org.apache.hadoop.hive.ql.udf.generic.GenericUDFOPAnd) HiveTest(com.mongodb.hadoop.hive.HiveTest) Test(org.junit.Test)

Example 8 with BasicDBObjectBuilder

use of com.mongodb.BasicDBObjectBuilder in project mongo-hadoop by mongodb.

the class HiveMongoInputFormatTest method testProjectionWithColumnMapping.

@Test
public void testProjectionWithColumnMapping() {
    DBObject mapping = new BasicDBObjectBuilder().add("i", "mongo_i").add("j", "mongo_j").add("id", "_id").get();
    String selectedColumns = "id,i";
    JobConf conf = new JobConf();
    conf.set(ColumnProjectionUtils.READ_COLUMN_NAMES_CONF_STR, selectedColumns);
    conf.set(BSONSerDe.MONGO_COLS, JSON.serialize(mapping));
    conf.setBoolean(ColumnProjectionUtils.READ_ALL_COLUMNS, false);
    // _id field is implicitly mapped to id field in Hive.
    assertEquals(new BasicDBObjectBuilder().add("mongo_i", 1).add("_id", 1).get(), inputFormat.getProjection(conf, colNameMapping));
}
Also used : BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) JobConf(org.apache.hadoop.mapred.JobConf) HiveTest(com.mongodb.hadoop.hive.HiveTest) Test(org.junit.Test)

Example 9 with BasicDBObjectBuilder

use of com.mongodb.BasicDBObjectBuilder in project mongo-hadoop by mongodb.

the class BSONStorage method putNext.

@Override
public void putNext(final Tuple tuple) throws IOException {
    try {
        final BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
        ResourceFieldSchema[] fields = null;
        if (schema != null) {
            fields = schema.getFields();
        }
        if (fields != null) {
            for (int i = 0; i < fields.length; i++) {
                writeField(builder, fields[i], tuple.get(i));
            }
        } else {
            for (int i = 0; i < tuple.size(); i++) {
                writeField(builder, null, tuple.get(i));
            }
        }
        out.write(null, builder.get());
    } catch (Exception e) {
        throw new IOException("Couldn't convert tuple to bson: ", e);
    }
}
Also used : BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) ResourceFieldSchema(org.apache.pig.ResourceSchema.ResourceFieldSchema) IOException(java.io.IOException) IOException(java.io.IOException)

Example 10 with BasicDBObjectBuilder

use of com.mongodb.BasicDBObjectBuilder in project mongo-hadoop by mongodb.

the class MongoInsertStorage method putNext.

@Override
public void putNext(final Tuple tuple) throws IOException {
    try {
        final BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
        ResourceFieldSchema[] fields = null;
        if (schema != null) {
            fields = schema.getFields();
        }
        if (fields != null) {
            for (int i = 0; i < fields.length; i++) {
                writeField(builder, fields[i], tuple.get(i));
            }
        } else {
            // MongoLoader, for example.
            if (tuple.size() != 1) {
                throw new IOException("Could not retrieve schema, but tuples did not contain a single item: " + tuple);
            }
            Object result = BSONStorage.getTypeForBSON(tuple.get(0), null, null);
            if (!(result instanceof Map)) {
                throw new IOException("Could not retrieve schema, but tuples contained something other than a Map: " + tuple);
            }
            Map<String, Object> documentMap = (Map<String, Object>) result;
            for (Map.Entry<String, Object> entry : documentMap.entrySet()) {
                builder.add(entry.getKey(), entry.getValue());
            }
        }
        out.write(null, builder.get());
    } catch (Exception e) {
        throw new IOException("Couldn't convert tuple to bson: ", e);
    }
}
Also used : BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) ResourceFieldSchema(org.apache.pig.ResourceSchema.ResourceFieldSchema) IOException(java.io.IOException) Map(java.util.Map) IOException(java.io.IOException)

Aggregations

BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)44 DBObject (com.mongodb.DBObject)30 Test (org.junit.Test)21 DBCollection (com.mongodb.DBCollection)18 BasicDBObject (com.mongodb.BasicDBObject)12 ArrayList (java.util.ArrayList)7 BasicDBList (com.mongodb.BasicDBList)6 List (java.util.List)4 ResourceFieldSchema (org.apache.pig.ResourceSchema.ResourceFieldSchema)4 HiveTest (com.mongodb.hadoop.hive.HiveTest)3 IOException (java.io.IOException)3 Map (java.util.Map)3 MongoException (com.mongodb.MongoException)2 Configuration (org.apache.hadoop.conf.Configuration)2 JobConf (org.apache.hadoop.mapred.JobConf)2 InputSplit (org.apache.hadoop.mapreduce.InputSplit)2 ResourceSchema (org.apache.pig.ResourceSchema)2 RyaStatement (org.apache.rya.api.domain.RyaStatement)2 BasicBSONObject (org.bson.BasicBSONObject)2 GeometryType (org.teiid.core.types.GeometryType)2