From 1ad0c5602a579d5e16525f9f56230fa6203bd9da Mon Sep 17 00:00:00 2001 From: inkyu Date: Tue, 8 Jul 2014 15:47:16 +0900 Subject: [PATCH] com.mongodb.hadoop.hive.BSONSerde.getValue: supporting quoted dots --- .../com/mongodb/hadoop/hive/BSONSerDe.java | 17 +++++++++-------- .../com/mongodb/hadoop/hive/BSONSerDeTest.java | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/hive/src/main/java/com/mongodb/hadoop/hive/BSONSerDe.java b/hive/src/main/java/com/mongodb/hadoop/hive/BSONSerDe.java index 59c901dd..9b250a5a 100644 --- a/hive/src/main/java/com/mongodb/hadoop/hive/BSONSerDe.java +++ b/hive/src/main/java/com/mongodb/hadoop/hive/BSONSerDe.java @@ -16,6 +16,7 @@ package com.mongodb.hadoop.hive; +import com.google.common.annotations.VisibleForTesting; import com.mongodb.hadoop.io.BSONWritable; import com.mongodb.util.JSON; import org.apache.commons.logging.Log; @@ -55,6 +56,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Properties; +import java.util.regex.Pattern; import static java.lang.String.format; @@ -217,16 +219,15 @@ public Object deserialize(final Writable writable) throws SerDeException { return row; } - 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); + @VisibleForTesting + Object getValue(final BSONObject doc, final String mongoMapping) { + Object res = doc; + String[] mappings = mongoMapping.split("(? diff --git a/hive/src/test/java/com/mongodb/hadoop/hive/BSONSerDeTest.java b/hive/src/test/java/com/mongodb/hadoop/hive/BSONSerDeTest.java index c89e5d6b..7b78e4d2 100644 --- a/hive/src/test/java/com/mongodb/hadoop/hive/BSONSerDeTest.java +++ b/hive/src/test/java/com/mongodb/hadoop/hive/BSONSerDeTest.java @@ -17,6 +17,7 @@ import org.bson.types.ObjectId; import org.junit.Test; +import java.lang.reflect.Method; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Date; @@ -331,4 +332,21 @@ public void testStruct() throws SerDeException { Object serialized = serde.serialize(obj, oi); assertThat(new BSONWritable(bObject), equalTo(serialized)); } + + @Test + public void testKeyWithDot() throws SerDeException { + BasicBSONObject value = new BasicBSONObject(); + BasicBSONObject nested = new BasicBSONObject(); + nested.put("simple", 10); + nested.put("with.dot", 20); + value.put("simple", 30); + value.put("with.dot", 40); + value.put("nested", nested); + + BSONSerDe serde = new BSONSerDe(); + assertThat((Integer)serde.getValue(value, "simple"), equalTo(30)); + assertThat((Integer)serde.getValue(value, "with\\.dot"), equalTo(40)); + assertThat((Integer)serde.getValue(value, "nested.simple"), equalTo(10)); + assertThat((Integer)serde.getValue(value, "nested.with\\.dot"), equalTo(20)); + } }