Search in sources :

Example 1 with DurationGranularity

use of io.druid.java.util.common.granularity.DurationGranularity in project druid by druid-io.

the class QueryGranularityTest method testSerializeDuration.

@Test
public void testSerializeDuration() throws Exception {
    ObjectMapper mapper = new DefaultObjectMapper();
    String json = "{ \"type\": \"duration\", \"duration\": \"3600000\" }";
    Granularity gran = mapper.readValue(json, Granularity.class);
    Assert.assertEquals(new DurationGranularity(3600000, null), gran);
    json = "{ \"type\": \"duration\", \"duration\": \"5\", \"origin\": \"2012-09-01T00:00:00.002Z\" }";
    gran = mapper.readValue(json, Granularity.class);
    Assert.assertEquals(new DurationGranularity(5, 2), gran);
    DurationGranularity expected = new DurationGranularity(5, 2);
    Assert.assertEquals(expected, mapper.readValue(mapper.writeValueAsString(expected), Granularity.class));
    String illegalJson = "{ \"type\": \"duration\", \"duration\": \"0\" }";
    try {
        mapper.readValue(illegalJson, Granularity.class);
        Assert.fail();
    } catch (JsonMappingException e) {
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) DurationGranularity(io.druid.java.util.common.granularity.DurationGranularity) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) Granularity(io.druid.java.util.common.granularity.Granularity) DurationGranularity(io.druid.java.util.common.granularity.DurationGranularity) PeriodGranularity(io.druid.java.util.common.granularity.PeriodGranularity) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with DurationGranularity

use of io.druid.java.util.common.granularity.DurationGranularity in project druid by druid-io.

the class QueryGranularityTest method testDurationToDateTime.

@Test
public void testDurationToDateTime() throws Exception {
    final DateTime origin = new DateTime("2012-01-02T05:00:00.000-08:00");
    Granularity gran = new DurationGranularity(new Period("PT12H5M").toStandardDuration().getMillis(), origin);
    Assert.assertEquals(new DateTime("2012-01-01T05:00:04.123-08:00"), gran.toDateTime(new DateTime("2012-01-01T05:00:04.123-08:00").getMillis()));
}
Also used : DurationGranularity(io.druid.java.util.common.granularity.DurationGranularity) Period(org.joda.time.Period) Granularity(io.druid.java.util.common.granularity.Granularity) DurationGranularity(io.druid.java.util.common.granularity.DurationGranularity) PeriodGranularity(io.druid.java.util.common.granularity.PeriodGranularity) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 3 with DurationGranularity

use of io.druid.java.util.common.granularity.DurationGranularity in project druid by druid-io.

the class GroupByQueryRunnerTest method testGroupByWithDurationGranularity.

@Test
public void testGroupByWithDurationGranularity() {
    GroupByQuery query = GroupByQuery.builder().setDataSource(QueryRunnerTestHelper.dataSource).setQuerySegmentSpec(QueryRunnerTestHelper.firstToThird).setDimensions(Lists.<DimensionSpec>newArrayList(new DefaultDimensionSpec("quality", "alias"))).setAggregatorSpecs(Arrays.asList(QueryRunnerTestHelper.rowsCount, new LongSumAggregatorFactory("idx", "index"))).setGranularity(new DurationGranularity(86400L, 0L)).build();
    List<Row> expectedResults = Arrays.asList(GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "automotive", "rows", 1L, "idx", 135L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "business", "rows", 1L, "idx", 118L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "entertainment", "rows", 1L, "idx", 158L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "health", "rows", 1L, "idx", 120L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "mezzanine", "rows", 3L, "idx", 2870L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "news", "rows", 1L, "idx", 121L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "premium", "rows", 3L, "idx", 2900L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "technology", "rows", 1L, "idx", 78L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "travel", "rows", 1L, "idx", 119L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "automotive", "rows", 1L, "idx", 147L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "business", "rows", 1L, "idx", 112L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "entertainment", "rows", 1L, "idx", 166L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "health", "rows", 1L, "idx", 113L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "mezzanine", "rows", 3L, "idx", 2447L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "news", "rows", 1L, "idx", 114L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "premium", "rows", 3L, "idx", 2505L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "technology", "rows", 1L, "idx", 97L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "travel", "rows", 1L, "idx", 126L));
    Iterable<Row> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
    TestHelper.assertExpectedObjects(expectedResults, results, "");
}
Also used : DefaultDimensionSpec(io.druid.query.dimension.DefaultDimensionSpec) RegexFilteredDimensionSpec(io.druid.query.dimension.RegexFilteredDimensionSpec) ExtractionDimensionSpec(io.druid.query.dimension.ExtractionDimensionSpec) ListFilteredDimensionSpec(io.druid.query.dimension.ListFilteredDimensionSpec) DimensionSpec(io.druid.query.dimension.DimensionSpec) LongSumAggregatorFactory(io.druid.query.aggregation.LongSumAggregatorFactory) DurationGranularity(io.druid.java.util.common.granularity.DurationGranularity) Row(io.druid.data.input.Row) DefaultDimensionSpec(io.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 4 with DurationGranularity

use of io.druid.java.util.common.granularity.DurationGranularity in project druid by druid-io.

the class DataSchemaTest method testSerde.

@Test
public void testSerde() throws Exception {
    String jsonStr = "{" + "\"dataSource\":\"test\"," + "\"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(), "test");
    Assert.assertEquals(actual.getParser().getParseSpec(), new JSONParseSpec(new TimestampSpec("xXx", null, null), new DimensionsSpec(null, Arrays.asList("metric1", "xXx", "col1"), null), null, null));
    Assert.assertEquals(actual.getAggregators(), new AggregatorFactory[] { new DoubleSumAggregatorFactory("metric1", "col1") });
    Assert.assertEquals(actual.getGranularitySpec(), new ArbitraryGranularitySpec(new DurationGranularity(86400000, null), ImmutableList.of(Interval.parse("2014/2015"))));
}
Also used : DoubleSumAggregatorFactory(io.druid.query.aggregation.DoubleSumAggregatorFactory) TimestampSpec(io.druid.data.input.impl.TimestampSpec) DimensionsSpec(io.druid.data.input.impl.DimensionsSpec) DurationGranularity(io.druid.java.util.common.granularity.DurationGranularity) JSONParseSpec(io.druid.data.input.impl.JSONParseSpec) ArbitraryGranularitySpec(io.druid.segment.indexing.granularity.ArbitraryGranularitySpec) Test(org.junit.Test)

Aggregations

DurationGranularity (io.druid.java.util.common.granularity.DurationGranularity)4 Test (org.junit.Test)4 Granularity (io.druid.java.util.common.granularity.Granularity)2 PeriodGranularity (io.druid.java.util.common.granularity.PeriodGranularity)2 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Row (io.druid.data.input.Row)1 DimensionsSpec (io.druid.data.input.impl.DimensionsSpec)1 JSONParseSpec (io.druid.data.input.impl.JSONParseSpec)1 TimestampSpec (io.druid.data.input.impl.TimestampSpec)1 DefaultObjectMapper (io.druid.jackson.DefaultObjectMapper)1 DoubleSumAggregatorFactory (io.druid.query.aggregation.DoubleSumAggregatorFactory)1 LongSumAggregatorFactory (io.druid.query.aggregation.LongSumAggregatorFactory)1 DefaultDimensionSpec (io.druid.query.dimension.DefaultDimensionSpec)1 DimensionSpec (io.druid.query.dimension.DimensionSpec)1 ExtractionDimensionSpec (io.druid.query.dimension.ExtractionDimensionSpec)1 ListFilteredDimensionSpec (io.druid.query.dimension.ListFilteredDimensionSpec)1 RegexFilteredDimensionSpec (io.druid.query.dimension.RegexFilteredDimensionSpec)1 ArbitraryGranularitySpec (io.druid.segment.indexing.granularity.ArbitraryGranularitySpec)1 DateTime (org.joda.time.DateTime)1