use of org.apache.apex.malhar.lib.util.KeyValPair in project apex-malhar by apache.
the class KeyPairToHashMapTest method testNodeProcessing.
/**
* Test oper pass through. The Object passed is not relevant
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testNodeProcessing() throws Exception {
KeyValPairToHashMap oper = new KeyValPairToHashMap();
CountTestSink mapSink = new CountTestSink();
oper.map.setSink(mapSink);
oper.beginWindow(0);
KeyValPair<String, String> input = new KeyValPair<String, String>("a", "1");
// Same input object can be used as the oper is just pass through
int numtuples = 1000;
for (int i = 0; i < numtuples; i++) {
oper.keyval.process(input);
}
oper.endWindow();
Assert.assertEquals("number emitted tuples", numtuples, mapSink.count);
}
use of org.apache.apex.malhar.lib.util.KeyValPair in project apex-malhar by apache.
the class MqttInputOperatorTest method testInputOperator.
@Test
public void testInputOperator() throws InterruptedException, Exception {
String host = "localhost";
int port = 1883;
MqttClientConfig config = new MqttClientConfig();
config.setHost(host);
config.setPort(port);
config.setCleanSession(true);
LocalMode lma = LocalMode.newInstance();
DAG dag = lma.getDAG();
final TestMqttInputOperator input = dag.addOperator("input", TestMqttInputOperator.class);
CollectorModule<KeyValPair<String, String>> collector = dag.addOperator("collector", new CollectorModule<KeyValPair<String, String>>());
input.addSubscribeTopic("a", QoS.AT_MOST_ONCE);
input.addSubscribeTopic("b", QoS.AT_MOST_ONCE);
input.addSubscribeTopic("c", QoS.AT_MOST_ONCE);
input.setMqttClientConfig(config);
input.setup(null);
dag.addStream("stream", input.outputPort, collector.inputPort);
final LocalMode.Controller lc = lma.getController();
lc.runAsync();
long timeout = System.currentTimeMillis() + 3000;
while (true) {
if (activated) {
break;
}
Assert.assertTrue("Activation timeout", timeout > System.currentTimeMillis());
Thread.sleep(1000);
}
input.activate(null);
// Thread.sleep(3000);
input.generateData();
long timeout1 = System.currentTimeMillis() + 5000;
try {
while (true) {
if (resultCount == 0) {
Thread.sleep(10);
Assert.assertTrue("timeout without getting any data", System.currentTimeMillis() < timeout1);
} else {
break;
}
}
} catch (InterruptedException ex) {
// ignore
}
lc.shutdown();
Assert.assertEquals("Number of emitted tuples", 3, resultMap.size());
Assert.assertEquals("value of a is ", "10", resultMap.get("a"));
Assert.assertEquals("value of b is ", "200", resultMap.get("b"));
Assert.assertEquals("value of c is ", "3000", resultMap.get("c"));
logger.debug("resultCount:" + resultCount);
}
use of org.apache.apex.malhar.lib.util.KeyValPair in project apex-malhar by apache.
the class ChangeAlertTest method testNodeProcessingSchema.
@SuppressWarnings({ "rawtypes", "unchecked" })
public <V extends Number> void testNodeProcessingSchema(ChangeAlert<V> oper) {
CollectorTestSink alertSink = new CollectorTestSink();
oper.alert.setSink(alertSink);
oper.setPercentThreshold(5);
oper.beginWindow(0);
oper.data.process(oper.getValue(10));
// alert
oper.data.process(oper.getValue(12));
oper.data.process(oper.getValue(12));
// alert
oper.data.process(oper.getValue(18));
// alert
oper.data.process(oper.getValue(0));
// this will not alert
oper.data.process(oper.getValue(20));
// alert
oper.data.process(oper.getValue(30));
oper.endWindow();
// One for a, Two for b
Assert.assertEquals("number emitted tuples", 4, alertSink.collectedTuples.size());
double aval = 0;
log.debug("\nLogging tuples");
for (Object o : alertSink.collectedTuples) {
KeyValPair<Number, Double> map = (KeyValPair<Number, Double>) o;
log.debug(o.toString());
aval += map.getValue().doubleValue();
}
Assert.assertEquals("change in a", 220.0, aval, 0);
}
use of org.apache.apex.malhar.lib.util.KeyValPair in project apex-malhar by apache.
the class SeedEventGenerator method emitTuple.
/**
* Inserts a tuple for a given outbound key
*
* @param i
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public void emitTuple(int i) {
HashMap<String, String> stuple;
HashMap<String, ArrayList<KeyValPair>> atuple;
String key = Integer.toString(i);
if (keys == null) {
if (string_data.isConnected()) {
stuple = new HashMap<String, String>(1);
stuple.put(key, null);
string_data.emit(stuple);
}
if (keyvalpair_list.isConnected()) {
atuple = new HashMap<String, ArrayList<KeyValPair>>(1);
atuple.put(key, null);
keyvalpair_list.emit(atuple);
}
return;
}
ArrayList<KeyValPair> alist = null;
ArrayList<Integer> vlist = null;
String str = new String();
String vstr = new String();
boolean iskv = keyvalpair_list.isConnected();
boolean isvl = val_list.isConnected();
boolean issd = string_data.isConnected();
boolean isvd = val_data.isConnected();
int j = 0;
for (String s : keys) {
if (iskv) {
if (alist == null) {
alist = new ArrayList<KeyValPair>(keys.size());
}
alist.add(new KeyValPair<String, Integer>(s, new Integer(keys_min.get(j) + random.nextInt(keys_range.get(j)))));
}
if (isvl) {
if (vlist == null) {
vlist = new ArrayList<Integer>(keys.size());
}
vlist.add(new Integer(keys_min.get(j) + random.nextInt(keys_range.get(j))));
}
if (issd) {
if (!str.isEmpty()) {
str += ';';
}
str += s + ":" + Integer.toString(keys_min.get(j) + random.nextInt(keys_range.get(j)));
}
if (isvd) {
if (!vstr.isEmpty()) {
vstr += ';';
}
vstr += Integer.toString(keys_min.get(j) + random.nextInt(keys_range.get(j)));
}
j++;
}
if (iskv) {
atuple = new HashMap<String, ArrayList<KeyValPair>>(1);
atuple.put(key, alist);
keyvalpair_list.emit(atuple);
}
if (isvl) {
HashMap<String, ArrayList<Integer>> ituple = new HashMap<String, ArrayList<Integer>>(1);
ituple.put(key, vlist);
val_list.emit(ituple);
}
if (issd) {
stuple = new HashMap<String, String>(1);
stuple.put(key, str);
string_data.emit(stuple);
}
if (isvd) {
HashMap vtuple = new HashMap<String, String>(1);
vtuple.put(key, vstr);
val_data.emit(vtuple);
}
}
use of org.apache.apex.malhar.lib.util.KeyValPair in project apex-malhar by apache.
the class MultiWindowDimensionAggregation method beginWindow.
@Override
public void beginWindow(long arg0) {
Map<String, Map<String, Number>> currentWindowMap = cacheOject.get(currentWindow);
if (currentWindowMap == null) {
currentWindowMap = new HashMap<String, Map<String, Number>>();
} else {
for (Map.Entry<String, Map<String, Number>> tupleEntry : currentWindowMap.entrySet()) {
String tupleKey = tupleEntry.getKey();
Map<String, Number> tupleValue = tupleEntry.getValue();
int currentPattern = 0;
for (Pattern pattern : patternList) {
Matcher matcher = pattern.matcher(tupleKey);
if (matcher.matches()) {
String currentPatternString = dimensionArrayString.get(currentPattern);
Map<String, KeyValPair<MutableDouble, Integer>> currentPatternMap = outputMap.get(currentPatternString);
if (currentPatternMap != null) {
StringBuilder builder = new StringBuilder(matcher.group(2));
for (int i = 1; i < dimensionArray.get(currentPattern).length; i++) {
builder.append("," + matcher.group(i + 2));
}
KeyValPair<MutableDouble, Integer> currentDimensionKeyValPair = currentPatternMap.get(builder.toString());
if (currentDimensionKeyValPair != null) {
currentDimensionKeyValPair.getKey().add(0 - tupleValue.get(dimensionKeyVal).doubleValue());
currentDimensionKeyValPair.setValue(currentDimensionKeyValPair.getValue() - 1);
if (currentDimensionKeyValPair.getKey().doubleValue() == 0.0) {
currentPatternMap.remove(builder.toString());
}
}
}
break;
}
currentPattern++;
}
}
}
currentWindowMap.clear();
if (patternList == null || patternList.isEmpty()) {
setUpPatternList();
}
}
Aggregations