Search in sources :

Example 46 with BasicBSONObject

use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.

the class JSONPigReplaceTest method testSimpleNestedReplace.

@Test
public void testSimpleNestedReplace() throws Exception {
    // create tuple ({("Daniel", "Alabi")}, "Carleton College")
    // with schema 'b:{t:(f:chararray,l:chararray)}, s:chararray'
    Tuple t1 = tupleFactory.newTuple(2);
    t1.set(0, "Daniel");
    t1.set(1, "Alabi");
    DataBag b = bagFactory.newDefaultBag();
    b.add(t1);
    Tuple t = tupleFactory.newTuple(2);
    t.set(0, b);
    t.set(1, "Carleton College");
    JSONPigReplace j = new JSONPigReplace(new String[] { "{first:'$f', last:'$l', school:'$s'}" });
    BasicBSONObject[] bs = j.substitute(t, "b:{t:(f:chararray,l:chararray)}, s:chararray", null);
    assertNotNull(bs);
    assertTrue(bs.length == 1);
    // should produce
    // { "first" : "Daniel" , "last" : "Alabi" , "school" : "Carleton College"}
    BasicBSONObject res = bs[0];
    assertEquals(res.get("first"), "Daniel");
    assertEquals(res.get("last"), "Alabi");
    assertEquals(res.get("school"), "Carleton College");
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) DataBag(org.apache.pig.data.DataBag) Tuple(org.apache.pig.data.Tuple) Test(org.junit.Test)

Example 47 with BasicBSONObject

use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.

the class JSONPigReplaceTest method testSampleQueryUpdateReplace.

@Test
public void testSampleQueryUpdateReplace() throws Exception {
    // create tuple ("Daniel", "Alabi", 19, {("a"), ("b"), ("c")})
    // with schema 'f:chararray,l:chararray,age:int,cars:{t:(t:chararray)}'
    DataBag b = bagFactory.newDefaultBag();
    b.add(tupleFactory.newTuple("a"));
    b.add(tupleFactory.newTuple("b"));
    b.add(tupleFactory.newTuple("c"));
    Tuple t = tupleFactory.newTuple(4);
    t.set(0, "Daniel");
    t.set(1, "Alabi");
    t.set(2, 19);
    t.set(3, b);
    JSONPigReplace j = new JSONPigReplace(new String[] { "{first:'$f', last:'$l'}", "{$set: {age: '$age'}, $pushAll : {cars: '$cars'}}" });
    BasicBSONObject[] bs = j.substitute(t, "f:chararray,l:chararray,age:int,cars:{t:(t:chararray)}", "t");
    assertTrue(bs.length == 2);
    // should produce
    // { "first" : "Daniel" , "last" : "Alabi"}
    // { "$set" : { "age" : 19} , "$pushAll" : { "cars" : [ "a" , "b" , "c"]}}
    BasicBSONObject res1 = bs[0];
    BasicBSONObject res2 = bs[1];
    assertEquals(res1.get("first"), "Daniel");
    assertEquals(((BasicBSONObject) res2.get("$set")).get("age"), 19);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) DataBag(org.apache.pig.data.DataBag) Tuple(org.apache.pig.data.Tuple) Test(org.junit.Test)

Example 48 with BasicBSONObject

use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.

the class BSONSerDeTest method testDates.

@Test
public void testDates() throws SerDeException {
    String columnNames = "d";
    String columnTypes = "timestamp";
    Date d = new Date();
    Timestamp value = new Timestamp(d.getTime());
    BSONSerDe serde = new BSONSerDe();
    Object result = helpDeserialize(serde, columnNames, columnTypes, value);
    assertThat(value, equalTo(result));
    result = serde.deserializeField(d, serde.columnTypes.get(0), "");
    assertThat(value, equalTo(result));
    BSONTimestamp bts = new BSONTimestamp(((Long) (d.getTime() / 1000L)).intValue(), 1);
    result = serde.deserializeField(bts, serde.columnTypes.get(0), "");
    // BSONTimestamp only takes an int, so the long returned in the Timestamp won't be the same
    assertThat((long) bts.getTime(), equalTo(((Timestamp) result).getTime() / 1000L));
    // Utilizes a timestampWritable because there's no native timestamp type in java for
    // object inspector class to relate to
    ObjectInspector innerInspector = PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(TimestampWritable.class);
    BasicBSONObject bObject = new BasicBSONObject();
    BSONWritable serialized = (BSONWritable) helpSerialize(columnNames, innerInspector, bObject, new TimestampWritable(value), serde);
    // The time going in to serialize is Timestamp but it comes out as BSONTimestamp
    BasicBSONObject bsonWithTimestamp = new BasicBSONObject();
    bsonWithTimestamp.put(columnNames, bts);
    assertThat(value.getTime(), equalTo(((Date) serialized.getDoc().get(columnNames)).getTime()));
}
Also used : BSONWritable(com.mongodb.hadoop.io.BSONWritable) BasicBSONObject(org.bson.BasicBSONObject) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) BSONTimestamp(org.bson.types.BSONTimestamp) TimestampWritable(org.apache.hadoop.hive.serde2.io.TimestampWritable) BasicBSONObject(org.bson.BasicBSONObject) Timestamp(java.sql.Timestamp) BSONTimestamp(org.bson.types.BSONTimestamp) Date(java.util.Date) Test(org.junit.Test)

Example 49 with BasicBSONObject

use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.

the class JSONPigReplace method fillReplacementMap.

/*
     * Fills map of replacement strings (reps) with entries that 
     * map replacement strings to corresponding objects to replace these strings with
     * 
     * @param Object pObj : Object representing pig tuple
     */
private void fillReplacementMap(final Object pObj) throws IOException {
    if (pObj instanceof BasicBSONObject || pObj instanceof Map) {
        @SuppressWarnings("unchecked") Map<String, Object> p = (Map<String, Object>) pObj;
        Object val;
        for (String k : p.keySet()) {
            val = p.get(k);
            if (reps.containsKey(k)) {
                reps.put(k, val);
            } else // check if 'val' is an array or an embedded BSON document
            if (val instanceof BasicBSONObject || val instanceof ArrayList) {
                fillReplacementMap(val);
            }
        }
    } else if (pObj instanceof ArrayList) {
        for (Object o : (ArrayList) pObj) {
            fillReplacementMap(o);
        }
    }
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) ArrayList(java.util.ArrayList) BasicBSONObject(org.bson.BasicBSONObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 50 with BasicBSONObject

use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.

the class JSONPigReplace method replaceAll.

/*
     * static method to
     * use reps (map of strings to replace to object  -> corresponding replacements)
     * to make replacements in the BSONObject to act on
     * 
     * @param BasicBSONObject in : BSONObject to make replacements in
     * @param HashMap<String, Object> reps : replacement map  
     * 
     * @return BasicBSONObject : result of replacing "marked" strings specified in reps
     */
public static BasicBSONObject replaceAll(final BasicBSONObject in, final Map<String, Object> reps) {
    if (in == null) {
        throw new IllegalArgumentException("JSON/BasicBSONObject to make substitutions in cannot be null!");
    }
    BasicBSONObject res = new BasicBSONObject();
    String k;
    Object v;
    for (Entry<String, Object> e : in.entrySet()) {
        k = e.getKey();
        v = e.getValue();
        // v is a nested BasicBSONObject or an array
        if (v instanceof BasicBSONObject) {
            res.put(k, replaceAll((BasicBSONObject) v, reps));
        } else {
            if (v instanceof String && ((String) v).startsWith("$")) {
                res.put(k, reps.get(((String) v).substring(1)));
            } else {
                res.put(k, v);
            }
        }
    }
    return res;
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject)

Aggregations

BasicBSONObject (org.bson.BasicBSONObject)71 Test (org.junit.Test)23 BSONObject (org.bson.BSONObject)20 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)14 BSONWritable (com.mongodb.hadoop.io.BSONWritable)13 ArrayList (java.util.ArrayList)13 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)13 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)13 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)13 BasicDBObject (com.mongodb.BasicDBObject)11 ObjectId (org.bson.types.ObjectId)11 IOException (java.io.IOException)7 DataBag (org.apache.pig.data.DataBag)6 Tuple (org.apache.pig.data.Tuple)5 Mongo (com.mongodb.Mongo)4 Date (java.util.Date)4 Map (java.util.Map)4 DoubleWritable (org.apache.hadoop.io.DoubleWritable)4 Text (org.apache.hadoop.io.Text)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3