@JsonCreator public JSONFlatDataParser( @JacksonInject @Json ObjectMapper jsonMapper, @JsonProperty("keyFieldName") final String keyFieldName, @JsonProperty("valueFieldName") final String valueFieldName ) { Preconditions.checkArgument(!Strings.isNullOrEmpty(keyFieldName), "[keyFieldName] cannot be empty"); Preconditions.checkArgument(!Strings.isNullOrEmpty(valueFieldName), "[valueFieldName] cannot be empty"); this.keyFieldName = keyFieldName; this.valueFieldName = valueFieldName; // Copy jsonMapper; don't want to share canonicalization tables, etc., with the global ObjectMapper. this.parser = new DelegateParser( new JSONPathParser( new JSONPathSpec( false, ImmutableList.of( new JSONPathFieldSpec(JSONPathFieldType.ROOT, keyFieldName, keyFieldName), new JSONPathFieldSpec(JSONPathFieldType.ROOT, valueFieldName, valueFieldName) ) ), jsonMapper.copy() ), keyFieldName, valueFieldName ); }
fields.add(JSONPathFieldSpec.createJqField("hey0barx", ".hey[0].barx")); JSONPathSpec flattenSpec = new JSONPathSpec(true, fields); JSONPathSpec.class ); Assert.assertTrue(serde.isUseFieldDiscovery()); List<JSONPathFieldSpec> serdeFields = serde.getFields(); JSONPathFieldSpec foobar1 = serdeFields.get(0); JSONPathFieldSpec baz0 = serdeFields.get(1);
fields.add(JSONPathFieldSpec.createRootField("foo.bar1")); JSONPathSpec flattenSpec = new JSONPathSpec(true, fields); final StringInputRowParser parser = new StringInputRowParser( new JSONParseSpec( List<JSONPathFieldSpec> fieldSpecs = parsedSpec.getFields(); Assert.assertEquals(JSONPathFieldType.ROOT, fieldSpecs.get(0).getType()); Assert.assertEquals("parseThisRootField", fieldSpecs.get(0).getName());
for (final JSONPathFieldSpec fieldSpec : flattenSpec.getFields()) { final Function<T, Object> extractor;
for (final JSONPathFieldSpec fieldSpec : flattenSpec.getFields()) { final Function<T, Object> extractor;
@Test public void testWithNumbers() { List<JSONPathFieldSpec> fields = new ArrayList<>(); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(true, fields), null); final Map<String, Object> jsonMap = jsonParser.parseToMap(numbersJson); Assert.assertEquals( "jsonMap", ImmutableMap.of("five", 5.0, "six", 6L, "many", 1234567878900L, "toomany", 1.23456789E21), jsonMap ); }
@Test public void testSimple() { List<JSONPathFieldSpec> fields = new ArrayList<>(); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(true, fields), null); final Map<String, Object> jsonMap = jsonParser.parseToMap(json); Assert.assertEquals( "jsonMap", ImmutableMap.of("one", "foo", "two", ImmutableList.of("bar", "baz"), "three", "qux"), jsonMap ); }
@Test public void testWithWhackyCharacters() { List<JSONPathFieldSpec> fields = new ArrayList<>(); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(true, fields), null); final Map<String, Object> jsonMap = jsonParser.parseToMap(whackyCharacterJson); Assert.assertEquals( "jsonMap", ImmutableMap.of("one", "foo?"), jsonMap ); }
public Parser getFieldDiscoveryParser() { List<JSONPathFieldSpec> fields = new ArrayList<>(); JSONPathSpec flattenSpec = new JSONPathSpec(true, fields); JSONParseSpec spec = new JSONParseSpec( new TimestampSpec("ts", "iso", null), new DimensionsSpec(null, null, null), flattenSpec, null ); return spec.makeParser(); }
fields.add(JSONPathFieldSpec.createNestedField("e4.e4.m4", "$['e4.e4.m4']")); JSONPathSpec flattenSpec = new JSONPathSpec(false, fields); JSONParseSpec spec = new JSONParseSpec( new TimestampSpec("ts", "iso", null),
fields.add(JSONPathFieldSpec.createJqField("e4.e4.m4", ".e4.e4.m4")); JSONPathSpec flattenSpec = new JSONPathSpec(true, fields); JSONParseSpec spec = new JSONParseSpec( new TimestampSpec("ts", "iso", null),
fields.add(JSONPathFieldSpec.createNestedField("e4.e4.m4", "$.e4.e4.m4")); JSONPathSpec flattenSpec = new JSONPathSpec(true, fields); JSONParseSpec spec = new JSONParseSpec( new TimestampSpec("ts", "iso", null),
@Test public void testParseFail() { List<JSONPathFieldSpec> fields = new ArrayList<>(); thrown.expect(ParseException.class); thrown.expectMessage("Unable to parse row [" + notJson + "]"); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(true, fields), null); jsonParser.parseToMap(notJson); } }
@Test public void testRejectDuplicates() { List<JSONPathFieldSpec> fields = new ArrayList<>(); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "met-array", "$.met.a")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "met-array", "$.met.a")); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Cannot have duplicate field definition: met-array"); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(false, fields), null); jsonParser.parseToMap(nestedJson); }
@Test public void testRejectDuplicates2() { List<JSONPathFieldSpec> fields = new ArrayList<>(); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "met-array", "$.met.a")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.JQ, "met-array", ".met.a")); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Cannot have duplicate field definition: met-array"); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(false, fields), null); jsonParser.parseToMap(nestedJson); }
fields.add(new JSONPathFieldSpec(JSONPathFieldType.JQ, "jq-met-array", ".met.a")); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(false, fields), null); final Map<String, Object> jsonMap = jsonParser.parseToMap(nestedJson);
final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(true, fields), null); final Map<String, Object> jsonMap = jsonParser.parseToMap(nestedJson);
@Test public void testParseRowWithConditional() { final JSONParseSpec parseSpec = new JSONParseSpec( new TimestampSpec("timestamp", "iso", null), new DimensionsSpec(DimensionsSpec.getDefaultSchemas(ImmutableList.of("foo")), null, null), new JSONPathSpec( true, ImmutableList.of( new JSONPathFieldSpec(JSONPathFieldType.PATH, "foo", "$.[?(@.maybe_object)].maybe_object.foo.test"), new JSONPathFieldSpec(JSONPathFieldType.PATH, "baz", "$.maybe_object_2.foo.test"), new JSONPathFieldSpec(JSONPathFieldType.PATH, "bar", "$.[?(@.something_else)].something_else.foo") ) ), null ); final Map<String, Object> expected = new HashMap<>(); expected.put("foo", new ArrayList()); expected.put("baz", null); expected.put("bar", Collections.singletonList("test")); final Parser<String, Object> parser = parseSpec.makeParser(); final Map<String, Object> parsedRow = parser.parseToMap("{\"something_else\": {\"foo\": \"test\"}}"); Assert.assertNotNull(parsedRow); Assert.assertEquals(expected, parsedRow); }
new TimestampSpec("timestamp", "iso", null), new DimensionsSpec(DimensionsSpec.getDefaultSchemas(ImmutableList.of("bar", "foo")), null, null), new JSONPathSpec( true, ImmutableList.of(