use of org.bson.BSONObject in project mongo-hadoop by mongodb.
the class BSONLoader method readField.
/**
* Convert an object from a MongoDB document into a type that Pig can
* understand, based on the expectations of the given schema.
* @param obj object from a MongoDB document
* @param field the schema describing this field
* @return an object appropriate for Pig
* @throws IOException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected static Object readField(final Object obj, final ResourceFieldSchema field) throws IOException {
if (obj == null) {
return null;
}
try {
if (field == null) {
return obj;
}
switch(field.getType()) {
case DataType.INTEGER:
return Integer.parseInt(obj.toString());
case DataType.LONG:
return Long.parseLong(obj.toString());
case DataType.FLOAT:
return Float.parseFloat(obj.toString());
case DataType.DOUBLE:
return Double.parseDouble(obj.toString());
case DataType.BYTEARRAY:
return BSONLoader.convertBSONtoPigType(obj);
case DataType.CHARARRAY:
return obj.toString();
case DataType.DATETIME:
return new DateTime(obj);
case DataType.TUPLE:
ResourceSchema s = field.getSchema();
ResourceFieldSchema[] fs = s.getFields();
Tuple t = tupleFactory.newTuple(fs.length);
BasicDBObject val = (BasicDBObject) obj;
for (int j = 0; j < fs.length; j++) {
t.set(j, readField(val.get(fs[j].getName()), fs[j]));
}
return t;
case DataType.BAG:
s = field.getSchema();
fs = s.getFields();
s = fs[0].getSchema();
fs = s.getFields();
DataBag bag = bagFactory.newDefaultBag();
BasicDBList vals = (BasicDBList) obj;
for (Object val1 : vals) {
t = tupleFactory.newTuple(fs.length);
for (int k = 0; k < fs.length; k++) {
t.set(k, readField(((BasicDBObject) val1).get(fs[k].getName()), fs[k]));
}
bag.add(t);
}
return bag;
case DataType.MAP:
s = field.getSchema();
fs = s != null ? s.getFields() : null;
Map outputMap = new HashMap();
if (obj instanceof BSONObject) {
BasicBSONObject inputMap = (BasicBSONObject) obj;
for (String key : inputMap.keySet()) {
if (fs != null) {
outputMap.put(key, readField(inputMap.get(key), fs[0]));
} else {
outputMap.put(key, readField(inputMap.get(key), null));
}
}
} else if (obj instanceof DBRef) {
DBRef ref = (DBRef) obj;
outputMap.put("$ref", ref.getCollectionName());
outputMap.put("$id", ref.getId().toString());
}
return outputMap;
default:
LOG.info("asfkjabskfjbsaf default for " + field.getName());
return BSONLoader.convertBSONtoPigType(obj);
}
} catch (Exception e) {
String fieldName = field.getName() == null ? "" : field.getName();
String type = DataType.genTypeToNameMap().get(field.getType());
LOG.warn("Type " + type + " for field " + fieldName + " can not be applied to " + obj.getClass().toString());
return null;
}
}
use of org.bson.BSONObject in project mongo-hadoop by mongodb.
the class MongoLoader method getNext.
@Override
public Tuple getNext() throws IOException {
BSONObject val;
try {
if (!in.nextKeyValue()) {
return null;
}
val = (BSONObject) in.getCurrentValue();
} catch (Exception ie) {
throw new IOException(ie);
}
Tuple t;
if (fields == null) {
// dynamic schema mode - just output a tuple with a single element,
// which is a map storing the keys/values in the document
// Since there is no schema, no projection can be made, and
// there's no need to worry about retrieving projected fields.
t = tupleFactory.newTuple(1);
t.set(0, BSONLoader.convertBSONtoPigType(val));
} else {
// A schema was provided. Try to retrieve the projection.
int tupleSize;
if (projectedFields != null) {
tupleSize = projectedFields.size();
} else {
tupleSize = fields.length;
}
t = tupleFactory.newTuple(tupleSize);
for (int i = 0; i < t.size(); i++) {
String fieldTemp;
ResourceFieldSchema fieldSchema;
if (null == projectedFields) {
fieldTemp = fields[i].getName();
fieldSchema = fields[i];
if (idAlias != null && idAlias.equals(fieldTemp)) {
fieldTemp = "_id";
}
} else {
fieldTemp = projectedFields.get(i);
// Use id alias in order to retrieve type info.
if (idAlias != null && "_id".equals(fieldTemp)) {
fieldSchema = schemaMapping.get(idAlias);
} else {
fieldSchema = schemaMapping.get(fieldTemp);
}
}
t.set(i, BSONLoader.readField(val.get(fieldTemp), fieldSchema));
}
}
return t;
}
use of org.bson.BSONObject 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);
}
}
use of org.bson.BSONObject in project mongo-java-driver by mongodb.
the class DBObjectCodec method writeValue.
@SuppressWarnings("unchecked")
private void writeValue(final BsonWriter bsonWriter, final EncoderContext encoderContext, final Object initialValue) {
Object value = BSON.applyEncodingHooks(initialValue);
if (value == null) {
bsonWriter.writeNull();
} else if (value instanceof DBRef) {
encodeDBRef(bsonWriter, (DBRef) value);
} else if (value instanceof Map) {
encodeMap(bsonWriter, (Map<String, Object>) value);
} else if (value instanceof Iterable) {
encodeIterable(bsonWriter, (Iterable) value);
} else if (value instanceof BSONObject) {
encodeBsonObject(bsonWriter, ((BSONObject) value));
} else if (value instanceof CodeWScope) {
encodeCodeWScope(bsonWriter, (CodeWScope) value);
} else if (value instanceof byte[]) {
encodeByteArray(bsonWriter, (byte[]) value);
} else if (value.getClass().isArray()) {
encodeArray(bsonWriter, value);
} else if (value instanceof Symbol) {
bsonWriter.writeSymbol(((Symbol) value).getSymbol());
} else {
Codec codec = codecRegistry.get(value.getClass());
codec.encode(bsonWriter, value, encoderContext);
}
}
use of org.bson.BSONObject in project mongo-java-driver by mongodb.
the class DefaultDBCallback method objectDone.
@Override
public Object objectDone() {
String name = curName();
BSONObject document = (BSONObject) super.objectDone();
if (!(document instanceof BasicBSONList)) {
Iterator<String> iterator = document.keySet().iterator();
if (iterator.hasNext() && iterator.next().equals("$ref") && iterator.hasNext() && iterator.next().equals("$id")) {
_put(name, new DBRef((String) document.get("$db"), (String) document.get("$ref"), document.get("$id")));
}
}
return document;
}
Aggregations