Search in sources :

Example 1 with ConstantAveragerFactory

use of org.apache.druid.query.movingaverage.averagers.ConstantAveragerFactory in project druid by druid-io.

the class MovingAverageIterableTest method testNext.

@Test
public void testNext() {
    List<DimensionSpec> dims = Arrays.asList(new DefaultDimensionSpec(GENDER, GENDER), new DefaultDimensionSpec(AGE, AGE), new DefaultDimensionSpec(COUNTRY, COUNTRY));
    Sequence<RowBucket> dayBuckets = Sequences.simple(Arrays.asList(new RowBucket(JAN_1, Arrays.asList(new MapBasedRow(JAN_1, DIMS1), new MapBasedRow(JAN_1, DIMS2))), new RowBucket(JAN_2, Collections.singletonList(new MapBasedRow(JAN_2, DIMS1))), new RowBucket(JAN_3, Collections.emptyList()), new RowBucket(JAN_4, Arrays.asList(new MapBasedRow(JAN_4, DIMS2), new MapBasedRow(JAN_4, DIMS3)))));
    Iterable<Row> iterable = new MovingAverageIterable(dayBuckets, dims, Collections.singletonList(new ConstantAveragerFactory("noop", 1, 1.1f)), Collections.emptyList(), Collections.emptyList());
    Iterator<Row> iter = iterable.iterator();
    Assert.assertTrue(iter.hasNext());
    Row r = iter.next();
    Assert.assertEquals(JAN_1, r.getTimestamp());
    Assert.assertEquals("m", r.getRaw(GENDER));
    Assert.assertTrue(iter.hasNext());
    r = iter.next();
    Assert.assertEquals(JAN_1, r.getTimestamp());
    Assert.assertEquals("f", r.getRaw(GENDER));
    Assert.assertTrue(iter.hasNext());
    r = iter.next();
    Assert.assertEquals(JAN_2, r.getTimestamp());
    Assert.assertEquals("m", r.getRaw(GENDER));
    Assert.assertTrue(iter.hasNext());
    r = iter.next();
    Assert.assertEquals(JAN_2, r.getTimestamp());
    Assert.assertEquals("f", r.getRaw(GENDER));
    Assert.assertTrue(iter.hasNext());
    r = iter.next();
    Row r2 = r;
    Assert.assertEquals(JAN_3, r.getTimestamp());
    Assert.assertEquals("US", r.getRaw(COUNTRY));
    Assert.assertTrue(iter.hasNext());
    r = iter.next();
    Assert.assertEquals(JAN_3, r.getTimestamp());
    Assert.assertEquals("US", r.getRaw(COUNTRY));
    Assert.assertThat(r.getRaw(AGE), CoreMatchers.not(CoreMatchers.equalTo(r2.getRaw(AGE))));
    Assert.assertTrue(iter.hasNext());
    r = iter.next();
    Assert.assertEquals(JAN_4, r.getTimestamp());
    Assert.assertEquals("f", r.getRaw(GENDER));
    Assert.assertTrue(iter.hasNext());
    r = iter.next();
    Assert.assertEquals(JAN_4, r.getTimestamp());
    Assert.assertEquals("u", r.getRaw(GENDER));
    Assert.assertTrue(iter.hasNext());
    r = iter.next();
    Assert.assertEquals(JAN_4, r.getTimestamp());
    Assert.assertEquals("m", r.getRaw(GENDER));
    Assert.assertFalse(iter.hasNext());
}
Also used : MapBasedRow(org.apache.druid.data.input.MapBasedRow) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) ConstantAveragerFactory(org.apache.druid.query.movingaverage.averagers.ConstantAveragerFactory) MapBasedRow(org.apache.druid.data.input.MapBasedRow) Row(org.apache.druid.data.input.Row) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 2 with ConstantAveragerFactory

use of org.apache.druid.query.movingaverage.averagers.ConstantAveragerFactory in project druid by druid-io.

the class MovingAverageIterableTest method testAveraging.

@Test
public void testAveraging() {
    Map<String, Object> event1 = new HashMap<>();
    Map<String, Object> event2 = new HashMap<>();
    Map<String, Object> event3 = new HashMap<>();
    Map<String, Object> event4 = new HashMap<>();
    List<DimensionSpec> ds = new ArrayList<>();
    ds.add(new DefaultDimensionSpec("gender", "gender"));
    event1.put("gender", "m");
    event1.put("pageViews", 10L);
    Row row1 = new MapBasedRow(JAN_1, event1);
    event2.put("gender", "m");
    event2.put("pageViews", 20L);
    Row row2 = new MapBasedRow(JAN_2, event2);
    event3.put("gender", "m");
    event3.put("pageViews", 30L);
    Row row3 = new MapBasedRow(JAN_3, event3);
    event4.put("gender", "f");
    event4.put("pageViews", 40L);
    Row row4 = new MapBasedRow(JAN_3, event4);
    float retval = 14.5f;
    Sequence<RowBucket> seq = Sequences.simple(Arrays.asList(new RowBucket(JAN_1, Collections.singletonList(row1)), new RowBucket(JAN_2, Collections.singletonList(row2)), new RowBucket(JAN_3, Arrays.asList(row3, row4))));
    Iterator<Row> iter = new MovingAverageIterable(seq, ds, Arrays.asList(new ConstantAveragerFactory("costPageViews", 7, retval), new LongMeanAveragerFactory("movingAvgPageViews", 7, 1, "pageViews")), Collections.emptyList(), Collections.singletonList(new LongSumAggregatorFactory("pageViews", "pageViews"))).iterator();
    Assert.assertTrue(iter.hasNext());
    Row caResult = iter.next();
    Assert.assertEquals(JAN_1, caResult.getTimestamp());
    Assert.assertEquals("m", (caResult.getDimension("gender")).get(0));
    Assert.assertEquals(retval, caResult.getMetric("costPageViews").floatValue(), 0.0f);
    Assert.assertEquals(1.4285715f, caResult.getMetric("movingAvgPageViews").floatValue(), 0.0f);
    Assert.assertTrue(iter.hasNext());
    caResult = iter.next();
    Assert.assertEquals("m", (caResult.getDimension("gender")).get(0));
    Assert.assertEquals(4.285714f, caResult.getMetric("movingAvgPageViews").floatValue(), 0.0f);
    Assert.assertTrue(iter.hasNext());
    caResult = iter.next();
    Assert.assertEquals("m", (caResult.getDimension("gender")).get(0));
    Assert.assertEquals(8.571428f, caResult.getMetric("movingAvgPageViews").floatValue(), 0.0f);
    Assert.assertTrue(iter.hasNext());
    caResult = iter.next();
    Assert.assertEquals("f", (caResult.getDimension("gender")).get(0));
    Assert.assertEquals(5.714285850f, caResult.getMetric("movingAvgPageViews").floatValue(), 0.0f);
    Assert.assertFalse(iter.hasNext());
}
Also used : DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) ConstantAveragerFactory(org.apache.druid.query.movingaverage.averagers.ConstantAveragerFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) MapBasedRow(org.apache.druid.data.input.MapBasedRow) LongMeanAveragerFactory(org.apache.druid.query.movingaverage.averagers.LongMeanAveragerFactory) MapBasedRow(org.apache.druid.data.input.MapBasedRow) Row(org.apache.druid.data.input.Row) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Aggregations

MapBasedRow (org.apache.druid.data.input.MapBasedRow)2 Row (org.apache.druid.data.input.Row)2 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)2 DimensionSpec (org.apache.druid.query.dimension.DimensionSpec)2 ConstantAveragerFactory (org.apache.druid.query.movingaverage.averagers.ConstantAveragerFactory)2 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)1 LongMeanAveragerFactory (org.apache.druid.query.movingaverage.averagers.LongMeanAveragerFactory)1