diff --git a/api/src/test/java/io/druid/data/input/impl/JSONParseSpecTest.java b/api/src/test/java/io/druid/data/input/impl/JSONParseSpecTest.java index 69834c9aa54..26812b459ba 100644 --- a/api/src/test/java/io/druid/data/input/impl/JSONParseSpecTest.java +++ b/api/src/test/java/io/druid/data/input/impl/JSONParseSpecTest.java @@ -22,17 +22,63 @@ package io.druid.data.input.impl; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableList; import io.druid.TestObjectMapper; +import io.druid.java.util.common.parsers.JSONPathFieldSpec; +import io.druid.java.util.common.parsers.JSONPathFieldType; +import io.druid.java.util.common.parsers.JSONPathSpec; +import io.druid.java.util.common.parsers.Parser; import org.junit.Assert; import org.junit.Test; import java.io.IOException; import java.util.Arrays; import java.util.HashMap; +import java.util.Map; public class JSONParseSpecTest { private final ObjectMapper jsonMapper = new TestObjectMapper(); + @Test + public void testParseRow() + { + final JSONParseSpec parseSpec = new JSONParseSpec( + new TimestampSpec("timestamp", "iso", null), + new DimensionsSpec(DimensionsSpec.getDefaultSchemas(ImmutableList.of("bar", "foo")), null, null), + new JSONPathSpec( + true, + ImmutableList.of( + new JSONPathFieldSpec(JSONPathFieldType.ROOT, "root_baz", "baz"), + new JSONPathFieldSpec(JSONPathFieldType.ROOT, "root_baz2", "baz2"), + new JSONPathFieldSpec(JSONPathFieldType.PATH, "path_omg", "$.o.mg"), + new JSONPathFieldSpec(JSONPathFieldType.PATH, "path_omg2", "$.o.mg2"), + new JSONPathFieldSpec(JSONPathFieldType.JQ, "jq_omg", ".o.mg"), + new JSONPathFieldSpec(JSONPathFieldType.JQ, "jq_omg2", ".o.mg2") + ) + ), + null + ); + + final Map expected = new HashMap<>(); + expected.put("foo", "x"); + expected.put("baz", 4L); + expected.put("root_baz", 4L); + expected.put("root_baz2", null); + expected.put("path_omg", 1L); + expected.put("path_omg2", null); + expected.put("jq_omg", 1L); + expected.put("jq_omg2", null); + + final Parser parser = parseSpec.makeParser(); + final Map parsedRow = parser.parse("{\"bar\":null,\"foo\":\"x\",\"baz\":4,\"o\":{\"mg\":1}}"); + Assert.assertNotNull(parsedRow); + Assert.assertEquals(expected, parsedRow); + Assert.assertNull(parsedRow.get("bar")); + Assert.assertNull(parsedRow.get("buzz")); + Assert.assertNull(parsedRow.get("root_baz2")); + Assert.assertNull(parsedRow.get("jq_omg2")); + Assert.assertNull(parsedRow.get("path_omg2")); + } + @Test public void testSerde() throws IOException { diff --git a/java-util/src/main/java/io/druid/java/util/common/parsers/JSONFlattenerMaker.java b/java-util/src/main/java/io/druid/java/util/common/parsers/JSONFlattenerMaker.java index 9c6bfc4f953..5f1c6736a02 100644 --- a/java-util/src/main/java/io/druid/java/util/common/parsers/JSONFlattenerMaker.java +++ b/java-util/src/main/java/io/druid/java/util/common/parsers/JSONFlattenerMaker.java @@ -100,7 +100,7 @@ public class JSONFlattenerMaker implements ObjectFlatteners.FlattenerMaker