Search in sources :

Example 16 with BSONObject

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

the class MongoInputSplit method write.

@Override
public void write(final DataOutput out) throws IOException {
    BSONObject spec = BasicDBObjectBuilder.start().add("inputURI", getInputURI().toString()).add("authURI", getAuthURI() != null ? getAuthURI().toString() : null).add("keyField", getKeyField()).add("fields", getFields()).add("query", getQuery()).add("sort", getSort()).add("min", getMin()).add("max", getMax()).add("notimeout", getNoTimeout()).add("limit", limit).add("skip", skip).get();
    byte[] buf = _bsonEncoder.encode(spec);
    out.write(buf);
}
Also used : BSONObject(org.bson.BSONObject)

Example 17 with BSONObject

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

the class BSONSerDe method getValue.

private Object getValue(final BSONObject doc, final String mongoMapping) {
    if (mongoMapping.contains(".")) {
        int index = mongoMapping.indexOf('.');
        BSONObject object = (BSONObject) doc.get(mongoMapping.substring(0, index));
        return getValue(object, mongoMapping.substring(index + 1));
    }
    return doc.get(mongoMapping);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject)

Example 18 with BSONObject

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

the class BSONSerDe method deserialize.

/**
     * Given a Writable object of BSON, turn it into a Hive table row
     */
@Override
public //CHECKSTYLE:OFF
Object deserialize(final Writable writable) throws SerDeException {
    //CHECKSTYLE:ON
    BSONObject doc;
    row.clear();
    // Make sure it's a BSONWritable object
    if (writable instanceof BSONWritable) {
        doc = ((BSONWritable) writable).getDoc();
    } else {
        throw new SerDeException(format("%srequires a BSONWritable object, not%s", getClass(), writable.getClass()));
    }
    // For each field, cast it to a HIVE type and add to the current row
    Object value;
    List<String> structFieldNames = docTypeInfo.getAllStructFieldNames();
    for (String fieldName : structFieldNames) {
        try {
            TypeInfo fieldTypeInfo = docTypeInfo.getStructFieldTypeInfo(fieldName);
            // get the corresponding field name in MongoDB
            String mongoMapping;
            if (hiveToMongo == null) {
                mongoMapping = fieldName;
            } else {
                mongoMapping = hiveToMongo.containsKey(fieldName) ? hiveToMongo.get(fieldName) : fieldName;
            }
            value = deserializeField(getValue(doc, mongoMapping), fieldTypeInfo, fieldName);
        } catch (Exception e) {
            LOG.warn("Could not find the appropriate field for name " + fieldName);
            value = null;
        }
        row.add(value);
    }
    return row;
}
Also used : BSONWritable(com.mongodb.hadoop.io.BSONWritable) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) SerDeException(org.apache.hadoop.hive.serde2.SerDeException) SerDeException(org.apache.hadoop.hive.serde2.SerDeException)

Example 19 with BSONObject

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

the class BSONSerDe method serializeMap.

/**
     * Serialize a Hive Map into a BSONObject.
     * @param obj the Hive Map.
     * @param mapOI an {@code ObjectInspector} for the Hive Map.
     * @param ext the field name
     * @return a BSONObject representing the Hive Map
     */
private Object serializeMap(final Object obj, final MapObjectInspector mapOI, final String ext) {
    BasicBSONObject bsonObject = new BasicBSONObject();
    ObjectInspector mapValOI = mapOI.getMapValueObjectInspector();
    // Each value is guaranteed to be of the same type
    for (Entry<?, ?> entry : mapOI.getMap(obj).entrySet()) {
        String field = entry.getKey().toString();
        Object value = serializeObject(entry.getValue(), mapValOI, ext);
        bsonObject.put(field, value);
    }
    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) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject)

Example 20 with BSONObject

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

the class HiveMongoInputFormat method columnMapping.

private Map<String, String> columnMapping(final JobConf conf) {
    String colMapString = conf.get(BSONSerDe.MONGO_COLS);
    if (null == colMapString) {
        return null;
    }
    BSONObject mappingBSON = (BSONObject) JSON.parse(colMapString);
    Map<String, String> mapping = new HashMap<String, String>();
    for (String key : mappingBSON.keySet()) {
        mapping.put(key.toLowerCase(), (String) mappingBSON.get(key));
    }
    return mapping;
}
Also used : HashMap(java.util.HashMap) BSONObject(org.bson.BSONObject)

Aggregations

BSONObject (org.bson.BSONObject)66 BasicBSONObject (org.bson.BasicBSONObject)31 BasicDBObject (com.mongodb.BasicDBObject)19 DBObject (com.mongodb.DBObject)10 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)6 LazyBSONObject (org.bson.LazyBSONObject)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 BSONFileSplit (com.mongodb.hadoop.input.BSONFileSplit)4 BSONWritable (com.mongodb.hadoop.io.BSONWritable)4 DBCollection (com.sequoiadb.base.DBCollection)4 Map (java.util.Map)4 ObjectId (org.bson.types.ObjectId)4 DBRef (com.mongodb.DBRef)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Configuration (org.apache.hadoop.conf.Configuration)3 Tuple (org.apache.pig.data.Tuple)3