use of org.apache.apex.malhar.lib.streamquery.index.ColumnIndex in project apex-malhar by apache.
the class GroupByOperatorTest method testSqlGroupBy.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testSqlGroupBy() {
// create operator
GroupByHavingOperator oper = new GroupByHavingOperator();
oper.addColumnGroupByIndex(new ColumnIndex("b", null));
try {
oper.addAggregateIndex(new SumFunction("c", null));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
EqualValueCondition condition = new EqualValueCondition();
condition.addEqualValue("a", 1);
oper.setCondition(condition);
CollectorTestSink sink = new CollectorTestSink();
oper.outport.setSink(sink);
oper.setup(null);
oper.beginWindow(1);
HashMap<String, Object> tuple = new HashMap<String, Object>();
tuple.put("a", 1);
tuple.put("b", 1);
tuple.put("c", 2);
oper.inport.process(tuple);
tuple = new HashMap<String, Object>();
tuple.put("a", 1);
tuple.put("b", 1);
tuple.put("c", 4);
oper.inport.process(tuple);
tuple = new HashMap<String, Object>();
tuple.put("a", 1);
tuple.put("b", 2);
tuple.put("c", 6);
oper.inport.process(tuple);
tuple = new HashMap<String, Object>();
tuple.put("a", 1);
tuple.put("b", 2);
tuple.put("c", 7);
oper.inport.process(tuple);
oper.endWindow();
oper.teardown();
LOG.debug("{}", sink.collectedTuples);
}
use of org.apache.apex.malhar.lib.streamquery.index.ColumnIndex in project apex-malhar by apache.
the class InnerJoinOperatorTest method testSqlSelect.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testSqlSelect() {
// create operator
InnerJoinOperator oper = new InnerJoinOperator();
CollectorTestSink sink = new CollectorTestSink();
oper.outport.setSink(sink);
// set column join condition
Condition cond = new JoinColumnEqualCondition("a", "a");
oper.setJoinCondition(cond);
// add columns
oper.selectTable1Column(new ColumnIndex("b", null));
oper.selectTable2Column(new ColumnIndex("c", null));
oper.setup(null);
oper.beginWindow(1);
HashMap<String, Object> tuple = new HashMap<String, Object>();
tuple.put("a", 0);
tuple.put("b", 1);
tuple.put("c", 2);
oper.inport1.process(tuple);
tuple = new HashMap<String, Object>();
tuple.put("a", 1);
tuple.put("b", 3);
tuple.put("c", 4);
oper.inport1.process(tuple);
tuple = new HashMap<String, Object>();
tuple.put("a", 0);
tuple.put("b", 7);
tuple.put("c", 8);
oper.inport2.process(tuple);
tuple = new HashMap<String, Object>();
tuple.put("a", 1);
tuple.put("b", 5);
tuple.put("c", 6);
oper.inport2.process(tuple);
oper.endWindow();
oper.teardown();
LOG.debug("{}", sink.collectedTuples);
}
use of org.apache.apex.malhar.lib.streamquery.index.ColumnIndex in project apex-malhar by apache.
the class GroupByHavingOperator method endWindow.
/**
* Create aggregate at end window.
*/
@Override
public void endWindow() {
// group names
if (columnGroupIndexes.size() == 0) {
rows = new ArrayList<Map<String, Object>>();
return;
}
// group rows
HashMap<MultiKeyCompare, ArrayList<Map<String, Object>>> groups = new HashMap<MultiKeyCompare, ArrayList<Map<String, Object>>>();
for (Map<String, Object> row : rows) {
MultiKeyCompare key = new MultiKeyCompare();
for (ColumnIndex index : columnGroupIndexes) {
key.addCompareKey(row.get(index.getColumn()));
}
ArrayList<Map<String, Object>> subRows;
if (groups.containsKey(key)) {
subRows = groups.get(key);
} else {
subRows = new ArrayList<Map<String, Object>>();
groups.put(key, subRows);
}
subRows.add(row);
}
// Iterate over groups and emit aggregate values
for (Map.Entry<MultiKeyCompare, ArrayList<Map<String, Object>>> entry : groups.entrySet()) {
ArrayList<Map<String, Object>> subRows = entry.getValue();
// get result
Map<String, Object> result = new HashMap<String, Object>();
for (ColumnIndex index : columnGroupIndexes) {
index.filter(subRows.get(0), result);
}
// append aggregate values
for (FunctionIndex aggregate : aggregates) {
try {
aggregate.filter(subRows, result);
} catch (Exception e) {
e.printStackTrace();
}
}
// check valid having aggregate
boolean isValidHaving = true;
for (HavingCondition condition : havingConditions) {
try {
isValidHaving &= condition.isValidAggregate(subRows);
} catch (Exception e) {
e.printStackTrace();
return;
}
}
if (isValidHaving) {
outport.emit(result);
}
}
rows = new ArrayList<Map<String, Object>>();
}
use of org.apache.apex.malhar.lib.streamquery.index.ColumnIndex in project apex-malhar by apache.
the class BetweenConditionTest method testSqlSelect.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testSqlSelect() {
// create operator
SelectOperator oper = new SelectOperator();
oper.addIndex(new ColumnIndex("b", null));
oper.addIndex(new ColumnIndex("c", null));
BetweenCondition cond = new BetweenCondition("a", 0, 2);
oper.setCondition(cond);
CollectorTestSink sink = new CollectorTestSink();
oper.outport.setSink(sink);
oper.setup(null);
oper.beginWindow(1);
HashMap<String, Object> tuple = new HashMap<String, Object>();
tuple.put("a", 0);
tuple.put("b", 1);
tuple.put("c", 2);
oper.inport.process(tuple);
tuple = new HashMap<String, Object>();
tuple.put("a", 1);
tuple.put("b", 3);
tuple.put("c", 4);
oper.inport.process(tuple);
tuple = new HashMap<String, Object>();
tuple.put("a", 2);
tuple.put("b", 5);
tuple.put("c", 6);
oper.inport.process(tuple);
tuple = new HashMap<String, Object>();
tuple.put("a", 3);
tuple.put("b", 7);
tuple.put("c", 8);
oper.inport.process(tuple);
oper.endWindow();
oper.teardown();
LOG.debug("{}", sink.collectedTuples);
}
use of org.apache.apex.malhar.lib.streamquery.index.ColumnIndex in project apex-malhar by apache.
the class InConditionTest method testSqlSelect.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testSqlSelect() {
// create operator
SelectOperator oper = new SelectOperator();
oper.addIndex(new ColumnIndex("b", null));
oper.addIndex(new ColumnIndex("c", null));
InCondition cond = new InCondition("a");
cond.addInValue(0);
cond.addInValue(1);
oper.setCondition(cond);
CollectorTestSink sink = new CollectorTestSink();
oper.outport.setSink(sink);
oper.setup(null);
oper.beginWindow(1);
HashMap<String, Object> tuple = new HashMap<String, Object>();
tuple.put("a", 0);
tuple.put("b", 1);
tuple.put("c", 2);
oper.inport.process(tuple);
tuple = new HashMap<String, Object>();
tuple.put("a", 1);
tuple.put("b", 3);
tuple.put("c", 4);
oper.inport.process(tuple);
tuple = new HashMap<String, Object>();
tuple.put("a", 2);
tuple.put("b", 5);
tuple.put("c", 6);
oper.inport.process(tuple);
tuple = new HashMap<String, Object>();
tuple.put("a", 3);
tuple.put("b", 7);
tuple.put("c", 8);
oper.inport.process(tuple);
oper.endWindow();
oper.teardown();
LOG.debug("{}", sink.collectedTuples);
}
Aggregations