Search in sources :

Example 26 with JSONParseSpec

use of org.apache.druid.data.input.impl.JSONParseSpec in project druid by druid-io.

the class DataSchemaTest method testSerde.

@Test
public void testSerde() throws Exception {
    String jsonStr = "{" + "\"dataSource\":\"" + StringEscapeUtils.escapeJson(IdUtilsTest.VALID_ID_CHARS) + "\"," + "\"parser\":{" + "\"type\":\"string\"," + "\"parseSpec\":{" + "\"format\":\"json\"," + "\"timestampSpec\":{\"column\":\"xXx\", \"format\": \"auto\", \"missingValue\": null}," + "\"dimensionsSpec\":{\"dimensions\":[], \"dimensionExclusions\":[]}," + "\"flattenSpec\":{\"useFieldDiscovery\":true, \"fields\":[]}," + "\"featureSpec\":{}}," + "\"encoding\":\"UTF-8\"" + "}," + "\"metricsSpec\":[{\"type\":\"doubleSum\",\"name\":\"metric1\",\"fieldName\":\"col1\"}]," + "\"granularitySpec\":{" + "\"type\":\"arbitrary\"," + "\"queryGranularity\":{\"type\":\"duration\",\"duration\":86400000,\"origin\":\"1970-01-01T00:00:00.000Z\"}," + "\"intervals\":[\"2014-01-01T00:00:00.000Z/2015-01-01T00:00:00.000Z\"]}}";
    DataSchema actual = jsonMapper.readValue(jsonMapper.writeValueAsString(jsonMapper.readValue(jsonStr, DataSchema.class)), DataSchema.class);
    Assert.assertEquals(actual.getDataSource(), IdUtilsTest.VALID_ID_CHARS);
    Assert.assertEquals(actual.getParser().getParseSpec(), new JSONParseSpec(new TimestampSpec("xXx", null, null), DimensionsSpec.builder().setDimensionExclusions(Arrays.asList("__time", "metric1", "xXx", "col1")).build(), null, null, null));
    Assert.assertArrayEquals(actual.getAggregators(), new AggregatorFactory[] { new DoubleSumAggregatorFactory("metric1", "col1") });
    Assert.assertEquals(actual.getGranularitySpec(), new ArbitraryGranularitySpec(new DurationGranularity(86400000, null), ImmutableList.of(Intervals.of("2014/2015"))));
}
Also used : DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) TimestampSpec(org.apache.druid.data.input.impl.TimestampSpec) DurationGranularity(org.apache.druid.java.util.common.granularity.DurationGranularity) JSONParseSpec(org.apache.druid.data.input.impl.JSONParseSpec) ArbitraryGranularitySpec(org.apache.druid.segment.indexing.granularity.ArbitraryGranularitySpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test) IdUtilsTest(org.apache.druid.common.utils.IdUtilsTest)

Example 27 with JSONParseSpec

use of org.apache.druid.data.input.impl.JSONParseSpec in project druid by druid-io.

the class DataSchemaTest method testSerdeWithUpdatedDataSchemaRemovedField.

@Test
public void testSerdeWithUpdatedDataSchemaRemovedField() throws IOException {
    Map<String, Object> parser = jsonMapper.convertValue(new StringInputRowParser(new JSONParseSpec(new TimestampSpec("time", "auto", null), new DimensionsSpec(DimensionsSpec.getDefaultSchemas(ImmutableList.of("dimB", "dimA"))), null, null, null), null), JacksonUtils.TYPE_REFERENCE_MAP_STRING_OBJECT);
    TestModifiedDataSchema originalSchema = new TestModifiedDataSchema(IdUtilsTest.VALID_ID_CHARS, null, null, new AggregatorFactory[] { new DoubleSumAggregatorFactory("metric1", "col1"), new DoubleSumAggregatorFactory("metric2", "col2") }, new ArbitraryGranularitySpec(Granularities.DAY, ImmutableList.of(Intervals.of("2014/2015"))), null, parser, jsonMapper, "some arbitrary string");
    String serialized = jsonMapper.writeValueAsString(originalSchema);
    DataSchema deserialized = jsonMapper.readValue(serialized, DataSchema.class);
    Assert.assertEquals(originalSchema.getDataSource(), deserialized.getDataSource());
    Assert.assertEquals(originalSchema.getGranularitySpec(), deserialized.getGranularitySpec());
    Assert.assertEquals(originalSchema.getParser().getParseSpec(), deserialized.getParser().getParseSpec());
    Assert.assertArrayEquals(originalSchema.getAggregators(), deserialized.getAggregators());
    Assert.assertEquals(originalSchema.getTransformSpec(), deserialized.getTransformSpec());
    Assert.assertEquals(originalSchema.getParserMap(), deserialized.getParserMap());
}
Also used : DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) StringInputRowParser(org.apache.druid.data.input.impl.StringInputRowParser) TimestampSpec(org.apache.druid.data.input.impl.TimestampSpec) DimensionsSpec(org.apache.druid.data.input.impl.DimensionsSpec) JSONParseSpec(org.apache.druid.data.input.impl.JSONParseSpec) ArbitraryGranularitySpec(org.apache.druid.segment.indexing.granularity.ArbitraryGranularitySpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test) IdUtilsTest(org.apache.druid.common.utils.IdUtilsTest)

Example 28 with JSONParseSpec

use of org.apache.druid.data.input.impl.JSONParseSpec in project druid by druid-io.

the class DataSchemaTest method testSerdeWithUpdatedDataSchemaAddedField.

@Test
public void testSerdeWithUpdatedDataSchemaAddedField() throws IOException {
    Map<String, Object> parser = jsonMapper.convertValue(new StringInputRowParser(new JSONParseSpec(new TimestampSpec("time", "auto", null), new DimensionsSpec(DimensionsSpec.getDefaultSchemas(ImmutableList.of("dimB", "dimA"))), null, null, null), null), JacksonUtils.TYPE_REFERENCE_MAP_STRING_OBJECT);
    DataSchema originalSchema = new DataSchema(IdUtilsTest.VALID_ID_CHARS, parser, new AggregatorFactory[] { new DoubleSumAggregatorFactory("metric1", "col1"), new DoubleSumAggregatorFactory("metric2", "col2") }, new ArbitraryGranularitySpec(Granularities.DAY, ImmutableList.of(Intervals.of("2014/2015"))), null, jsonMapper);
    String serialized = jsonMapper.writeValueAsString(originalSchema);
    TestModifiedDataSchema deserialized = jsonMapper.readValue(serialized, TestModifiedDataSchema.class);
    Assert.assertEquals(null, deserialized.getExtra());
    Assert.assertEquals(originalSchema.getDataSource(), deserialized.getDataSource());
    Assert.assertEquals(originalSchema.getGranularitySpec(), deserialized.getGranularitySpec());
    Assert.assertEquals(originalSchema.getParser().getParseSpec(), deserialized.getParser().getParseSpec());
    Assert.assertArrayEquals(originalSchema.getAggregators(), deserialized.getAggregators());
    Assert.assertEquals(originalSchema.getTransformSpec(), deserialized.getTransformSpec());
    Assert.assertEquals(originalSchema.getParserMap(), deserialized.getParserMap());
}
Also used : DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) StringInputRowParser(org.apache.druid.data.input.impl.StringInputRowParser) TimestampSpec(org.apache.druid.data.input.impl.TimestampSpec) DimensionsSpec(org.apache.druid.data.input.impl.DimensionsSpec) JSONParseSpec(org.apache.druid.data.input.impl.JSONParseSpec) ArbitraryGranularitySpec(org.apache.druid.segment.indexing.granularity.ArbitraryGranularitySpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test) IdUtilsTest(org.apache.druid.common.utils.IdUtilsTest)

Aggregations

JSONParseSpec (org.apache.druid.data.input.impl.JSONParseSpec)28 TimestampSpec (org.apache.druid.data.input.impl.TimestampSpec)26 StringInputRowParser (org.apache.druid.data.input.impl.StringInputRowParser)16 Test (org.junit.Test)16 DimensionsSpec (org.apache.druid.data.input.impl.DimensionsSpec)13 ArbitraryGranularitySpec (org.apache.druid.segment.indexing.granularity.ArbitraryGranularitySpec)11 IdUtilsTest (org.apache.druid.common.utils.IdUtilsTest)10 DoubleSumAggregatorFactory (org.apache.druid.query.aggregation.DoubleSumAggregatorFactory)10 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)10 ArrayList (java.util.ArrayList)7 JSONPathFieldSpec (org.apache.druid.java.util.common.parsers.JSONPathFieldSpec)6 JSONPathSpec (org.apache.druid.java.util.common.parsers.JSONPathSpec)6 Before (org.junit.Before)6 MapInputRowParser (org.apache.druid.data.input.impl.MapInputRowParser)5 DefaultObjectMapper (org.apache.druid.jackson.DefaultObjectMapper)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)4 CSVParseSpec (org.apache.druid.data.input.impl.CSVParseSpec)3 StringDimensionSchema (org.apache.druid.data.input.impl.StringDimensionSchema)3 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)3