Search in sources :

Example 16 with KeyValPair

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);
}
Also used : CountTestSink(org.apache.apex.malhar.lib.testbench.CountTestSink) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) Test(org.junit.Test)

Example 17 with KeyValPair

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);
}
Also used : LocalMode(com.datatorrent.api.LocalMode) DAG(com.datatorrent.api.DAG) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) Test(org.junit.Test)

Example 18 with KeyValPair

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);
}
Also used : KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink)

Example 19 with KeyValPair

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);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair)

Example 20 with KeyValPair

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();
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

KeyValPair (org.apache.apex.malhar.lib.util.KeyValPair)42 Test (org.junit.Test)16 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)15 Map (java.util.Map)10 HashMap (java.util.HashMap)9 ArrayList (java.util.ArrayList)4 List (java.util.List)4 WindowOption (org.apache.apex.malhar.lib.window.WindowOption)4 LocalMode (com.datatorrent.api.LocalMode)3 Calendar (java.util.Calendar)3 Date (java.util.Date)3 MachineInfo (org.apache.apex.examples.machinedata.data.MachineInfo)3 MachineKey (org.apache.apex.examples.machinedata.data.MachineKey)3 ResourceType (org.apache.apex.examples.machinedata.data.ResourceType)3 Function (org.apache.apex.malhar.lib.function.Function)3 TimeBucketKey (org.apache.apex.malhar.lib.util.TimeBucketKey)3 TriggerOption (org.apache.apex.malhar.lib.window.TriggerOption)3 MutableDouble (org.apache.commons.lang.mutable.MutableDouble)3 DAG (com.datatorrent.api.DAG)2 Multimap (com.google.common.collect.Multimap)2