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