Search in sources :

Example 1 with BasicBSONList

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

the class BSONLoader method convertBSONtoPigType.

/**
 * Convert an object from a MongoDB document into a type that Pig can
 * understand, based on the type of the input object.
 * @param o object from a MongoDB document
 * @return object appropriate for pig
 * @throws ExecException for lower-level Pig errors
 */
public static Object convertBSONtoPigType(final Object o) throws ExecException {
    if (o == null) {
        return null;
    } else if (o instanceof Number || o instanceof String) {
        return o;
    } else if (o instanceof Date) {
        return ((Date) o).getTime();
    } else if (o instanceof ObjectId) {
        return o.toString();
    } else if (o instanceof UUID) {
        return o.toString();
    } else if (o instanceof BasicBSONList) {
        BasicBSONList bl = (BasicBSONList) o;
        Tuple t = tupleFactory.newTuple(bl.size());
        for (int i = 0; i < bl.size(); i++) {
            t.set(i, convertBSONtoPigType(bl.get(i)));
        }
        return t;
    } else if (o instanceof Map) {
        // TODO make this more efficient for lazy objects?
        Map<String, Object> fieldsMap = (Map<String, Object>) o;
        HashMap<String, Object> pigMap = new HashMap<String, Object>(fieldsMap.size());
        for (Map.Entry<String, Object> field : fieldsMap.entrySet()) {
            pigMap.put(field.getKey(), convertBSONtoPigType(field.getValue()));
        }
        return pigMap;
    } else if (o instanceof byte[]) {
        return new DataByteArray((byte[]) o);
    } else if (o instanceof Binary) {
        return new DataByteArray(((Binary) o).getData());
    } else if (o instanceof DBRef) {
        HashMap<String, String> pigMap = new HashMap<String, String>(2);
        pigMap.put("$ref", ((DBRef) o).getCollectionName());
        pigMap.put("$id", ((DBRef) o).getId().toString());
        return pigMap;
    } else {
        return o;
    }
}
Also used : ObjectId(org.bson.types.ObjectId) HashMap(java.util.HashMap) BasicBSONList(org.bson.types.BasicBSONList) DBRef(com.mongodb.DBRef) Date(java.util.Date) BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) Binary(org.bson.types.Binary) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple)

Example 2 with BasicBSONList

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

the class TagsMapper method map.

@Override
public void map(final Object key, final BSONWritable value, final OutputCollector<Text, BSONWritable> output, final Reporter reporter) throws IOException {
    BasicBSONList tags = (BasicBSONList) value.getDoc().get("tags");
    Text text = new Text();
    value.getDoc().removeField("tags");
    for (Object tag : tags) {
        text.set((String) tag);
        output.collect(text, value);
    }
}
Also used : BasicBSONList(org.bson.types.BasicBSONList) Text(org.apache.hadoop.io.Text) BSONObject(org.bson.BSONObject)

Example 3 with BasicBSONList

use of org.bson.types.BasicBSONList in project Mycat-Server by MyCATApache.

the class SequoiaSQLParser method parserDBObject.

private void parserDBObject(BasicBSONObject ob, String akey, String aop, Object aval) {
    boolean isok = false;
    if (!(ob.keySet().isEmpty())) {
        for (String field : ob.keySet()) {
            if (akey.equals(field)) {
                Object val = ob.get(field);
                if (val instanceof BasicBSONObject) {
                    ((BasicBSONObject) val).put(aop, aval);
                    ob.put(field, (BasicBSONObject) val);
                    isok = true;
                    break;
                } else if (val instanceof BasicBSONList) {
                // newobj.put(field, ((BasicDBList)val).copy());
                }
            }
        }
    }
    if (isok == false) {
        BasicBSONObject xo = new BasicBSONObject();
        xo.put(aop, aval);
        ob.put(akey, xo);
    }
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BasicBSONList(org.bson.types.BasicBSONList) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject)

Example 4 with BasicBSONList

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

the class BasicDBObjectTest method testEqualsAndHashCode.

@Test
public void testEqualsAndHashCode() {
    assertEquality(new BasicDBObject(), new BasicDBObject());
    assertEquality(new BasicDBObject("x", 1), new BasicDBObject("x", 1));
    assertEquality(new BasicDBObject("x", 1), new BasicBSONObject("x", 1));
    assertInequality(new BasicDBObject("x", 1), new BasicDBObject("x", 2));
    assertInequality(new BasicDBObject("x", 1), new BasicBSONObject("x", 2));
    assertInequality(new BasicDBObject("x", 1), new BasicDBObject("y", 1));
    assertInequality(new BasicDBObject("x", 1), new BasicBSONObject("y", 1));
    assertEquality(new BasicDBObject("x", asList(1, 2, 3)), new BasicDBObject("x", new int[] { 1, 2, 3 }));
    assertEquality(new BasicDBObject("x", asList(1, 2, 3)), new BasicBSONObject("x", asList(1, 2, 3)));
    BasicDBList list = new BasicDBList();
    list.put(0, 1);
    list.put(1, 2);
    list.put(2, 3);
    assertEquality(new BasicDBObject("x", asList(1, 2, 3)), new BasicDBObject("x", list));
    assertEquality(new BasicDBObject("x", asList(1, 2, 3)), new BasicBSONObject("x", list));
    assertEquality(new BasicDBObject("x", 1).append("y", 2), new BasicDBObject("y", 2).append("x", 1));
    assertEquality(new BasicDBObject("x", 1).append("y", 2), new BasicBSONObject("y", 2).append("x", 1));
    assertEquality(new BasicDBObject("a", new BasicDBObject("y", 2).append("x", 1)), new BasicDBObject("a", new BasicDBObject("x", 1).append("y", 2)));
    assertEquality(new BasicDBObject("a", new BasicDBObject("y", 2).append("x", 1)), new BasicBSONObject("a", new BasicBSONObject("x", 1).append("y", 2)));
    assertEquality(new BasicDBObject("a", asList(new BasicDBObject("y", 2).append("x", 1))), new BasicDBObject("a", asList(new BasicDBObject("x", 1).append("y", 2))));
    assertEquality(new BasicDBObject("a", asList(new BasicDBObject("y", 2).append("x", 1))), new BasicBSONObject("a", asList(new BasicBSONObject("x", 1).append("y", 2))));
    assertEquality(new BasicDBObject("a", new BasicDBList().put(1, new BasicDBObject("y", 2).append("x", 1))), new BasicDBObject("a", new BasicDBList().put(1, new BasicDBObject("x", 1).append("y", 2))));
    assertEquality(new BasicDBObject("a", new BasicDBList().put(1, new BasicDBObject("y", 2).append("x", 1))), new BasicBSONObject("a", new BasicBSONList().put(1, new BasicBSONObject("x", 1).append("y", 2))));
    Map<String, Object> first = new HashMap<String, Object>();
    first.put("1", new BasicDBObject("y", 2).append("x", 1));
    first.put("2", new BasicDBObject("a", 2).append("b", 1));
    Map<String, Object> second = new TreeMap<String, Object>();
    second.put("2", new BasicDBObject("b", 1).append("a", 2));
    second.put("1", new BasicDBObject("x", 1).append("y", 2));
    Map<String, Object> third = new TreeMap<String, Object>();
    third.put("2", new BasicBSONObject("a", 2).append("b", 1));
    third.put("1", new BasicBSONObject("x", 1).append("y", 2));
    assertEquality(new BasicDBObject("a", first), new BasicDBObject("a", second));
    assertEquality(new BasicDBObject("a", first), new BasicBSONObject("a", third));
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) HashMap(java.util.HashMap) BasicBSONList(org.bson.types.BasicBSONList) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 5 with BasicBSONList

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

the class DefaultDBCallback method objectDone.

@Override
public Object objectDone() {
    String name = curName();
    BSONObject document = (BSONObject) super.objectDone();
    if (!(document instanceof BasicBSONList)) {
        Iterator<String> iterator = document.keySet().iterator();
        if (iterator.hasNext() && iterator.next().equals("$ref") && iterator.hasNext() && iterator.next().equals("$id")) {
            _put(name, new DBRef((String) document.get("$db"), (String) document.get("$ref"), document.get("$id")));
        }
    }
    return document;
}
Also used : BasicBSONList(org.bson.types.BasicBSONList) BSONObject(org.bson.BSONObject)

Aggregations

BasicBSONList (org.bson.types.BasicBSONList)11 BSONObject (org.bson.BSONObject)8 BasicBSONObject (org.bson.BasicBSONObject)6 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 ArrayList (java.util.ArrayList)1