Search in sources :

Example 36 with KeyValPair

use of org.apache.apex.malhar.lib.util.KeyValPair in project apex-malhar by apache.

the class AverageKeyValTest method testNodeProcessing.

/**
 * Test operator logic emits correct results.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testNodeProcessing() {
    AverageKeyVal<String> oper = new AverageKeyVal<String>();
    CollectorTestSink averageSink = new CollectorTestSink();
    oper.doubleAverage.setSink(averageSink);
    // 
    oper.beginWindow(0);
    oper.data.process(new KeyValPair("a", 2.0));
    oper.data.process(new KeyValPair("b", 20.0));
    oper.data.process(new KeyValPair("c", 1000.0));
    oper.data.process(new KeyValPair("a", 1.0));
    oper.data.process(new KeyValPair("a", 10.0));
    oper.data.process(new KeyValPair("b", 5.0));
    oper.data.process(new KeyValPair("d", 55.0));
    oper.data.process(new KeyValPair("b", 12.0));
    oper.data.process(new KeyValPair("d", 22.0));
    oper.data.process(new KeyValPair("d", 14.2));
    oper.data.process(new KeyValPair("d", 46.0));
    oper.data.process(new KeyValPair("e", 2.0));
    oper.data.process(new KeyValPair("a", 23.0));
    oper.data.process(new KeyValPair("d", 4.0));
    // 
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 5, averageSink.collectedTuples.size());
    for (Object o : averageSink.collectedTuples) {
        KeyValPair<String, Double> e = (KeyValPair<String, Double>) o;
        Double val = e.getValue();
        if (e.getKey().equals("a")) {
            Assert.assertEquals("emitted value for 'a' was ", new Double(36 / 4.0), val);
        } else if (e.getKey().equals("b")) {
            Assert.assertEquals("emitted tuple for 'b' was ", new Double(37 / 3.0), val);
        } else if (e.getKey().equals("c")) {
            Assert.assertEquals("emitted tuple for 'c' was ", new Double(1000 / 1.0), val);
        } else if (e.getKey().equals("d")) {
            Assert.assertEquals("emitted tuple for 'd' was ", new Double(141.2 / 5), val);
        } else if (e.getKey().equals("e")) {
            Assert.assertEquals("emitted tuple for 'e' was ", new Double(2 / 1.0), val);
        }
    }
}
Also used : KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Example 37 with KeyValPair

use of org.apache.apex.malhar.lib.util.KeyValPair in project apex-malhar by apache.

the class MarginKeyValTest method testNodeProcessingSchema.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void testNodeProcessingSchema(MarginKeyVal oper) {
    CollectorTestSink marginSink = new CollectorTestSink();
    oper.margin.setSink(marginSink);
    oper.beginWindow(0);
    oper.numerator.process(new KeyValPair("a", 2));
    oper.numerator.process(new KeyValPair("b", 20));
    oper.numerator.process(new KeyValPair("c", 1000));
    oper.denominator.process(new KeyValPair("a", 2));
    oper.denominator.process(new KeyValPair("b", 40));
    oper.denominator.process(new KeyValPair("c", 500));
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 3, marginSink.collectedTuples.size());
    for (int i = 0; i < marginSink.collectedTuples.size(); i++) {
        if ("a".equals(((KeyValPair<String, Number>) marginSink.collectedTuples.get(i)).getKey())) {
            Assert.assertEquals("emitted value for 'a' was ", 0d, ((KeyValPair<String, Number>) marginSink.collectedTuples.get(i)).getValue().doubleValue(), 0);
        }
        if ("b".equals(((KeyValPair<String, Number>) marginSink.collectedTuples.get(i)).getKey())) {
            Assert.assertEquals("emitted value for 'b' was ", 0.5, ((KeyValPair<String, Number>) marginSink.collectedTuples.get(i)).getValue().doubleValue(), 0);
        }
        if ("c".equals(((KeyValPair<String, Number>) marginSink.collectedTuples.get(i)).getKey())) {
            Assert.assertEquals("emitted value for 'c' was ", (double) -1, ((KeyValPair<String, Number>) marginSink.collectedTuples.get(i)).getValue().doubleValue(), 0);
        }
    }
}
Also used : KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink)

Example 38 with KeyValPair

use of org.apache.apex.malhar.lib.util.KeyValPair in project apex-malhar by apache.

the class MultiWindowRangeKeyValTest method testNodeProcessing.

/**
 * Test functional logic
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testNodeProcessing() throws InterruptedException {
    MultiWindowRangeKeyVal<String, Integer> oper = new MultiWindowRangeKeyVal<String, Integer>();
    CollectorTestSink swinSink = new CollectorTestSink();
    oper.range.setSink(swinSink);
    oper.beginWindow(0);
    KeyValPair<String, Integer> low = new KeyValPair<String, Integer>("a", 3);
    oper.data.process(low);
    KeyValPair<String, Integer> high = new KeyValPair<String, Integer>("a", 11);
    oper.data.process(high);
    oper.endWindow();
    oper.beginWindow(1);
    low = new KeyValPair<String, Integer>("a", 1);
    oper.data.process(low);
    high = new KeyValPair<String, Integer>("a", 9);
    oper.data.process(high);
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, swinSink.collectedTuples.size());
    for (Object o : swinSink.collectedTuples) {
        log.debug(o.toString());
    }
}
Also used : KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Example 39 with KeyValPair

use of org.apache.apex.malhar.lib.util.KeyValPair in project apex-malhar by apache.

the class SimpleMovingAverageTest method testNodeProcessing.

/**
 * Test functional logic
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testNodeProcessing() throws InterruptedException {
    SimpleMovingAverage<String, Double> oper = new SimpleMovingAverage<String, Double>();
    CollectorTestSink sink = new CollectorTestSink();
    CollectorTestSink sink2 = new CollectorTestSink();
    oper.doubleSMA.setSink(sink);
    oper.integerSMA.setSink(sink2);
    oper.setWindowSize(3);
    double val = 30;
    double val2 = 51;
    oper.beginWindow(0);
    oper.data.process(new KeyValPair<String, Double>("a", ++val));
    oper.data.process(new KeyValPair<String, Double>("a", ++val));
    oper.data.process(new KeyValPair<String, Double>("b", ++val2));
    oper.data.process(new KeyValPair<String, Double>("b", ++val2));
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 2, sink.collectedTuples.size());
    for (int i = 0; i < 2; i++) {
        KeyValPair<String, Double> pair = (KeyValPair<String, Double>) sink.collectedTuples.get(i);
        if (pair.getKey().equals("a")) {
            Assert.assertEquals("a SMA", 31.5, pair.getValue(), 0);
        } else {
            Assert.assertEquals("b SMA", 52.5, pair.getValue(), 0);
        }
    }
    oper.beginWindow(1);
    oper.data.process(new KeyValPair<String, Double>("a", ++val));
    oper.data.process(new KeyValPair<String, Double>("a", ++val));
    oper.data.process(new KeyValPair<String, Double>("b", ++val2));
    oper.data.process(new KeyValPair<String, Double>("b", ++val2));
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 4, sink.collectedTuples.size());
    for (int i = 2; i < 4; i++) {
        KeyValPair<String, Double> pair = (KeyValPair<String, Double>) sink.collectedTuples.get(i);
        if (pair.getKey().equals("a")) {
            Assert.assertEquals("a SMA", 32.5, pair.getValue(), 0);
        } else {
            Assert.assertEquals("b SMA", 53.5, pair.getValue(), 0);
        }
    }
    oper.beginWindow(2);
    oper.data.process(new KeyValPair<String, Double>("a", ++val));
    oper.data.process(new KeyValPair<String, Double>("a", ++val));
    oper.data.process(new KeyValPair<String, Double>("b", ++val2));
    oper.data.process(new KeyValPair<String, Double>("b", ++val2));
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 6, sink.collectedTuples.size());
    for (int i = 4; i < 6; i++) {
        KeyValPair<String, Double> pair = (KeyValPair<String, Double>) sink.collectedTuples.get(i);
        if (pair.getKey().equals("a")) {
            Assert.assertEquals("a SMA", 33.5, pair.getValue(), 0);
        } else {
            Assert.assertEquals("b SMA", 54.5, pair.getValue(), 0);
        }
    }
    oper.beginWindow(3);
    oper.data.process(new KeyValPair<String, Double>("a", ++val));
    oper.data.process(new KeyValPair<String, Double>("a", ++val));
    oper.data.process(new KeyValPair<String, Double>("b", ++val2));
    oper.data.process(new KeyValPair<String, Double>("b", ++val2));
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 8, sink.collectedTuples.size());
    for (int i = 6; i < 8; i++) {
        KeyValPair<String, Double> pair = (KeyValPair<String, Double>) sink.collectedTuples.get(i);
        if (pair.getKey().equals("a")) {
            Assert.assertEquals("a SMA", 35.5, pair.getValue(), 0);
        } else {
            Assert.assertEquals("b SMA", 56.5, pair.getValue(), 0);
        }
    }
}
Also used : KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Example 40 with KeyValPair

use of org.apache.apex.malhar.lib.util.KeyValPair in project apex-malhar by apache.

the class JsonParser method processTuple.

@Override
public void processTuple(byte[] tuple) {
    if (tuple == null) {
        if (err.isConnected()) {
            err.emit(new KeyValPair<String, String>(null, "null tuple"));
        }
        errorTupleCount++;
        return;
    }
    String incomingString = new String(tuple);
    try {
        if (schema != null) {
            ProcessingReport report = null;
            JsonNode data = JsonLoader.fromString(incomingString);
            report = schema.validate(data);
            if (report != null && !report.isSuccess()) {
                Iterator<ProcessingMessage> iter = report.iterator();
                StringBuilder s = new StringBuilder();
                while (iter.hasNext()) {
                    ProcessingMessage pm = iter.next();
                    s.append(pm.asJson().get("instance").findValue("pointer")).append(":").append(pm.asJson().get("message")).append(",");
                }
                s.setLength(s.length() - 1);
                errorTupleCount++;
                if (err.isConnected()) {
                    err.emit(new KeyValPair<String, String>(incomingString, s.toString()));
                }
                return;
            }
        }
        if (parsedOutput.isConnected()) {
            parsedOutput.emit(new JSONObject(incomingString));
            parsedOutputCount++;
        }
        if (out.isConnected()) {
            out.emit(objMapper.readValue(tuple, clazz));
            emittedObjectCount++;
        }
    } catch (JSONException | ProcessingException | IOException e) {
        errorTupleCount++;
        if (err.isConnected()) {
            err.emit(new KeyValPair<String, String>(incomingString, e.getMessage()));
        }
        logger.error("Failed to parse json tuple {}, Exception = {} ", tuple, e);
    }
}
Also used : ProcessingMessage(com.github.fge.jsonschema.core.report.ProcessingMessage) JSONException(org.codehaus.jettison.json.JSONException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JSONObject(org.codehaus.jettison.json.JSONObject) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException)

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