Search in sources :

Example 11 with KeyValPair

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

the class CalculatorOperatorTest method testStandarDeviation.

public void testStandarDeviation(CalculatorOperator oper) {
    CollectorTestSink sortSink = new CollectorTestSink();
    oper.sdOutputPort.setSink(sortSink);
    Calendar calendar = Calendar.getInstance();
    Date date = calendar.getTime();
    String timeKey = minuteDateFormat.format(date);
    String day = calendar.get(Calendar.DAY_OF_MONTH) + "";
    Integer vs = new Integer(1);
    MachineKey mk = new MachineKey(timeKey, day, vs, vs, vs, vs, vs, vs, vs);
    oper.beginWindow(0);
    MachineInfo info = new MachineInfo(mk, 1, 1, 1);
    oper.dataPort.process(info);
    info.setCpu(2);
    oper.dataPort.process(info);
    info.setCpu(3);
    oper.dataPort.process(info);
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, sortSink.collectedTuples.size());
    for (Object o : sortSink.collectedTuples) {
        LOG.debug(o.toString());
        KeyValPair<TimeBucketKey, Map<ResourceType, Double>> keyValPair = (KeyValPair<TimeBucketKey, Map<ResourceType, Double>>) o;
        Assert.assertEquals("emitted value for 'cpu' was ", getSD(ImmutableList.of(1, 2, 3)), keyValPair.getValue().get(ResourceType.CPU), 0);
        Assert.assertEquals("emitted value for 'hdd' was ", getSD(ImmutableList.of(1, 1, 1)), keyValPair.getValue().get(ResourceType.HDD), 0);
        Assert.assertEquals("emitted value for 'ram' was ", getSD(ImmutableList.of(1, 1, 1)), keyValPair.getValue().get(ResourceType.RAM), 0);
    }
    LOG.debug("Done sd testing\n");
}
Also used : Calendar(java.util.Calendar) ResourceType(org.apache.apex.examples.machinedata.data.ResourceType) Date(java.util.Date) TimeBucketKey(org.apache.apex.malhar.lib.util.TimeBucketKey) MachineInfo(org.apache.apex.examples.machinedata.data.MachineInfo) MachineKey(org.apache.apex.examples.machinedata.data.MachineKey) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) Map(java.util.Map) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink)

Example 12 with KeyValPair

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

the class CalculatorOperatorTest method testPercentile.

public void testPercentile(CalculatorOperator oper) {
    CollectorTestSink sortSink = new CollectorTestSink();
    oper.percentileOutputPort.setSink(sortSink);
    oper.setKthPercentile(50);
    Calendar calendar = Calendar.getInstance();
    Date date = calendar.getTime();
    String timeKey = minuteDateFormat.format(date);
    String day = calendar.get(Calendar.DAY_OF_MONTH) + "";
    Integer vs = new Integer(1);
    MachineKey mk = new MachineKey(timeKey, day, vs, vs, vs, vs, vs, vs, vs);
    oper.beginWindow(0);
    MachineInfo info = new MachineInfo(mk, 1, 1, 1);
    oper.dataPort.process(info);
    info.setCpu(2);
    oper.dataPort.process(info);
    info.setCpu(3);
    oper.dataPort.process(info);
    oper.endWindow();
    Assert.assertEquals("number emitted tuples", 1, sortSink.collectedTuples.size());
    for (Object o : sortSink.collectedTuples) {
        LOG.debug(o.toString());
        KeyValPair<TimeBucketKey, Map<ResourceType, Double>> keyValPair = (KeyValPair<TimeBucketKey, Map<ResourceType, Double>>) o;
        Assert.assertEquals("emitted value for 'cpu' was ", 2.0, keyValPair.getValue().get(ResourceType.CPU), 0);
        Assert.assertEquals("emitted value for 'hdd' was ", 1.0, keyValPair.getValue().get(ResourceType.HDD), 0);
        Assert.assertEquals("emitted value for 'ram' was ", 1.0, keyValPair.getValue().get(ResourceType.RAM), 0);
    }
    LOG.debug("Done percentile testing\n");
}
Also used : Calendar(java.util.Calendar) ResourceType(org.apache.apex.examples.machinedata.data.ResourceType) Date(java.util.Date) TimeBucketKey(org.apache.apex.malhar.lib.util.TimeBucketKey) MachineInfo(org.apache.apex.examples.machinedata.data.MachineInfo) MachineKey(org.apache.apex.examples.machinedata.data.MachineKey) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) Map(java.util.Map) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink)

Example 13 with KeyValPair

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

the class PojoOuterJoinTest method PojoLeftOuterJoinTestWithMap.

@Test
public void PojoLeftOuterJoinTestWithMap() {
    String[] leftKeys = { "uId", "uName" };
    String[] rightKeys = { "uId", "uNickName" };
    Map<String, KeyValPair<AbstractPojoJoin.STREAM, String>> outputInputMap = new HashMap<>();
    outputInputMap.put("uId", new KeyValPair<>(AbstractPojoJoin.STREAM.LEFT, "uId"));
    outputInputMap.put("age", new KeyValPair<>(AbstractPojoJoin.STREAM.RIGHT, "age"));
    PojoLeftOuterJoin<TestPojo1, TestPojo3> pij = new PojoLeftOuterJoin<>(TestOutClass.class, leftKeys, rightKeys, outputInputMap);
    List<Multimap<List<Object>, Object>> accu = pij.defaultAccumulatedValue();
    Assert.assertEquals(2, accu.size());
    accu = pij.accumulate(accu, new TestPojo1(1, "Josh"));
    accu = pij.accumulate(accu, new TestPojo1(2, "Bob"));
    accu = pij.accumulate2(accu, new TestPojo3(1, "Josh", 12));
    accu = pij.accumulate2(accu, new TestPojo3(3, "ECE", 13));
    List result = pij.getOutput(accu);
    Assert.assertEquals(2, result.size());
    Object o = result.get(0);
    Assert.assertTrue(o instanceof TestOutClass);
    TestOutClass testOutClass = (TestOutClass) o;
    int uId = testOutClass.getUId();
    if (uId == 1) {
        checkNameAge(null, 12, testOutClass);
        o = result.get(1);
        Assert.assertTrue(o instanceof TestOutClass);
        testOutClass = (TestOutClass) o;
        uId = testOutClass.getUId();
        Assert.assertEquals(2, uId);
        checkNameAge(null, 0, testOutClass);
    } else if (uId == 2) {
        checkNameAge(null, 0, testOutClass);
        o = result.get(1);
        Assert.assertTrue(o instanceof TestOutClass);
        testOutClass = (TestOutClass) o;
        uId = testOutClass.getUId();
        Assert.assertEquals(1, uId);
        checkNameAge(null, 12, testOutClass);
    }
}
Also used : HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) List(java.util.List) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) Test(org.junit.Test)

Example 14 with KeyValPair

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

the class PojoOuterJoinTest method PojoRightOuterJoinTestWithMap.

@Test
public void PojoRightOuterJoinTestWithMap() {
    String[] leftKeys = { "uId", "uName" };
    String[] rightKeys = { "uId", "uNickName" };
    Map<String, KeyValPair<AbstractPojoJoin.STREAM, String>> outputInputMap = new HashMap<>();
    outputInputMap.put("uId", new KeyValPair<>(AbstractPojoJoin.STREAM.LEFT, "uId"));
    outputInputMap.put("age", new KeyValPair<>(AbstractPojoJoin.STREAM.RIGHT, "age"));
    PojoRightOuterJoin<TestPojo1, TestPojo3> pij = new PojoRightOuterJoin<>(TestOutClass.class, leftKeys, rightKeys, outputInputMap);
    List<Multimap<List<Object>, Object>> accu = pij.defaultAccumulatedValue();
    Assert.assertEquals(2, accu.size());
    accu = pij.accumulate(accu, new TestPojo1(1, "Josh"));
    accu = pij.accumulate(accu, new TestPojo1(2, "Bob"));
    accu = pij.accumulate2(accu, new TestPojo3(1, "Josh", 12));
    accu = pij.accumulate2(accu, new TestPojo3(3, "Bob", 13));
    List result = pij.getOutput(accu);
    Assert.assertEquals(2, result.size());
    Object o = result.get(0);
    Assert.assertTrue(o instanceof TestOutClass);
    TestOutClass testOutClass = (TestOutClass) o;
    int uId = testOutClass.getUId();
    if (uId == 1) {
        checkNameAge(null, 12, testOutClass);
        o = result.get(1);
        Assert.assertTrue(o instanceof TestOutClass);
        testOutClass = (TestOutClass) o;
        uId = testOutClass.getUId();
        Assert.assertEquals(0, uId);
        checkNameAge(null, 13, testOutClass);
    } else if (uId == 0) {
        checkNameAge(null, 13, testOutClass);
        o = result.get(1);
        Assert.assertTrue(o instanceof TestOutClass);
        testOutClass = (TestOutClass) o;
        uId = testOutClass.getUId();
        Assert.assertEquals(1, uId);
        checkNameAge(null, 12, testOutClass);
    }
}
Also used : HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) List(java.util.List) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) Test(org.junit.Test)

Example 15 with KeyValPair

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

the class WindowedMergeOperatorTest method keyedWindowedMergeOperatorMergeTest.

@Test
public void keyedWindowedMergeOperatorMergeTest() {
    KeyedWindowedMergeOperatorImpl<String, Integer, Integer, List<Set<Integer>>, List<List<Integer>>> op = createDefaultKeyedWindowedMergeOperator();
    Window global = Window.GlobalWindow.INSTANCE;
    op.setDataStorage(new InMemoryWindowedKeyedStorage<String, List<Set<Integer>>>());
    op.setWindowOption(new WindowOption.GlobalWindow());
    op.initializeWindowStates(AbstractWindowedOperator.GLOBAL_WINDOW_SINGLETON_SET);
    op.processTuple(new Tuple.WindowedTuple<KeyValPair<String, Integer>>(global, new KeyValPair<String, Integer>("A", 100)));
    Assert.assertEquals(1, op.dataStorage.get(global, "A").get(0).size());
    Assert.assertTrue(op.dataStorage.get(global, "A").get(0).contains(100));
    op.processTuple2(new Tuple.WindowedTuple<KeyValPair<String, Integer>>(global, new KeyValPair<String, Integer>("A", 200)));
    Assert.assertEquals(1, op.dataStorage.get(global, "A").get(1).size());
    Assert.assertTrue(op.dataStorage.get(global, "A").get(1).contains(200));
    op.processTuple2(new Tuple.WindowedTuple<KeyValPair<String, Integer>>(global, new KeyValPair<String, Integer>("B", 300)));
    Assert.assertEquals(1, op.dataStorage.get(global, "A").get(1).size());
    Assert.assertEquals(1, op.dataStorage.get(global, "B").get(1).size());
    Assert.assertTrue(op.dataStorage.get(global, "B").get(1).contains(300));
    Assert.assertEquals(2, op.accumulation.getOutput(op.dataStorage.get(global, "A")).size());
}
Also used : Window(org.apache.apex.malhar.lib.window.Window) WindowOption(org.apache.apex.malhar.lib.window.WindowOption) List(java.util.List) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) Tuple(org.apache.apex.malhar.lib.window.Tuple) Test(org.junit.Test)

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