Search in sources :

Example 11 with BasicBSONList

use of org.bson.types.BasicBSONList in project mongo-java-driver by mongodb.

the class JSONSerializersTest method testLegacySerialization.

@Test
public void testLegacySerialization() {
    ObjectSerializer serializer = JSONSerializers.getLegacy();
    BasicDBObject testObj = new BasicDBObject();
    // test  ARRAY
    BasicDBObject[] a = { new BasicDBObject("object1", "value1"), new BasicDBObject("object2", "value2") };
    testObj.put("array", a);
    StringBuilder buf = new StringBuilder();
    serializer.serialize(a, buf);
    assertEquals("[ { \"object1\" : \"value1\"} , { \"object2\" : \"value2\"}]", buf.toString());
    // test  BINARY
    byte[] b = { 1, 2, 3, 4 };
    testObj = new BasicDBObject("binary", new org.bson.types.Binary(b));
    buf = new StringBuilder();
    serializer.serialize(testObj, buf);
    assertEquals("{ \"binary\" : <Binary Data>}", buf.toString());
    // test  BOOLEAN
    testObj = new BasicDBObject("boolean", new Boolean(true));
    buf = new StringBuilder();
    serializer.serialize(testObj, buf);
    assertEquals(buf.toString(), "{ \"boolean\" : true}");
    // test  BSON_TIMESTAMP,
    testObj = new BasicDBObject("timestamp", new BSONTimestamp());
    buf = new StringBuilder();
    serializer.serialize(testObj, buf);
    assertEquals("{ \"timestamp\" : { \"$ts\" : 0 , \"$inc\" : 0}}", buf.toString());
    // test  BYTE_ARRAY
    testObj = new BasicDBObject("byte_array", b);
    buf = new StringBuilder();
    serializer.serialize(testObj, buf);
    assertEquals("{ \"byte_array\" : <Binary Data>}", buf.toString());
    // test  CODE
    testObj = new BasicDBObject("code", new Code("test code"));
    buf = new StringBuilder();
    serializer.serialize(testObj, buf);
    assertEquals("{ \"code\" : { \"$code\" : \"test code\"}}", buf.toString());
    // test  CODE_W_SCOPE
    testObj = new BasicDBObject("scope", "scope of code");
    CodeWScope codewscope = new CodeWScope("test code", testObj);
    buf = new StringBuilder();
    serializer.serialize(codewscope, buf);
    assertEquals("{ \"$code\" : \"test code\" , \"$scope\" : { \"scope\" : \"scope of code\"}}", buf.toString());
    // test  DATE
    Date d = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
    buf = new StringBuilder();
    serializer.serialize(d, buf);
    assertEquals("{ \"$date\" : \"" + format.format(d) + "\"}", buf.toString());
    // test  DB_OBJECT implicit in preceding tests
    // test  DB_REF_BASE
    DBRef dbref = new com.mongodb.DBRef("test.test", "4d83ab59a39562db9c1ae2af");
    buf = new StringBuilder();
    serializer.serialize(dbref, buf);
    assertEquals("{ \"$ref\" : \"test.test\" , \"$id\" : \"4d83ab59a39562db9c1ae2af\"}", buf.toString());
    DBRef dbrefWithDatabaseName = new com.mongodb.DBRef("mydb", "test.test", "4d83ab59a39562db9c1ae2af");
    buf = new StringBuilder();
    serializer.serialize(dbrefWithDatabaseName, buf);
    assertEquals("{ \"$ref\" : \"test.test\" , \"$id\" : \"4d83ab59a39562db9c1ae2af\" , \"$db\" : \"mydb\"}", buf.toString());
    // test  ITERABLE
    BasicBSONList testList = new BasicBSONList();
    testList.add(new BasicDBObject("key1", "val1"));
    testList.add(new BasicDBObject("key2", "val2"));
    buf = new StringBuilder();
    serializer.serialize(testList, buf);
    assertEquals("[ { \"key1\" : \"val1\"} , { \"key2\" : \"val2\"}]", buf.toString());
    // test  MAP
    Map<String, String> testMap = new TreeMap<String, String>();
    testMap.put("key1", "val1");
    testMap.put("key2", "val2");
    buf = new StringBuilder();
    serializer.serialize(testMap, buf);
    assertEquals("{ \"key1\" : \"val1\" , \"key2\" : \"val2\"}", buf.toString());
    // test  MAXKEY
    buf = new StringBuilder();
    serializer.serialize(new MaxKey(), buf);
    assertEquals("{ \"$maxKey\" : 1}", buf.toString());
    // test  MINKEY
    buf = new StringBuilder();
    serializer.serialize(new MinKey(), buf);
    assertEquals("{ \"$minKey\" : 1}", buf.toString());
    // test  NULL
    buf = new StringBuilder();
    serializer.serialize(null, buf);
    assertEquals(" null ", buf.toString());
    // test  NUMBER
    Random rand = new Random();
    long val = rand.nextLong();
    Long longVal = new Long(val);
    buf = new StringBuilder();
    serializer.serialize(longVal, buf);
    assertEquals(String.valueOf(val), buf.toString());
    // test  OBJECT_ID
    buf = new StringBuilder();
    serializer.serialize(new ObjectId("4d83ab3ea39562db9c1ae2ae"), buf);
    assertEquals("{ \"$oid\" : \"4d83ab3ea39562db9c1ae2ae\"}", buf.toString());
    // test  PATTERN
    buf = new StringBuilder();
    serializer.serialize(Pattern.compile("test"), buf);
    assertEquals("{ \"$regex\" : \"test\"}", buf.toString());
    // test  STRING
    buf = new StringBuilder();
    serializer.serialize("test string", buf);
    assertEquals("\"test string\"", buf.toString());
    // test  UUID;
    UUID uuid = UUID.randomUUID();
    buf = new StringBuilder();
    serializer.serialize(uuid, buf);
    assertEquals("{ \"$uuid\" : \"" + uuid.toString() + "\"}", buf.toString());
    // test Decimal128
    Decimal128 decimal128 = Decimal128.parse("3.140");
    buf = new StringBuilder();
    serializer.serialize(decimal128, buf);
    assertEquals("{ \"$numberDecimal\" : \"3.140\"}", buf.toString());
}
Also used : MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) BasicDBObject(com.mongodb.BasicDBObject) MinKey(org.bson.types.MinKey) Random(java.util.Random) SimpleTimeZone(java.util.SimpleTimeZone) UUID(java.util.UUID) ObjectId(org.bson.types.ObjectId) BasicBSONList(org.bson.types.BasicBSONList) GregorianCalendar(java.util.GregorianCalendar) DBRef(com.mongodb.DBRef) Decimal128(org.bson.types.Decimal128) TreeMap(java.util.TreeMap) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) Binary(org.bson.types.Binary) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 12 with BasicBSONList

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

the class BSONSerDe method deserializeList.

/**
 * Deserialize a List with the same listElemTypeInfo for its elements
 * @param value the value for which to get the Hive representation
 * @param valueTypeInfo a description of the value's type
 * @param ext the field name
 * @return the Hive representation of the value
 */
private Object deserializeList(final Object value, final ListTypeInfo valueTypeInfo, final String ext) {
    BasicBSONList list = (BasicBSONList) value;
    TypeInfo listElemTypeInfo = valueTypeInfo.getListElementTypeInfo();
    for (int i = 0; i < list.size(); i++) {
        list.set(i, deserializeField(list.get(i), listElemTypeInfo, ext));
    }
    return list.toArray();
}
Also used : BasicBSONList(org.bson.types.BasicBSONList) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)

Example 13 with BasicBSONList

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

the class BSONSerDe method serializeList.

private Object serializeList(final Object obj, final ListObjectInspector oi, final String ext) {
    BasicBSONList list = new BasicBSONList();
    List<?> field = oi.getList(obj);
    if (field == null) {
        return list;
    }
    ObjectInspector elemOI = oi.getListElementObjectInspector();
    for (Object elem : field) {
        list.add(serializeObject(elem, elemOI, ext));
    }
    return list;
}
Also used : ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) BasicBSONList(org.bson.types.BasicBSONList) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject)

Example 14 with BasicBSONList

use of org.bson.types.BasicBSONList 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 15 with BasicBSONList

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

the class TagsMapper method map.

@Override
protected void map(final Object key, final BSONObject value, final Context context) throws IOException, InterruptedException {
    BasicBSONList tags = (BasicBSONList) value.get("tags");
    Text text = new Text();
    value.removeField("tags");
    for (Object tag : tags) {
        text.set((String) tag);
        writable.setDoc(value);
        context.write(text, writable);
    }
}
Also used : BasicBSONList(org.bson.types.BasicBSONList) Text(org.apache.hadoop.io.Text) BSONObject(org.bson.BSONObject)

Aggregations

BasicBSONList (org.bson.types.BasicBSONList)19 BasicBSONObject (org.bson.BasicBSONObject)12 BSONObject (org.bson.BSONObject)11 Document (org.bson.Document)5 Test (org.junit.Test)3 BasicDBObject (com.mongodb.BasicDBObject)2 DBRef (com.mongodb.DBRef)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 TreeMap (java.util.TreeMap)2 UUID (java.util.UUID)2 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)2 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)2 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)2 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)2 Text (org.apache.hadoop.io.Text)2 Binary (org.bson.types.Binary)2 ObjectId (org.bson.types.ObjectId)2 BSONWritable (com.mongodb.hadoop.io.BSONWritable)1 SimpleDateFormat (java.text.SimpleDateFormat)1