Java Code Examples for io.siddhi.core.event.Event#setData()

The following examples show how to use io.siddhi.core.event.Event#setData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SiddhiPolicyTest.java    From eagle with Apache License 2.0 6 votes vote down vote up
@Test
public void testStrConcat() throws Exception {
    String ql = " define stream log(timestamp long, switchLabel string, port string, message string); " +
        " from log select timestamp, str:concat(switchLabel, '===', port) as alertKey, message insert into output; ";
    SiddhiManager manager = new SiddhiManager();
    SiddhiAppRuntime runtime = manager.createSiddhiAppRuntime(ql);
    runtime.addCallback("output", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
        }
    });

    runtime.start();

    InputHandler logInput = runtime.getInputHandler("log");

    Event e = new Event();
    e.setTimestamp(System.currentTimeMillis());
    e.setData(new Object[] {System.currentTimeMillis(), "switch-ra-slc-01", "port01", "log-message...."});
    logInput.send(e);

    Thread.sleep(1000);
    runtime.shutdown();

}
 
Example 2
Source File: StringSubtractFunctionExtensionTest.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Test
public void testStringSubtract() throws Exception {
    Semaphore semp = new Semaphore(1);
    String ql = " define stream log(timestamp long, switchLabel string, port string, message string); " +
            " from log select str:subtract(switchLabel, message) as alertKey insert into output; ";
    SiddhiManager manager = new SiddhiManager();
    SiddhiAppRuntime runtime = manager.createSiddhiAppRuntime(ql);
    runtime.addCallback("output", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            Assert.assertTrue(events.length == 1);
            Assert.assertTrue(events[0].getData(0).toString().equals("a\nc\ne"));
            semp.release();
        }
    });

    runtime.start();

    InputHandler logInput = runtime.getInputHandler("log");
    semp.acquire();
    Event e = new Event();
    e.setTimestamp(System.currentTimeMillis());
    String ths = "[\"a\", \"b\", \"c\", \"d\", \"e\"]";
    String rhs = "[\"b\", \"d\"]";
    e.setData(new Object[] {System.currentTimeMillis(), ths, "port01", rhs});
    logInput.send(e);

    semp.acquire();
    runtime.shutdown();

}
 
Example 3
Source File: StringListSizeFunctionExtensionTest.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Test
public void testStringListSize() throws Exception {
    Semaphore semp = new Semaphore(1);
    String ql = " define stream log(timestamp long, switchLabel string, port string, message string); " +
            " from log select str:listSize(switchLabel) as alertKey insert into output; ";
    SiddhiManager manager = new SiddhiManager();
    SiddhiAppRuntime runtime = manager.createSiddhiAppRuntime(ql);
    runtime.addCallback("output", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            Assert.assertTrue(events.length == 1);
            Assert.assertTrue(Integer.parseInt(events[0].getData(0).toString()) == 5);
            semp.release();
        }
    });

    runtime.start();

    InputHandler logInput = runtime.getInputHandler("log");
    semp.acquire();
    Event e = new Event();
    e.setTimestamp(System.currentTimeMillis());
    String ths = "[\"a\", \"b\", \"c\", \"d\", \"e\"]";
    String rhs = "[\"b\", \"d\"]";
    e.setData(new Object[] {System.currentTimeMillis(), ths, "port01", rhs});
    logInput.send(e);

    semp.acquire();
    runtime.shutdown();

}
 
Example 4
Source File: JsonSinkMapperTestCase.java    From siddhi-map-json with Apache License 2.0 4 votes vote down vote up
@Test
public void jsonSinkMapperTestCase1() throws InterruptedException {
    log.info("JsonSinkMapperTestCase 1");
    InMemoryBroker.Subscriber subscriberWSO2 = new InMemoryBroker.Subscriber() {
        @Override
        public void onMessage(Object msg) {
            String jsonString;
            switch (wso2Count.incrementAndGet()) {
                case 1:
                    jsonString = "{\"event\":{\"symbol\":\"WSO2\",\"price\":55.6,\"volume\":100}}";
                    AssertJUnit.assertEquals(jsonString , msg);
                    break;
                case 2:
                    jsonString = "{\"event\":{\"symbol\":\"WSO2\",\"price\":57.678,\"volume\":100}}";
                    AssertJUnit.assertEquals(jsonString, msg);
                    break;
                case 3:
                    jsonString = "{\"event\":{\"symbol\":\"WSO2\",\"price\":50.0,\"volume\":100}}";
                    AssertJUnit.assertEquals(jsonString , msg);
                    break;
                case 4:
                    jsonString = "{\"event\":{\"symbol\":\"WSO2#$%\",\"price\":50.0,\"volume\":100}}";
                    AssertJUnit.assertEquals(jsonString , msg);
                    break;
                case 5:
                    jsonString = "[{\"event\":{\"symbol\":\"WSO2\",\"price\":55.6,\"volume\":100}}," +
                            "{\"event\":{\"symbol\":\"IBM\",\"price\":32.6,\"volume\":160}}]";
                    AssertJUnit.assertEquals(jsonString , msg);
                    break;
                default:
                    AssertJUnit.fail();
            }
        }
        @Override
        public String getTopic() {
            return "WSO2";
        }
    };
    //subscribe to "inMemory" broker per topic
    InMemoryBroker.subscribe(subscriberWSO2);
    String streams = "" +
            "@App:name('TestSiddhiApp')" +
            "define stream FooStream (symbol string, price float, volume long); " +
            "@sink(type='inMemory', topic='WSO2', @map(type='json')) " +
            "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" +
            "from FooStream " +
            "select * " +
            "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("FooStream");
    siddhiAppRuntime.start();
    Event wso2Event = new Event();
    Event ibmEvent = new Event();
    Object[] wso2Data = {"WSO2", 55.6f, 100L};
    Object[] ibmData = {"IBM", 32.6f, 160L};
    wso2Event.setData(wso2Data);
    ibmEvent.setData(ibmData);
    stockStream.send(new Object[]{"WSO2", 55.6f, 100L});
    stockStream.send(new Object[]{"WSO2", 57.678f, 100L});
    stockStream.send(new Object[]{"WSO2", 50f, 100L});
    stockStream.send(new Object[]{"WSO2#$%", 50f, 100L});
    stockStream.send(new Event[]{wso2Event, ibmEvent});
    SiddhiTestHelper.waitForEvents(waitTime, 5, wso2Count, timeout);
    //assert event count
    AssertJUnit.assertEquals(5, wso2Count.get());
    siddhiAppRuntime.shutdown();
    //unsubscribe from "inMemory" broker per topic
    InMemoryBroker.unsubscribe(subscriberWSO2);
}
 
Example 5
Source File: JsonSinkMapperTestCase.java    From siddhi-map-json with Apache License 2.0 4 votes vote down vote up
@Test
public void jsonSinkMapperTestCase12() throws InterruptedException {
    log.info("JsonSinkMapperTestCase with test custom multiple event with out enclose element");
    List<Object> onMessageList = new ArrayList<Object>();
    InMemoryBroker.Subscriber subscriberWSO2 = new InMemoryBroker.Subscriber() {
        @Override
        public void onMessage(Object msg) {
            wso2Count.incrementAndGet();
            onMessageList.add(msg);
        }

        @Override
        public String getTopic() {
            return "WSO2";
        }
    };
    //subscribe to "inMemory" broker per topic
    InMemoryBroker.subscribe(subscriberWSO2);
    String streams = "" + "@App:name('TestSiddhiApp')"
            + "define stream FooStream (symbol string, price float, volume long); "
            + "@sink(type='inMemory', topic='WSO2', @map(type='json',"
            + "@payload(\"\"\"{\n" + "   \"Stock Data\":{\n"
            + "       \"Symbol\":\"{{symbol}}\",\n" + "       \"Price\":{{price}},\n"
            + "       \"Volume\":{{volume}}\n" + "   }\n" + "}\"\"\"))) "
            + "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("FooStream");
    siddhiAppRuntime.start();
    Event wso2Event = new Event();
    Event ibmEvent = new Event();
    Object[] wso2Data = { "WSO2", 55.6f, 100L };
    Object[] ibmData = { "IBM", 32.6f, 160L };
    wso2Event.setData(wso2Data);
    ibmEvent.setData(ibmData);
    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "WSO2", 57.678f, 100L });
    stockStream.send(new Object[] { "WSO2", 50f, 100L });
    stockStream.send(new Object[] { "WSO2#$%", 50f, 100L });
    stockStream.send(new Event[] { wso2Event, ibmEvent });
    SiddhiTestHelper.waitForEvents(waitTime, 5, wso2Count, timeout);
    //assert event count
    AssertJUnit.assertEquals(5, wso2Count.get());
    AssertJUnit.assertEquals("Mapping incorrect!",
            "{\n" +
                    "   \"Stock Data\":{\n" +
                    "       \"Symbol\":\"WSO2\",\n" +
                    "       \"Price\":55.6,\n" +
                    "       \"Volume\":100\n" +
                    "   }\n" +
                    "}",
            onMessageList.get(0).toString());
    AssertJUnit.assertEquals("Mapping incorrect!",
            "{\n" +
                    "   \"Stock Data\":{\n" +
                    "       \"Symbol\":\"WSO2\",\n" +
                    "       \"Price\":57.678,\n" +
                    "       \"Volume\":100\n" +
                    "   }\n" +
                    "}",
            onMessageList.get(1).toString());
    AssertJUnit.assertEquals("Mapping incorrect!",
            "{\n" +
                    "   \"Stock Data\":{\n" +
                    "       \"Symbol\":\"WSO2\",\n" +
                    "       \"Price\":50.0,\n" +
                    "       \"Volume\":100\n" +
                    "   }\n" +
                    "}"
            , onMessageList.get(2).toString());
    AssertJUnit.assertEquals("Mapping incorrect!",
            "{\n" +
                    "   \"Stock Data\":{\n" +
                    "       \"Symbol\":\"WSO2#$%\",\n" +
                    "       \"Price\":50.0,\n" +
                    "       \"Volume\":100\n" +
                    "   }\n" +
                    "}", onMessageList.get(3).toString());
    AssertJUnit.assertEquals("Mapping incorrect!",
                    "[{\n" +
                            "   \"Stock Data\":{\n" +
                            "       \"Symbol\":\"WSO2\",\n" +
                            "       \"Price\":55.6,\n" +
                            "       \"Volume\":100\n" +
                            "   }\n" +
                            "},\n" +
                            "{\n" +
                            "   \"Stock Data\":{\n" +
                            "       \"Symbol\":\"IBM\",\n" +
                            "       \"Price\":32.6,\n" +
                            "       \"Volume\":160\n" +
                            "   }\n" +
                            "}]",
             onMessageList.get(4).toString());
    siddhiAppRuntime.shutdown();

    siddhiAppRuntime.shutdown();
    //unsubscribe from "inMemory" broker per topic
    InMemoryBroker.unsubscribe(subscriberWSO2);
}
 
Example 6
Source File: JsonSinkMapperTestCase.java    From siddhi-map-json with Apache License 2.0 4 votes vote down vote up
@Test
public void jsonSinkMapperTestCase14() throws InterruptedException {
    log.info("JsonSinkMapperTestCase with test default MultipleEvent");
    InMemoryBroker.Subscriber subscriberWSO2 = new InMemoryBroker.Subscriber() {
        @Override
        public void onMessage(Object msg) {
            String jsonString;
            switch (wso2Count.incrementAndGet()) {
            case 1:
                jsonString = "{\"portfolio\":{\"company\":{\"event\":{\"symbol\":\"WSO2\",\"price\":55.6,"
                        + "\"volume\":100}}}}";
                AssertJUnit.assertEquals(jsonString , msg);
                break;
            case 2:
                jsonString = "{\"portfolio\":{\"company\":{\"event\":{\"symbol\":\"WSO2\",\"price\":57.678,"
                        + "\"volume\":100}}}}";
                AssertJUnit.assertEquals(jsonString, msg);
                break;
            case 3:
                jsonString = "{\"portfolio\":{\"company\":{\"event\":{\"symbol\":\"WSO2\",\"price\":50.0,"
                        + "\"volume\":100}}}}";
                AssertJUnit.assertEquals(jsonString , msg);
                break;
            case 4:
                jsonString = "{\"portfolio\":{\"company\":{\"event\":{\"symbol\":\"WSO2#$%\",\"price\":50.0,"
                        + "\"volume\":100}}}}";
                AssertJUnit.assertEquals(jsonString , msg);
                break;
            case 5:
                jsonString = "{\"portfolio\":{\"company\":[{\"event\":{\"symbol\":\"WSO2\",\"price\":55.6,"
                        + "\"volume\":100}}," +
                        "{\"event\":{\"symbol\":\"IBM\",\"price\":32.6,\"volume\":160}}]}}";
                AssertJUnit.assertEquals(jsonString , msg);
                break;
            default:
                AssertJUnit.fail();
            }
        }
        @Override
        public String getTopic() {
            return "WSO2";
        }
    };
    //subscribe to "inMemory" broker per topic
    InMemoryBroker.subscribe(subscriberWSO2);
    String streams = "" +
            "@App:name('TestSiddhiApp')" +
            "define stream FooStream (symbol string, price float, volume long); " +
            "@sink(type='inMemory', topic='WSO2', @map(type='json'," +
            " enclosing.element=\"$.portfolio.company\")) " +
            "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" +
            "from FooStream " +
            "select * " +
            "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("FooStream");
    siddhiAppRuntime.start();
    Event wso2Event = new Event();
    Event ibmEvent = new Event();
    Object[] wso2Data = {"WSO2", 55.6f, 100L};
    Object[] ibmData = {"IBM", 32.6f, 160L};
    wso2Event.setData(wso2Data);
    ibmEvent.setData(ibmData);
    stockStream.send(new Object[]{"WSO2", 55.6f, 100L});
    stockStream.send(new Object[]{"WSO2", 57.678f, 100L});
    stockStream.send(new Object[]{"WSO2", 50f, 100L});
    stockStream.send(new Object[]{"WSO2#$%", 50f, 100L});
    stockStream.send(new Event[]{wso2Event, ibmEvent});
    SiddhiTestHelper.waitForEvents(waitTime, 5, wso2Count, timeout);
    //assert event count
    AssertJUnit.assertEquals(5, wso2Count.get());
    siddhiAppRuntime.shutdown();
    //unsubscribe from "inMemory" broker per topic
    InMemoryBroker.unsubscribe(subscriberWSO2);
}
 
Example 7
Source File: TestSiddhiExternalTimeBatch.java    From eagle with Apache License 2.0 4 votes vote down vote up
Event createEvent(String host, long timestamp) {
    Event e = new Event();
    e.setTimestamp(timestamp);
    e.setData(new Object[] {host, timestamp, "missingblocks", "site1", 14.0});
    return e;
}
 
Example 8
Source File: TestSiddhiAggregator.java    From eagle with Apache License 2.0 4 votes vote down vote up
Event createEvent(String host, long timestamp) {
    Event e = new Event();
    e.setTimestamp(timestamp);
    e.setData(new Object[] {host, timestamp, "missingblocks", "site1", 14.0});
    return e;
}
 
Example 9
Source File: StringListSizeFunctionExtensionTest.java    From eagle with Apache License 2.0 4 votes vote down vote up
@Test
public void testStringListSize2() throws Exception {
    Semaphore semp = new Semaphore(1);
    String ql = " define stream log(timestamp long, site string, component string, resource string, host string, value string); " +
            " from a = log[resource == \"hadoop.namenode.namenodeinfo.corruptfiles\"],\n" +
            "b = log[component == a.component and resource == a.resource and host == a.host and a.value != b.value]\n" +
            "select b.site as site, b.host as host, b.component as component, b.resource as resource, " +
            "b.timestamp as timestamp, str:listSize(b.value) as newMissingBlocksNumber, str:listSize(a.value) as oldMissingBlocksNumber, str:subtract(b.value, a.value) as missingBlocks\n" +
            "insert into output;";
    SiddhiManager manager = new SiddhiManager();
    SiddhiAppRuntime runtime = manager.createSiddhiAppRuntime(ql);
    runtime.addCallback("output", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            Assert.assertTrue(events.length == 1);
            Assert.assertTrue(Integer.parseInt(events[0].getData(5).toString()) == 5);
            Assert.assertTrue(Integer.parseInt(events[0].getData(6).toString()) == 2);
            Assert.assertTrue(events[0].getData(7).toString().equals("a\nc\ne"));
            semp.release();
        }
    });

    runtime.start();

    InputHandler logInput = runtime.getInputHandler("log");
    semp.acquire();
    Event e = new Event();
    e.setTimestamp(System.currentTimeMillis());
    String rhs = "[\"b\", \"d\"]";
    e.setData(new Object[] {System.currentTimeMillis(), "a", "a", "hadoop.namenode.namenodeinfo.corruptfiles", "port01", rhs});
    logInput.send(e);

    e.setTimestamp(System.currentTimeMillis());
    String ths = "[\"a\", \"b\", \"c\", \"d\", \"e\"]";
    e.setData(new Object[] {System.currentTimeMillis(), "a", "a", "hadoop.namenode.namenodeinfo.corruptfiles", "port01", ths});
    logInput.send(e);

    semp.acquire();
    runtime.shutdown();

}