use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class JSONPigReplace method substitute.
/*
* Returns result of substituting pig objects in Tuple t into
* initStr
*
* @param Tuple t : Pig tuple containing pig objects
* @param Object s : Schema representing Tuple t
* @param String un : String to represent un-named Schema Fields
*
* @return Array of BasicBSONObjects that contain all replacements for "marked" strings
*/
public BasicBSONObject[] substitute(final Tuple t, final Object s, final String un) throws Exception {
unnamedStr = un;
final ResourceFieldSchema[] fields;
try {
final ResourceSchema schema;
if (s instanceof String) {
schema = new ResourceSchema(Utils.getSchemaFromString((String) s));
} else if (s instanceof Schema) {
schema = new ResourceSchema((Schema) s);
} else if (s instanceof ResourceSchema) {
schema = (ResourceSchema) s;
} else {
throw new IllegalArgumentException("Schema must be represented either by a string or a Schema " + "object, not " + s);
}
fields = schema.getFields();
} catch (Exception e) {
throw new IllegalArgumentException("Invalid Schema Format", e);
}
// Make Tuple t into BSONObject using schema provided and store result in pObj
final BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
for (int i = 0; i < fields.length; i++) {
writeField(builder, fields[i], t.get(i));
}
// BSONObject that represents Pig Tuple input using Pig Schema
BasicBSONObject pObj = (BasicBSONObject) builder.get();
// fill map of replacement strings to corresponding objects to replace these strings with
fillReplacementMap(pObj);
// Now, replace replacement strings (of form $elem) with corresponding objects in pObj
return replaceAll(initBSONs, reps);
}
use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class MongoLoader method pushProjection.
@Override
public RequiredFieldResponse pushProjection(final RequiredFieldList requiredFieldList) throws FrontendException {
// this.
if (null == schema) {
return new RequiredFieldResponse(false);
}
BSONObject projection = new BasicBSONObject();
boolean needId = false;
for (RequiredField field : requiredFieldList.getFields()) {
String fieldName = field.getAlias();
if (idAlias != null && idAlias.equals(fieldName)) {
fieldName = "_id";
needId = true;
}
List<RequiredField> subFields = field.getSubFields();
if (subFields != null && !subFields.isEmpty()) {
// Pig is limited to populating at most one subfield level deep.
for (RequiredField subField : subFields) {
projection.put(fieldName + "." + subField.getAlias(), true);
}
} else {
projection.put(fieldName, true);
}
}
// Turn off _id unless asked for.
if (!needId) {
projection.put("_id", false);
}
LOG.debug("projection: " + projection);
// Store projection to be retrieved later and stored into the job
// configuration.
getUDFProperties().setProperty(MongoConfigUtil.INPUT_FIELDS, JSON.serialize(projection));
// Return a response indicating that we can honor the projection.
return new RequiredFieldResponse(true);
}
use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class BSONSerDeTest method testBinary.
@Test
public void testBinary() throws SerDeException {
String columnNames = "b";
String columnTypes = "binary";
byte[] value = new byte[2];
value[0] = 'A';
value[1] = '1';
BSONSerDe serde = new BSONSerDe();
Object result = helpDeserialize(serde, columnNames, columnTypes, value);
assertThat(value, equalTo(result));
ObjectInspector innerInspector = PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(byte[].class);
BasicBSONObject bObject = new BasicBSONObject();
Object serialized = helpSerialize(columnNames, innerInspector, bObject, value, serde);
assertThat(new BSONWritable(bObject), equalTo(serialized));
}
use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class BSONSerDeTest method testObjectID.
@Test
public void testObjectID() throws SerDeException {
String columnNames = "o";
String columnTypes = "struct<oid:string,bsontype:int>";
ObjectId value = new ObjectId();
ArrayList<Object> returned = new ArrayList<Object>(2);
returned.add(value.toString());
returned.add(8);
BSONSerDe serde = new BSONSerDe();
Object result = helpDeserialize(serde, columnNames, columnTypes, value);
assertThat(returned, equalTo(result));
// Since objectid is currently taken to be a string
ObjectInspector innerInspector = PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(String.class);
BasicBSONObject bObject = new BasicBSONObject();
Object serialized = helpSerialize(columnNames, innerInspector, bObject, value.toString(), serde);
assertThat(new BSONWritable(bObject), equalTo(serialized));
}
use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class BSONSerDeTest method testList.
@Test
public void testList() throws SerDeException {
String columnNames = "a";
String columnTypes = "array<string>";
String inner = "inside";
ArrayList<String> value = new ArrayList<String>();
value.add(inner);
BasicBSONList b = new BasicBSONList();
b.add(inner);
BSONSerDe serde = new BSONSerDe();
Object result = helpDeserialize(serde, columnNames, columnTypes, b);
assertThat(value.toArray(), equalTo(result));
// Since objectid is currently taken to be a string
ObjectInspector innerInspector = PrimitiveObjectInspectorFactory.getPrimitiveObjectInspectorFromClass(String.class);
ListObjectInspector listInspector = ObjectInspectorFactory.getStandardListObjectInspector(innerInspector);
BasicBSONObject bObject = new BasicBSONObject();
Object serialized = helpSerialize(columnNames, listInspector, bObject, value, serde);
assertThat(new BSONWritable(bObject), equalTo(serialized));
}
Aggregations