use of org.bson.types.ObjectId in project mongo-hadoop by mongodb.
the class DeviceReducer method reduce.
@Override
public void reduce(final Text pKey, final Iterable<Text> pValues, final Context pContext) throws IOException, InterruptedException {
BasicBSONObject query = new BasicBSONObject("_id", pKey.toString());
ArrayList<ObjectId> devices = new ArrayList<ObjectId>();
for (Text val : pValues) {
devices.add(new ObjectId(val.toString()));
}
BasicBSONObject update = new BasicBSONObject("$pushAll", new BasicBSONObject("devices", devices));
reduceResult.setQuery(query);
reduceResult.setModifiers(update);
pContext.write(null, reduceResult);
}
use of org.bson.types.ObjectId in project mongo-hadoop by mongodb.
the class LogReducer method reduce.
@Override
public void reduce(final Text pKey, final Iterable<IntWritable> pValues, final Context pContext) throws IOException, InterruptedException {
int count = 0;
for (IntWritable val : pValues) {
count += val.get();
}
BasicBSONObject query = new BasicBSONObject("devices", new ObjectId(pKey.toString()));
BasicBSONObject update = new BasicBSONObject("$inc", new BasicBSONObject("logs_count", count));
if (LOG.isDebugEnabled()) {
LOG.debug("query: " + query);
LOG.debug("update: " + update);
}
reduceResult.setQuery(query);
reduceResult.setModifiers(update);
pContext.write(null, reduceResult);
}
use of org.bson.types.ObjectId 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.ObjectId in project mongo-hadoop by mongodb.
the class UDFTest method setUpClass.
@BeforeClass
public static void setUpClass() {
INPUT_COLLECTION.drop();
insertedDocuments = new ArrayList<Document>(100);
for (int i = 0; i < 100; ++i) {
ObjectId id = new ObjectId();
insertedDocuments.add(new Document("_id", id).append("minkey", new MinKey()).append("maxkey", new MaxKey()).append("dbref", new DBRef("othercollection", new ObjectId())).append("binary", new Binary(new byte[] { 1, 2, 3, 4, 5 })).append("oidBytes", new Binary(id.toByteArray())));
}
INPUT_COLLECTION.insertMany(insertedDocuments);
}
use of org.bson.types.ObjectId in project mongo-hadoop by mongodb.
the class UDFTest method testUDFsSchemaless.
@Test
public void testUDFsSchemaless() throws IOException, ParseException {
// Test that one of our UDFs can work without any schemas being
// specified. This mostly tests that BSONStorage can infer the type
// correctly.
PigTest.runScript("/pig/udfschemaless.pig");
assertEquals(insertedDocuments.size(), OUTPUT_COLLECTION.count());
Iterator<Document> it = insertedDocuments.iterator();
for (Document doc : OUTPUT_COLLECTION.find()) {
// We don't know what Pig will call the fields that aren't "_id".
ObjectId expectedId = it.next().getObjectId("_id");
for (Map.Entry<String, Object> entry : doc.entrySet()) {
// interested in.
if ("_id".equals(entry.getKey())) {
continue;
}
assertEquals(expectedId, entry.getValue());
}
}
}
Aggregations