Search in sources :

Example 66 with BasicBSONObject

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

the class BSONSerDe method serializeStruct.

/**
     * Turn a Hive struct into a BasicBSONObject.
     * @param obj the Hive struct
     * @param structOI an {@code ObjectInspector} for the struct
     * @param ext the field name
     * @return a BasicBSONObject representing the Hive struct
     */
private Object serializeStruct(final Object obj, final StructObjectInspector structOI, final String ext) {
    if (ext.length() > 0 && isObjectIdStruct(obj, structOI)) {
        String objectIdString = "";
        for (StructField s : structOI.getAllStructFieldRefs()) {
            if (s.getFieldName().equals(OID)) {
                objectIdString = structOI.getStructFieldData(obj, s).toString();
                break;
            }
        }
        return new ObjectId(objectIdString);
    } else {
        BasicBSONObject bsonObject = new BasicBSONObject();
        // fields is the list of all variable names and information within the struct obj
        List<? extends StructField> fields = structOI.getAllStructFieldRefs();
        for (int i = 0; i < fields.size(); i++) {
            StructField field = fields.get(i);
            String fieldName, hiveMapping;
            // get corresponding mongoDB field  
            if (ext.length() == 0) {
                fieldName = columnNames.get(i);
                hiveMapping = fieldName;
            } else {
                fieldName = field.getFieldName();
                hiveMapping = ext + "." + fieldName;
            }
            ObjectInspector fieldOI = field.getFieldObjectInspector();
            Object fieldObj = structOI.getStructFieldData(obj, field);
            if (hiveToMongo != null && hiveToMongo.containsKey(hiveMapping)) {
                String mongoMapping = hiveToMongo.get(hiveMapping);
                int lastDotPos = mongoMapping.lastIndexOf(".");
                String lastMapping = lastDotPos == -1 ? mongoMapping : mongoMapping.substring(lastDotPos + 1);
                bsonObject.put(lastMapping, serializeObject(fieldObj, fieldOI, hiveMapping));
            } else {
                bsonObject.put(fieldName, serializeObject(fieldObj, fieldOI, hiveMapping));
            }
        }
        return bsonObject;
    }
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) StructField(org.apache.hadoop.hive.serde2.objectinspector.StructField) ObjectId(org.bson.types.ObjectId) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject)

Example 67 with BasicBSONObject

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

the class TreasuryYieldUpdateReducer method reduce.

@Override
public void reduce(final IntWritable pKey, final Iterable<DoubleWritable> pValues, final Context pContext) throws IOException, InterruptedException {
    int count = 0;
    double sum = 0;
    for (final DoubleWritable value : pValues) {
        sum += value.get();
        count++;
    }
    final double avg = sum / count;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Average 10 Year Treasury for " + pKey.get() + " was " + avg);
    }
    BasicBSONObject query = new BasicBSONObject("_id", pKey.get());
    BasicBSONObject modifiers = new BasicBSONObject();
    modifiers.put("$set", BasicDBObjectBuilder.start().add("count", count).add("avg", avg).add("sum", sum).get());
    modifiers.put("$push", new BasicBSONObject("calculatedAt", new Date()));
    modifiers.put("$inc", new BasicBSONObject("numCalculations", 1));
    reduceResult.setQuery(query);
    reduceResult.setModifiers(modifiers);
    pContext.write(null, reduceResult);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) DoubleWritable(org.apache.hadoop.io.DoubleWritable) Date(java.util.Date)

Example 68 with BasicBSONObject

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

the class LogReducer method reduce.

@Override
public void reduce(final Text key, final Iterator<IntWritable> values, final OutputCollector<NullWritable, MongoUpdateWritable> output, final Reporter reporter) throws IOException {
    int count = 0;
    while (values.hasNext()) {
        count += values.next().get();
    }
    BasicBSONObject query = new BasicBSONObject("devices", new ObjectId(key.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);
    output.collect(null, reduceResult);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) ObjectId(org.bson.types.ObjectId)

Example 69 with BasicBSONObject

use of org.bson.BasicBSONObject in project mongo-spark by plaa.

the class JavaWordCount method main.

public static void main(String[] args) {
    JavaSparkContext sc = new JavaSparkContext("local", "Java Word Count");
    Configuration config = new Configuration();
    config.set("mongo.input.uri", "mongodb://127.0.0.1:27017/beowulf.input");
    config.set("mongo.output.uri", "mongodb://127.0.0.1:27017/beowulf.output");
    JavaPairRDD<Object, BSONObject> mongoRDD = sc.newAPIHadoopRDD(config, com.mongodb.hadoop.MongoInputFormat.class, Object.class, BSONObject.class);
    // Input contains tuples of (ObjectId, BSONObject)
    JavaRDD<String> words = mongoRDD.flatMap(new FlatMapFunction<Tuple2<Object, BSONObject>, String>() {

        @Override
        public Iterable<String> call(Tuple2<Object, BSONObject> arg) {
            Object o = arg._2.get("text");
            if (o instanceof String) {
                String str = (String) o;
                str = str.toLowerCase().replaceAll("[.,!?\n]", " ");
                return Arrays.asList(str.split(" "));
            } else {
                return Collections.emptyList();
            }
        }
    });
    JavaPairRDD<String, Integer> ones = words.map(new PairFunction<String, String, Integer>() {

        public Tuple2<String, Integer> call(String s) {
            return new Tuple2<>(s, 1);
        }
    });
    JavaPairRDD<String, Integer> counts = ones.reduceByKey(new Function2<Integer, Integer, Integer>() {

        public Integer call(Integer i1, Integer i2) {
            return i1 + i2;
        }
    });
    // Output contains tuples of (null, BSONObject) - ObjectId will be generated by Mongo driver if null
    JavaPairRDD<Object, BSONObject> save = counts.map(new PairFunction<Tuple2<String, Integer>, Object, BSONObject>() {

        @Override
        public Tuple2<Object, BSONObject> call(Tuple2<String, Integer> tuple) {
            BSONObject bson = new BasicBSONObject();
            bson.put("word", tuple._1);
            bson.put("count", tuple._2);
            return new Tuple2<>(null, bson);
        }
    });
    // Only MongoOutputFormat and config are relevant
    save.saveAsNewAPIHadoopFile("file:///bogus", Object.class, Object.class, MongoOutputFormat.class, config);
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BasicBSONObject(org.bson.BasicBSONObject) Tuple2(scala.Tuple2) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext)

Example 70 with BasicBSONObject

use of org.bson.BasicBSONObject in project jackrabbit-oak by apache.

the class ReplicaSetInfo method getHiddenMembers.

List<String> getHiddenMembers() {
    BasicDBObject result;
    try {
        result = getReplicaConfig();
    } catch (MongoException e) {
        LOG.error("Can't get replica configuration", e);
        return null;
    }
    @SuppressWarnings("unchecked") Iterable<BasicBSONObject> members = (Iterable<BasicBSONObject>) result.get("members");
    if (members == null) {
        members = Collections.emptyList();
    }
    List<String> hiddenMembers = new ArrayList<String>();
    for (BasicBSONObject member : members) {
        if (member.getBoolean("hidden")) {
            hiddenMembers.add(member.getString("host"));
        }
    }
    return hiddenMembers;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicBSONObject(org.bson.BasicBSONObject) MongoException(com.mongodb.MongoException) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

BasicBSONObject (org.bson.BasicBSONObject)71 Test (org.junit.Test)23 BSONObject (org.bson.BSONObject)20 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)14 BSONWritable (com.mongodb.hadoop.io.BSONWritable)13 ArrayList (java.util.ArrayList)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)7 DataBag (org.apache.pig.data.DataBag)6 Tuple (org.apache.pig.data.Tuple)5 Mongo (com.mongodb.Mongo)4 Date (java.util.Date)4 Map (java.util.Map)4 DoubleWritable (org.apache.hadoop.io.DoubleWritable)4 Text (org.apache.hadoop.io.Text)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3