use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class JSONPigReplace method fillReplacementMap.
/*
* Fills map of replacement strings (reps) with entries that
* map replacement strings to corresponding objects to replace these strings with
*
* @param Object pObj : Object representing pig tuple
*/
private void fillReplacementMap(final Object pObj) throws IOException {
if (pObj instanceof BasicBSONObject || pObj instanceof Map) {
@SuppressWarnings("unchecked") Map<String, Object> p = (Map<String, Object>) pObj;
Object val;
for (String k : p.keySet()) {
val = p.get(k);
if (reps.containsKey(k)) {
reps.put(k, val);
} else // check if 'val' is an array or an embedded BSON document
if (val instanceof BasicBSONObject || val instanceof ArrayList) {
fillReplacementMap(val);
}
}
} else if (pObj instanceof ArrayList) {
for (Object o : (ArrayList) pObj) {
fillReplacementMap(o);
}
}
}
use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class JSONPigReplace method replaceAll.
/*
* static method to
* use reps (map of strings to replace to object -> corresponding replacements)
* to make replacements in the BSONObject to act on
*
* @param BasicBSONObject in : BSONObject to make replacements in
* @param HashMap<String, Object> reps : replacement map
*
* @return BasicBSONObject : result of replacing "marked" strings specified in reps
*/
public static BasicBSONObject replaceAll(final BasicBSONObject in, final Map<String, Object> reps) {
if (in == null) {
throw new IllegalArgumentException("JSON/BasicBSONObject to make substitutions in cannot be null!");
}
BasicBSONObject res = new BasicBSONObject();
String k;
Object v;
for (Entry<String, Object> e : in.entrySet()) {
k = e.getKey();
v = e.getValue();
// v is a nested BasicBSONObject or an array
if (v instanceof BasicBSONObject) {
res.put(k, replaceAll((BasicBSONObject) v, reps));
} else {
if (v instanceof String && ((String) v).startsWith("$")) {
res.put(k, reps.get(((String) v).substring(1)));
} else {
res.put(k, v);
}
}
}
return res;
}
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 BSONWritableTest method testToBSON.
@Test
public void testToBSON() {
assertEquals(null, toBSON(null));
assertEquals(null, toBSON(NullWritable.get()));
assertEquals("hello", toBSON(new Text("hello")));
DBObject obj = new BasicDBObject("hello", "world");
assertEquals(obj, toBSON(new BSONWritable(obj)));
final BasicBSONObject bsonResult = new BasicBSONObject("one", 1);
SortedMapWritable smw = new SortedMapWritable();
smw.put(new Text("one"), new IntWritable(1));
assertEquals(bsonResult, toBSON(smw));
MapWritable mw = new MapWritable();
mw.put(new Text("one"), new IntWritable(1));
assertEquals(bsonResult, toBSON(mw));
String[] expectedObjects = new String[] { "one", "two" };
Writable[] writableObjects = new Writable[] { new Text("one"), new Text("two") };
ArrayWritable aw = new ArrayWritable(Text.class, writableObjects);
Object[] actual = (Object[]) toBSON(aw);
assertTrue(Arrays.equals(expectedObjects, actual));
assertEquals(false, toBSON(new BooleanWritable(false)));
byte[] bytes = new byte[] { '0', '1', '2' };
assertEquals(bytes, toBSON(new BytesWritable(bytes)));
byte b = (byte) 'c';
assertEquals(b, toBSON(new ByteWritable(b)));
assertEquals(3.14159, toBSON(new DoubleWritable(3.14159)));
assertEquals(3.14159f, toBSON(new FloatWritable(3.14159f)));
assertEquals(42L, toBSON(new LongWritable(42L)));
assertEquals(42, toBSON(new IntWritable(42)));
// Catchall
assertEquals("hi", toBSON("hi"));
}
Aggregations