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