Permalink
Browse files

Merge pull request #63 from mortardata/store_map

Fix bug where maps were having only one field stored.
  • Loading branch information...
2 parents 690a7e3 + 016ec1d commit 99f15753099f7997808aa0dc6b02642512154ffd @mpobrien mpobrien committed Dec 13, 2012
View
8 pig/src/main/java/com/mongodb/hadoop/pig/MongoStorage.java
@@ -94,7 +94,7 @@ public void putNext( Tuple tuple ) throws IOException{
_recordWriter.write( null, builder.get() );
}
- private void writeField(BasicDBObjectBuilder builder,
+ protected void writeField(BasicDBObjectBuilder builder,
ResourceSchema.ResourceFieldSchema field,
Object d) throws IOException {
@@ -179,6 +179,12 @@ private void writeField(BasicDBObjectBuilder builder,
builder.add( field.getName(), a);
return;
+ case DataType.MAP:
+ Map map = (Map) d;
+ for(Object key : map.keySet()) {
+ builder.add(key.toString(), map.get(key));
+ }
+ return;
}
}
View
41 pig/src/test/java/com/mongodb/hadoop/pig/MongoStorageTest.java
@@ -0,0 +1,41 @@
+package com.mongodb.hadoop.pig;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.pig.ResourceSchema;
+import org.apache.pig.impl.util.Utils;
+import org.junit.Test;
+
+import com.mongodb.BasicDBObjectBuilder;
+import com.mongodb.DBObject;
+
+@SuppressWarnings( {"rawtypes", "unchecked"} )
+public class MongoStorageTest {
+
+ @Test
+ public void testWriteField_map() throws Exception {
+ MongoStorage ms = new MongoStorage();
+ BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
+ ResourceSchema schema = new ResourceSchema(Utils.getSchemaFromString("m:map[]"));
+
+ Map val = new HashMap();
+ val.put("f1", 1);
+ val.put("f2", "2");
+
+ ms.writeField(builder, schema.getFields()[0], val);
+
+ DBObject out = builder.get();
+
+ Set<String> outKeySet = out.keySet();
+
+ assertEquals(2, outKeySet.size());
+ assertEquals(1, out.get("f1"));
+ assertEquals("2", out.get("f2"));
+ }
+
+
+}

0 comments on commit 99f1575

Please sign in to comment.