use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class BSONSerDeTest method testDouble.
@Test
public void testDouble() throws SerDeException {
String columnNames = "doub";
String columnTypes = "double";
Double value = 1.1D;
BSONSerDe serde = new BSONSerDe();
Object result = helpDeserialize(serde, columnNames, columnTypes, value);
assertThat(value, equalTo(result));
ObjectInspector innerInspector = PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(Double.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 testNumericCasts.
@Test
public void testNumericCasts() throws SerDeException {
BSONSerDe serde = new BSONSerDe();
String colName = "cast";
Number[] nums = { 42.0D, 42, (short) 42, 42.0f, 42L };
Class[] numericClasses = { Double.class, Integer.class, Short.class, Float.class, Long.class };
for (Number num : nums) {
// Double
Object result = helpDeserialize(serde, colName, "double", num);
assertThat(num.doubleValue(), equalTo(result));
// Int
result = helpDeserialize(serde, colName, "int", num);
assertThat(num.intValue(), equalTo(result));
// Short
result = helpDeserialize(serde, colName, "smallint", num);
assertThat(num.shortValue(), equalTo(result));
// Float
result = helpDeserialize(serde, colName, "float", num);
assertThat(num.floatValue(), equalTo(result));
// Long
result = helpDeserialize(serde, colName, "bigint", num);
assertThat(num.longValue(), equalTo(result));
for (Class klass : numericClasses) {
ObjectInspector oi = PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(klass);
BasicBSONObject obj = new BasicBSONObject();
Object serialized = helpSerialize(colName, oi, obj, num, serde);
assertThat(new BSONWritable(obj), equalTo(serialized));
}
}
}
use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class MongoUpdateOutputReaderTest method testUpdate.
@Test
public void testUpdate() throws IOException {
BasicBSONObject query = new BasicDBObject("i", 42);
BasicBSONObject modifiers = new BasicDBObject("$set", new BasicDBObject("a", "b"));
DBObject update = new BasicDBObjectBuilder().add("_id", query).add("modifiers", modifiers).push("options").add("multi", true).add("upsert", false).pop().get();
MongoUpdateWritable muw = new MongoUpdateWritable(query, modifiers, false, true, false);
PipeMapRed pipeMapRed = mock(PipeMapRed.class);
when(pipeMapRed.getClientInput()).thenReturn(inputFromBSONObject(update));
MongoUpdateOutputReader reader = new MongoUpdateOutputReader();
reader.initialize(pipeMapRed);
assertTrue(reader.readKeyValue());
assertEquals(muw, reader.getCurrentValue());
}
use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class MongoRecordReader method nextKeyValue.
public boolean nextKeyValue() throws IOException {
try {
if (!cursor.hasNext()) {
LOG.info("Read " + seen + " documents from:");
LOG.info(split.toString());
return false;
}
DBObject next = cursor.next();
this.currentVal.setDoc(next);
this.currentKey.setDoc(new BasicBSONObject("_id", next.get("_id")));
seen++;
return true;
} catch (MongoException e) {
throw new IOException("Couldn't get next key/value from mongodb: ", e);
}
}
use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class BSONWritable method toBSON.
/**
* Unwrap a (usually Writable) Object, getting back a value suitable for
* putting into a BSONObject. If the given object is not Writable, then
* simply return the Object back.
*
* @param x the Object to turn into BSON.
* @return the BSON representation of the Object.
*/
@SuppressWarnings("unchecked")
public static Object toBSON(final Object x) {
if (x == null) {
return null;
}
if (x instanceof Text) {
return x.toString();
}
if (x instanceof BSONWritable) {
return ((BSONWritable) x).getDoc();
}
if (x instanceof Writable) {
if (x instanceof AbstractMapWritable) {
if (!(x instanceof Map)) {
throw new IllegalArgumentException(String.format("Cannot turn %s into BSON, since it does " + "not implement java.util.Map.", x.getClass().getName()));
}
Map<Writable, Writable> map = (Map<Writable, Writable>) x;
BasicBSONObject bson = new BasicBSONObject();
for (Map.Entry<Writable, Writable> entry : map.entrySet()) {
bson.put(entry.getKey().toString(), toBSON(entry.getValue()));
}
return bson;
}
if (x instanceof ArrayWritable) {
Writable[] o = ((ArrayWritable) x).get();
Object[] a = new Object[o.length];
for (int i = 0; i < o.length; i++) {
a[i] = toBSON(o[i]);
}
return a;
}
if (x instanceof NullWritable) {
return null;
}
if (x instanceof BooleanWritable) {
return ((BooleanWritable) x).get();
}
if (x instanceof BytesWritable) {
return ((BytesWritable) x).getBytes();
}
if (x instanceof ByteWritable) {
return ((ByteWritable) x).get();
}
if (x instanceof DoubleWritable) {
return ((DoubleWritable) x).get();
}
if (x instanceof FloatWritable) {
return ((FloatWritable) x).get();
}
if (x instanceof LongWritable) {
return ((LongWritable) x).get();
}
if (x instanceof IntWritable) {
return ((IntWritable) x).get();
}
// TODO - Support counters
}
return x;
}
Aggregations