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));
}
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));
}
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));
}
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));
}
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);
}
Aggregations