Search in sources :

Example 11 with BSONWritable

use of com.mongodb.hadoop.io.BSONWritable in project mongo-hadoop by mongodb.

the class BSONSerDeTest method testBinary.

@Test
public void testBinary() throws SerDeException {
    String columnNames = "b";
    String columnTypes = "binary";
    byte[] value = new byte[2];
    value[0] = 'A';
    value[1] = '1';
    BSONSerDe serde = new BSONSerDe();
    Object result = helpDeserialize(serde, columnNames, columnTypes, value);
    assertThat(value, equalTo(result));
    ObjectInspector innerInspector = PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(byte[].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 12 with BSONWritable

use of com.mongodb.hadoop.io.BSONWritable in project mongo-hadoop by mongodb.

the class BSONSerDeTest method testObjectID.

@Test
public void testObjectID() throws SerDeException {
    String columnNames = "o";
    String columnTypes = "struct<oid:string,bsontype:int>";
    ObjectId value = new ObjectId();
    ArrayList<Object> returned = new ArrayList<Object>(2);
    returned.add(value.toString());
    returned.add(8);
    BSONSerDe serde = new BSONSerDe();
    Object result = helpDeserialize(serde, columnNames, columnTypes, value);
    assertThat(returned, equalTo(result));
    // Since objectid is currently taken to be a string
    ObjectInspector innerInspector = PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(String.class);
    BasicBSONObject bObject = new BasicBSONObject();
    Object serialized = helpSerialize(columnNames, innerInspector, bObject, value.toString(), 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) ObjectId(org.bson.types.ObjectId) ArrayList(java.util.ArrayList) BasicBSONObject(org.bson.BasicBSONObject) Test(org.junit.Test)

Example 13 with BSONWritable

use of com.mongodb.hadoop.io.BSONWritable in project mongo-hadoop by mongodb.

the class BSONSerDeTest method testList.

@Test
public void testList() throws SerDeException {
    String columnNames = "a";
    String columnTypes = "array<string>";
    String inner = "inside";
    ArrayList<String> value = new ArrayList<String>();
    value.add(inner);
    BasicBSONList b = new BasicBSONList();
    b.add(inner);
    BSONSerDe serde = new BSONSerDe();
    Object result = helpDeserialize(serde, columnNames, columnTypes, b);
    assertThat(value.toArray(), equalTo(result));
    // Since objectid is currently taken to be a string
    ObjectInspector innerInspector = PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(String.class);
    ListObjectInspector listInspector = ObjectInspectorFactory.getStandardListObjectInspector(innerInspector);
    BasicBSONObject bObject = new BasicBSONObject();
    Object serialized = helpSerialize(columnNames, listInspector, 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) BasicBSONList(org.bson.types.BasicBSONList) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) ArrayList(java.util.ArrayList) BasicBSONObject(org.bson.BasicBSONObject) Test(org.junit.Test)

Example 14 with BSONWritable

use of com.mongodb.hadoop.io.BSONWritable 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));
}
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 15 with BSONWritable

use of com.mongodb.hadoop.io.BSONWritable 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));
        }
    }
}
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)

Aggregations

BSONWritable (com.mongodb.hadoop.io.BSONWritable)21 BasicBSONObject (org.bson.BasicBSONObject)14 Test (org.junit.Test)13 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)11 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)11 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)11 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)11 BasicDBObject (com.mongodb.BasicDBObject)4 MongoUpdateWritable (com.mongodb.hadoop.io.MongoUpdateWritable)4 ArrayList (java.util.ArrayList)4 BSONObject (org.bson.BSONObject)4 DBObject (com.mongodb.DBObject)3 File (java.io.File)2 IOException (java.io.IOException)2 SerDeException (org.apache.hadoop.hive.serde2.SerDeException)2 NullWritable (org.apache.hadoop.io.NullWritable)2 FileSplit (org.apache.hadoop.mapred.FileSplit)2 JobConf (org.apache.hadoop.mapred.JobConf)2 BulkUpdateRequestBuilder (com.mongodb.BulkUpdateRequestBuilder)1 BulkWriteOperation (com.mongodb.BulkWriteOperation)1