Search in sources :

Example 21 with BasicBSONObject

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);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) ObjectId(org.bson.types.ObjectId) ArrayList(java.util.ArrayList) Text(org.apache.hadoop.io.Text)

Example 22 with BasicBSONObject

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);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) ObjectId(org.bson.types.ObjectId) ArrayList(java.util.ArrayList) Text(org.apache.hadoop.io.Text)

Example 23 with BasicBSONObject

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);
            }
        }
    }
}
Also used : IOException(java.io.IOException) BasicBSONObject(org.bson.BasicBSONObject) ResourceFieldSchema(org.apache.pig.ResourceSchema.ResourceFieldSchema) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 24 with BasicBSONObject

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;
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject)

Example 25 with BasicBSONObject

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);
    }
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) IOException(java.io.IOException)

Aggregations

BasicBSONObject (org.bson.BasicBSONObject)88 Test (org.junit.Test)39 BSONObject (org.bson.BSONObject)37 ArrayList (java.util.ArrayList)15 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)14 BSONWritable (com.mongodb.hadoop.io.BSONWritable)13 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)13 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)13 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)13 BasicDBObject (com.mongodb.BasicDBObject)11 ObjectId (org.bson.types.ObjectId)11 IOException (java.io.IOException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 DataBag (org.apache.pig.data.DataBag)6 Map (java.util.Map)5 Tuple (org.apache.pig.data.Tuple)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Mongo (com.mongodb.Mongo)4 Date (java.util.Date)4 DoubleWritable (org.apache.hadoop.io.DoubleWritable)4