Skip to content
This repository has been archived by the owner on Jan 29, 2022. It is now read-only.

com.mongodb.hadoop.hive.BSONSerde.getValue: supporting quoted dots #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions hive/src/main/java/com/mongodb/hadoop/hive/BSONSerDe.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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("(?<!\\\\)" + Pattern.quote("."));
for (String mapping: mappings)
res = ((BSONObject) res).get(mapping.replace("\\.", "."));
return res;
}


/**
* For a given Object value and its supposed TypeInfo determine and return its Hive object representation
* <p/>
Expand Down
18 changes: 18 additions & 0 deletions hive/src/test/java/com/mongodb/hadoop/hive/BSONSerDeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}