use of org.apache.druid.query.dimension.DimensionSpec in project druid by druid-io.
the class MovingAverageHelper method getDimKeyFromRow.
/**
* @param dimensions A list of DimensionSpec in the specified in the query
* @param row The Row to be used for looking up dimension values
*
* @return A Map of dimension/value from the row
*/
public static Map<String, Object> getDimKeyFromRow(Collection<DimensionSpec> dimensions, Row row) {
Map<String, Object> key = new HashMap<>();
Map<String, Object> event = ((MapBasedRow) row).getEvent();
for (DimensionSpec dimension : dimensions) {
key.put(dimension.getOutputName(), event.get(dimension.getOutputName()));
}
return key;
}
use of org.apache.druid.query.dimension.DimensionSpec in project druid by druid-io.
the class MovingAverageIterableTest method testCompleteData.
@Test
public void testCompleteData() {
Map<String, Object> event1 = new HashMap<>();
Map<String, Object> event2 = new HashMap<>();
Map<String, Object> event3 = new HashMap<>();
event1.put("gender", "m");
event1.put("pageViews", 10L);
event2.put("gender", "f");
event2.put("pageViews", 20L);
event3.put("gender", "u");
event3.put("pageViews", 30L);
List<DimensionSpec> ds = new ArrayList<>();
ds.add(new DefaultDimensionSpec("gender", "gender"));
Row jan1Row1 = new MapBasedRow(JAN_1, event1);
Row jan1Row2 = new MapBasedRow(JAN_1, event2);
Row jan1Row3 = new MapBasedRow(JAN_1, event3);
Row jan2Row1 = new MapBasedRow(JAN_2, event1);
Row jan2Row2 = new MapBasedRow(JAN_2, event2);
Row jan2Row3 = new MapBasedRow(JAN_2, event3);
Sequence<RowBucket> seq = Sequences.simple(Arrays.asList(new RowBucket(JAN_1, Arrays.asList(jan1Row1, jan1Row2, jan1Row3)), new RowBucket(JAN_2, Arrays.asList(jan2Row1, jan2Row2, jan2Row3))));
Iterator<Row> iter = new MovingAverageIterable(seq, ds, Collections.singletonList(new LongMeanAveragerFactory("movingAvgPageViews", 2, 1, "pageViews")), Collections.emptyList(), Collections.singletonList(new LongSumAggregatorFactory("pageViews", "pageViews"))).iterator();
Assert.assertTrue(iter.hasNext());
Row result = iter.next();
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_1, (result.getTimestamp()));
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("f", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_1, (result.getTimestamp()));
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("u", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_1, (result.getTimestamp()));
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_2, (result.getTimestamp()));
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("f", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_2, (result.getTimestamp()));
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("u", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_2, (result.getTimestamp()));
Assert.assertFalse(iter.hasNext());
}
use of org.apache.druid.query.dimension.DimensionSpec in project druid by druid-io.
the class MovingAverageIterableTest method testMissingDataAtTheEnd.
// test injection when the data is missing at the end
@Test
public void testMissingDataAtTheEnd() {
Map<String, Object> event1 = new HashMap<>();
Map<String, Object> event2 = new HashMap<>();
Map<String, Object> event3 = new HashMap<>();
event1.put("gender", "m");
event1.put("pageViews", 10L);
event2.put("gender", "f");
event2.put("pageViews", 20L);
event3.put("gender", "u");
event3.put("pageViews", 30L);
List<DimensionSpec> ds = new ArrayList<>();
ds.add(new DefaultDimensionSpec("gender", "gender"));
Row jan1Row1 = new MapBasedRow(JAN_1, event1);
Row jan1Row2 = new MapBasedRow(JAN_1, event2);
Row jan1Row3 = new MapBasedRow(JAN_1, event3);
Row jan2Row1 = new MapBasedRow(JAN_2, event1);
Sequence<RowBucket> seq = Sequences.simple(Arrays.asList(new RowBucket(JAN_1, Arrays.asList(jan1Row1, jan1Row2, jan1Row3)), new RowBucket(JAN_2, Collections.singletonList(jan2Row1))));
Iterator<Row> iter = new MovingAverageIterable(seq, ds, Collections.singletonList(new LongMeanAveragerFactory("movingAvgPageViews", 2, 1, "pageViews")), Collections.emptyList(), Collections.singletonList(new LongSumAggregatorFactory("pageViews", "pageViews"))).iterator();
Assert.assertTrue(iter.hasNext());
Row result = iter.next();
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_1, (result.getTimestamp()));
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("f", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_1, (result.getTimestamp()));
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("u", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_1, (result.getTimestamp()));
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_2, (result.getTimestamp()));
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("u", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_2, (result.getTimestamp()));
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("f", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_2, (result.getTimestamp()));
Assert.assertFalse(iter.hasNext());
}
use of org.apache.druid.query.dimension.DimensionSpec 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());
}
use of org.apache.druid.query.dimension.DimensionSpec in project druid by druid-io.
the class MovingAverageIterableTest method testMissingDataAtBeginning.
// no injection if the data missing at the begining
@Test
public void testMissingDataAtBeginning() {
Map<String, Object> event1 = new HashMap<>();
Map<String, Object> event2 = new HashMap<>();
Map<String, Object> event3 = new HashMap<>();
event1.put("gender", "m");
event1.put("pageViews", 10L);
event2.put("gender", "f");
event2.put("pageViews", 20L);
event3.put("gender", "u");
event3.put("pageViews", 30L);
List<DimensionSpec> ds = new ArrayList<>();
ds.add(new DefaultDimensionSpec("gender", "gender"));
Row jan1Row1 = new MapBasedRow(JAN_1, event1);
Row jan2Row1 = new MapBasedRow(JAN_2, event1);
Row jan2Row2 = new MapBasedRow(JAN_2, event2);
Row jan2Row3 = new MapBasedRow(JAN_2, event3);
Sequence<RowBucket> seq = Sequences.simple(Arrays.asList(new RowBucket(JAN_1, Collections.singletonList(jan1Row1)), new RowBucket(JAN_2, Arrays.asList(jan2Row1, jan2Row2, jan2Row3))));
Iterator<Row> iter = new MovingAverageIterable(seq, ds, Collections.singletonList(new LongMeanAveragerFactory("movingAvgPageViews", 2, 1, "pageViews")), Collections.emptyList(), Collections.singletonList(new LongSumAggregatorFactory("pageViews", "pageViews"))).iterator();
Assert.assertTrue(iter.hasNext());
Row result = iter.next();
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_1, (result.getTimestamp()));
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("m", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_2, (result.getTimestamp()));
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("f", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_2, (result.getTimestamp()));
Assert.assertTrue(iter.hasNext());
result = iter.next();
Assert.assertEquals("u", (result.getDimension("gender")).get(0));
Assert.assertEquals(JAN_2, (result.getTimestamp()));
Assert.assertFalse(iter.hasNext());
}
Aggregations