Search in sources :

Example 16 with BasicBSONObject

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

the class BSONSerDeTest method testStruct.

@Test
public void testStruct() throws SerDeException {
    String columnNames = "m";
    String columnTypes = "struct<one:int,two:string>";
    BasicBSONObject value = new BasicBSONObject();
    int oneValue = 10;
    String twoValue = "key";
    value.put("one", oneValue);
    value.put("two", twoValue);
    // Structs come back as arrays
    ArrayList<Object> returned = new ArrayList<Object>();
    returned.add(oneValue);
    returned.add(twoValue);
    BSONSerDe serde = new BSONSerDe();
    Object result = helpDeserialize(serde, columnNames, columnTypes, value, true);
    assertThat(returned, equalTo(result));
    // A struct must have an array or list of inner inspector types
    ArrayList<ObjectInspector> innerInspectorList = new ArrayList<ObjectInspector>();
    innerInspectorList.add(PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(Integer.class));
    innerInspectorList.add(PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(String.class));
    // As well as a fields list
    ArrayList<String> innerFieldsList = new ArrayList<String>();
    innerFieldsList.add("one");
    innerFieldsList.add("two");
    // Then you get that inner struct's inspector
    StructObjectInspector structInspector = ObjectInspectorFactory.getStandardStructObjectInspector(innerFieldsList, innerInspectorList);
    // Which is used to get the overall struct inspector
    StructObjectInspector oi = createObjectInspector(columnNames, structInspector);
    // This should be how it turns out
    BasicBSONObject bObject = new BasicBSONObject();
    bObject.put(columnNames, value);
    // But structs are stored as array/list inside hive, so this is passed in
    ArrayList<Object> obj = new ArrayList<Object>();
    obj.add(returned);
    Object serialized = serde.serialize(obj, oi);
    assertThat(new BSONWritable(bObject), equalTo(serialized));
}
Also used : 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) ArrayList(java.util.ArrayList) BSONWritable(com.mongodb.hadoop.io.BSONWritable) BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) Test(org.junit.Test)

Example 17 with BasicBSONObject

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

the class BSONSerDeTest method testMap.

@Test
public void testMap() throws SerDeException {
    String columnNames = "m";
    String columnTypes = "map<string,int>";
    BasicBSONObject value = new BasicBSONObject();
    String oneKey = "one";
    int oneValue = 10;
    value.put(oneKey, oneValue);
    String twoKey = "two";
    int twoValue = 20;
    value.put(twoKey, twoValue);
    BSONSerDe serde = new BSONSerDe();
    Object result = helpDeserialize(serde, columnNames, columnTypes, value);
    assertThat(value.toMap(), equalTo(result));
    // Since objectid is currently taken to be a string
    ObjectInspector keyInspector = PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(String.class);
    ObjectInspector valueInspector = PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(Integer.class);
    MapObjectInspector mapInspector = ObjectInspectorFactory.getStandardMapObjectInspector(keyInspector, valueInspector);
    BasicBSONObject bObject = new BasicBSONObject();
    Object serialized = helpSerialize(columnNames, mapInspector, bObject, value, serde);
    assertThat(new BSONWritable(bObject), equalTo(serialized));
}
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) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) BasicBSONObject(org.bson.BasicBSONObject) Test(org.junit.Test)

Example 18 with BasicBSONObject

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

the class BSONSerDeTest method testBoolean.

@Test
public void testBoolean() throws SerDeException {
    String columnNames = "bool";
    String columnTypes = "boolean";
    Boolean value = false;
    BSONSerDe serde = new BSONSerDe();
    Object result = helpDeserialize(serde, columnNames, columnTypes, value);
    assertThat(value, equalTo(result));
    ObjectInspector innerInspector = PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(Boolean.class);
    BasicBSONObject bObject = new BasicBSONObject();
    Object serialized = helpSerialize(columnNames, innerInspector, bObject, value, serde);
    assertThat(new BSONWritable(bObject), equalTo(serialized));
}
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) BasicBSONObject(org.bson.BasicBSONObject) Test(org.junit.Test)

Example 19 with BasicBSONObject

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

the class BSONSerDeTest method testInt.

@Test
public void testInt() throws SerDeException {
    String columnNames = "i";
    String columnTypes = "int";
    Integer value = 1234;
    BSONSerDe serde = new BSONSerDe();
    Object result = helpDeserialize(serde, columnNames, columnTypes, value);
    assertThat(value, equalTo(result));
    ObjectInspector innerInspector = PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(Integer.class);
    BasicBSONObject bObject = new BasicBSONObject();
    Object serialized = helpSerialize(columnNames, innerInspector, bObject, value, serde);
    assertThat(new BSONWritable(bObject), equalTo(serialized));
}
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) BasicBSONObject(org.bson.BasicBSONObject) Test(org.junit.Test)

Example 20 with BasicBSONObject

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

the class LogReducer method reduce.

@Override
public void reduce(final Text pKey, final Iterable<IntWritable> pValues, final Context pContext) throws IOException, InterruptedException {
    int count = 0;
    for (IntWritable val : pValues) {
        count += val.get();
    }
    BasicBSONObject query = new BasicBSONObject("devices", new ObjectId(pKey.toString()));
    BasicBSONObject update = new BasicBSONObject("$inc", new BasicBSONObject("logs_count", count));
    if (LOG.isDebugEnabled()) {
        LOG.debug("query: " + query);
        LOG.debug("update: " + update);
    }
    reduceResult.setQuery(query);
    reduceResult.setModifiers(update);
    pContext.write(null, reduceResult);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) ObjectId(org.bson.types.ObjectId) IntWritable(org.apache.hadoop.io.IntWritable)

Aggregations

BasicBSONObject (org.bson.BasicBSONObject)88 Test (org.junit.Test)39 BSONObject (org.bson.BSONObject)37 ArrayList (java.util.ArrayList)15 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)14 BSONWritable (com.mongodb.hadoop.io.BSONWritable)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)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 DataBag (org.apache.pig.data.DataBag)6 Map (java.util.Map)5 Tuple (org.apache.pig.data.Tuple)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Mongo (com.mongodb.Mongo)4 Date (java.util.Date)4 DoubleWritable (org.apache.hadoop.io.DoubleWritable)4