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);
}
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);
}
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;
}
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;
}
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;
}
Aggregations