use of org.bson.BasicBSONObject 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.BasicBSONObject in project mongo-hadoop by mongodb.
the class DeviceReducer method reduce.
@Override
public void reduce(final Text key, final Iterator<Text> values, final OutputCollector<NullWritable, MongoUpdateWritable> output, final Reporter reporter) throws IOException {
BasicBSONObject query = new BasicBSONObject("_id", key.toString());
ArrayList<ObjectId> devices = new ArrayList<ObjectId>();
while (values.hasNext()) {
Text val = values.next();
devices.add(new ObjectId(val.toString()));
}
BasicBSONObject update = new BasicBSONObject("$pushAll", new BasicBSONObject("devices", devices));
reduceResult.setQuery(query);
reduceResult.setModifiers(update);
output.collect(null, reduceResult);
}
use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class MongoLoader method prepareToRead.
@Override
public void prepareToRead(final RecordReader reader, final PigSplit split) throws IOException {
in = reader;
if (in == null) {
throw new IOException("Invalid Record Reader");
}
BasicBSONObject projection = getProjection();
if (fields != null && projection != null) {
schemaMapping = new HashMap<String, ResourceFieldSchema>(fields.length);
projectedFields = new ArrayList<String>();
Set<String> visitedKeys = new HashSet<String>();
// Prepare mapping of field name -> ResourceFieldSchema.
for (ResourceFieldSchema fieldSchema : fields) {
schemaMapping.put(fieldSchema.getName(), fieldSchema);
}
// Prepare list of projected fields.
for (Map.Entry<String, Object> entry : projection.entrySet()) {
boolean include = (Boolean) entry.getValue();
// Add the name of the outer-level field if this is a nested
// field. Pig will take care of pulling out the inner field.
String key = StringUtils.split(entry.getKey(), '\\', '.')[0];
if (include && !visitedKeys.contains(key)) {
projectedFields.add(key);
visitedKeys.add(key);
}
}
}
}
use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class MongoUpdateOutputReader method readKeyValue.
@Override
public boolean readKeyValue() throws IOException {
valueWritable.readFields(input);
Object id = valueWritable.getDoc().get("_id");
if (null == id) {
return false;
}
keyWritable.setDoc(new BasicBSONObject("_id", id));
return true;
}
use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class MongoUpdateOutputReader method initializeOutputWritable.
private void initializeOutputWritable(final BasicBSONObject value) throws IOException {
if (!(value.containsField("modifiers") && value.containsField("_id"))) {
outputWritable.setQuery(value);
return;
}
Object query = value.get("_id");
if (query instanceof BasicBSONObject) {
outputWritable.setQuery((BasicBSONObject) query);
} else {
throw new IOException("_id must be a document describing the query of the update, not " + query);
}
Object modifiers = value.get("modifiers");
if (modifiers instanceof BasicBSONObject) {
outputWritable.setModifiers((BasicBSONObject) modifiers);
} else {
throw new IOException("modifiers must be a replacement or update document, not" + modifiers);
}
Object options = value.get("options");
if (options instanceof BSONObject) {
BSONObject updateOptions = (BSONObject) value.get("options");
outputWritable.setUpsert(getBoolean(updateOptions, "upsert", true));
outputWritable.setMultiUpdate(getBoolean(updateOptions, "multi", false));
outputWritable.setReplace(getBoolean(updateOptions, "replace", false));
} else if (options != null) {
throw new IOException("options must either be null or a document providing update " + "options, not " + options);
}
}
Aggregations