Java Code Examples for io.siddhi.query.api.execution.query.Query#select()

The following examples show how to use io.siddhi.query.api.execution.query.Query#select() . 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: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuery7() {
    Query query = Query.query();
    query.from(
            InputStream.faultStream("StockStream").
                    filter(
                            Expression.isNullFaultStream("StockStream", 1)
                    )
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("symbol"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(Expression.variable("avgPrice"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(50)
                    ))
    );
    query.insertIntoFault("OutStockStream");

    SiddhiApp.siddhiApp("test").addQuery(query);
}
 
Example 2
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuery8() {
    Query query = Query.query();
    query.from(
            InputStream.faultStream("StockStream").
                    filter(
                            Expression.isNullInnerStream("StockStream", 1)
                    )
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("symbol"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(Expression.variable("avgPrice"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(50)
                    ))
    );
    query.insertIntoFault("OutStockStream");

    SiddhiApp.siddhiApp("test").addQuery(query);
}
 
Example 3
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery86() throws InterruptedException {
    log.info("Filter test86");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.LONG).attribute
            ("quantity", Attribute.Type.INT);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.LESS_THAN,
            Expression.value(40L))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")).select("quantity", Expression.variable("quantity")));
    query.insertInto("outputStream");

    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 500f, 50L, 6});
    inputHandler.send(new Object[]{"WSO2", 70f, 20L, 2});
    inputHandler.send(new Object[]{"WSO2", 50f, 300L, 4});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    AssertJUnit.assertEquals(1, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example 4
Source File: PatternQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPatternQuery5() {
    Query query = Query.query();
    query.from(
            InputStream.patternStream(
                    State.next(
                            State.every(
                                    State.stream(InputStream.stream("e1", "Stream1").filter(Expression.compare
                                            (Expression.variable("price"),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.value(30))))),
                            State.next(
                                    State.logicalAnd(
                                            State.stream(InputStream.stream("e2", "Stream1").filter(Expression
                                                    .compare(Expression.variable("price"),
                                                            Compare.Operator.GREATER_THAN_EQUAL,
                                                            Expression.value(20)))),
                                            State.stream(InputStream.stream("e3", "Stream2").filter(Expression
                                                    .compare(Expression.variable("price"),
                                                            Compare.Operator.GREATER_THAN_EQUAL,
                                                            Expression.variable("price").ofStream("e1")))))
                                    ,
                                    State.stream(InputStream.stream("e4", "Stream3").filter(Expression.compare
                                            (Expression.variable("price"),
                                                    Compare.Operator.GREATER_THAN,
                                                    Expression.value(74)))))
                    )
            )
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol").ofStream("e1")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price").ofStream("e2")))

    );
    query.insertInto("OutputStream");

}
 
Example 5
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery115() throws InterruptedException {
    log.info("Filter test115");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price1", Attribute.Type.FLOAT).attribute("price2", Attribute.Type.FLOAT)
            .attribute("volume", Attribute.Type.LONG).attribute("quantity", Attribute.Type.INT);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.value(false)));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price1", Expression
            .variable("price1")).select("quantity", Expression.variable("quantity")));
    query.insertInto("outputStream");

    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
            AssertJUnit.fail("No events should occur");

        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 50f, 60f, 60L, 6});
    Thread.sleep(100);
    siddhiAppRuntime.shutdown();
}
 
Example 6
Source File: TableQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatingInQuery() {
    Query query = Query.query();
    query.from(
            InputStream.stream("cseEventStream").
                    filter(
                            Expression.and(
                                    Expression.compare(
                                            Expression.add(Expression.value(7), Expression.value(9.5)),
                                            Compare.Operator.GREATER_THAN,
                                            Expression.variable("price")),
                                    Expression.in(
                                            Expression.compare(Expression.value(9.5),
                                                    Compare.Operator.GREATER_THAN,
                                                    Expression.variable("price")),
                                            "table"
                                    )
                            )
                    ).window("lengthBatch", Expression.value(50))
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("symbol"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(Expression.variable("avgPrice"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(50)
                    ))
    );
    query.insertInto("StockQuote");

}
 
Example 7
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery81() throws InterruptedException {
    log.info("Filter test81");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.LESS_THAN, Expression.value(70f))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")));
    query.insertInto("outputStream");

    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 50f, 60d});
    inputHandler.send(new Object[]{"WSO2", 70f, 40d});
    inputHandler.send(new Object[]{"WSO2", 44f, 200d});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    siddhiAppRuntime.shutdown();
}
 
Example 8
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery107() throws InterruptedException {
    log.info("Filter test107");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.LONG).attribute
            ("quantity", Attribute.Type.INT);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.GREATER_THAN_EQUAL, Expression.value(100f))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")).select("quantity", Expression.variable("quantity")));
    query.insertInto("outputStream");

    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 50f, 60L, 6});
    inputHandler.send(new Object[]{"WSO2", 70f, 40L, 10});
    inputHandler.send(new Object[]{"WSO2", 44f, 200L, 56});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    AssertJUnit.assertEquals(1, count.get());
    siddhiAppRuntime.shutdown();

}
 
Example 9
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery83() throws InterruptedException {
    log.info("Filter test83");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE)
            .attribute("quantity", Attribute.Type.INT);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.LESS_THAN,
            Expression.value(60L))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")).select("quantity", Expression.variable("quantity")));
    query.insertInto("outputStream");

    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 500f, 50d, 6});
    inputHandler.send(new Object[]{"WSO2", 70f, 60d, 2});
    inputHandler.send(new Object[]{"WSO2", 60f, 300d, 4});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    AssertJUnit.assertEquals(1, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example 10
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = UnsupportedAttributeTypeException.class)
public void testCreatingReturnFilterQueryLimitAndSortError4() {
    Query query = Query.query();
    query.from(
            InputStream.stream("StockStream"));
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    orderBy(Expression.variable("avgPrice")).
                    limit(Expression.value("Test"))
    );

}
 
Example 11
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery94() throws InterruptedException {
    log.info("Filter test94");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.GREATER_THAN_EQUAL, Expression.value(50d))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")));
    query.insertInto("outputStream");

    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 50f, 60d});
    inputHandler.send(new Object[]{"WSO2", 70f, 40d});
    inputHandler.send(new Object[]{"WSO2", 44f, 200d});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    AssertJUnit.assertEquals(2, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example 12
Source File: OutputRateTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatingQuery3() {
    Query query = Query.query();
    query.from(
            InputStream.stream("cseEventStream").
                    filter(Expression.and(Expression.compare(Expression.add(Expression.value(7), Expression.value
                                    (9.5)),
                            Compare.Operator.GREATER_THAN,
                            Expression.variable("price")),
                            Expression.compare(Expression.value(100),
                                    Compare.Operator.GREATER_THAN_EQUAL,
                                    Expression.variable("volume")
                            )
                            )
                    ).window("lengthBatch", Expression.value(50))
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("symbol"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(Expression.variable("avgPrice"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(50)
                    ))
    );

    query.output(OutputRate.perSnapshot(Expression.Time.minute(1)));
    query.insertInto("StockQuote");

}
 
Example 13
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery96() throws InterruptedException {
    log.info("Filter test96");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.DOUBLE).attribute("volume", Attribute.Type.DOUBLE);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("price"),
            Compare.Operator.GREATER_THAN_EQUAL, Expression.value(50))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")));
    query.insertInto("outputStream");

    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 50d, 60d});
    inputHandler.send(new Object[]{"WSO2", 70d, 40d});
    inputHandler.send(new Object[]{"WSO2", 44d, 200d});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    AssertJUnit.assertEquals(2, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example 14
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void test10() {
    Query queryString = SiddhiCompiler.parseQuery("" +
            "from  StockStream[7+9.5 > price and 100 >= volume]#window.length(50) " +
            " " +
            "select symbol as symbol, price as price, volume as volume  " +
            "update StockQuote \n " +
            "   set StockQuote.price = price, \n " +
            "    StockQuote.volume = volume " +
            " on symbol==StockQuote.symbol ;"
    );
    AssertJUnit.assertNotNull(queryString);

    Query query = Query.query();
    query.from(
            InputStream.stream("StockStream").
                    filter(Expression.and(Expression.compare(Expression.add(Expression.value(7), Expression.value
                                    (9.5)),
                            Compare.Operator.GREATER_THAN,
                            Expression.variable("price")),
                            Expression.compare(Expression.value(100),
                                    Compare.Operator.GREATER_THAN_EQUAL,
                                    Expression.variable("volume")
                            )
                            )
                    ).window("length", Expression.value(50))
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("price", Expression.variable("price")).
                    select("volume", Expression.variable("volume"))
    );
    query.updateBy("StockQuote", OutputStream.OutputEventType.CURRENT_EVENTS,
            UpdateStream.updateSet().
                    set(
                            Expression.variable("price").ofStream("StockQuote"),
                            Expression.variable("price")).
                    set(
                            Expression.variable("volume").ofStream("StockQuote"),
                            Expression.variable("volume")),
            Expression.compare(
                    Expression.variable("symbol"),
                    Compare.Operator.EQUAL,
                    Expression.variable("symbol").ofStream("StockQuote")));

    AssertJUnit.assertEquals(query, queryString);

}
 
Example 15
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testFilterQuery112() throws InterruptedException {
    log.info("Filter test112");

    SiddhiManager siddhiManager = new SiddhiManager();

    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE)
            .attribute("quantity", Attribute.Type.INT).attribute("awards", Attribute.Type.LONG);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream"));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("multipliedQuantity", Expression.multiply(Expression.variable("quantity"), Expression
                            .value(4))).
                    select("multipliedPrice", Expression.multiply(Expression.variable("price"), Expression.value
                            (2))).
                    select("multipliedVolume", Expression.multiply(Expression.variable("volume"), Expression
                            .value(3))).
                    select("multipliedAwards", Expression.multiply(Expression.variable("awards"), Expression
                            .value(5)))

    );
    query.insertInto("OutputStream");


    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override

        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            AssertJUnit.assertTrue("20".equals(inEvents[0].getData()[1].toString()));
            AssertJUnit.assertTrue("111.0".equals(inEvents[0].getData()[2].toString()));
            AssertJUnit.assertTrue("300.0".equals(inEvents[0].getData()[3].toString()));
            AssertJUnit.assertTrue("15".equals(inEvents[0].getData()[4].toString()));
            count.addAndGet(inEvents.length);
        }

    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 55.5f, 100d, 5, 3L});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    AssertJUnit.assertEquals(1, count.get());
    siddhiAppRuntime.shutdown();

}
 
Example 16
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testFilterQuery100() throws InterruptedException {
    log.info("Filter test100");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.LONG).attribute
            ("quantity", Attribute.Type.INT);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.GREATER_THAN_EQUAL,
            Expression.value(40L))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")).select("quantity", Expression.variable("quantity")));
    query.insertInto("outputStream");

    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 500f, 50L, 6});
    inputHandler.send(new Object[]{"WSO2", 70f, 20L, 2});
    inputHandler.send(new Object[]{"WSO2", 50f, 300L, 4});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    AssertJUnit.assertEquals(2, count.get());
    siddhiAppRuntime.shutdown();

}
 
Example 17
Source File: PartitionTestCase1.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testPartitionQuery39() throws InterruptedException {
    log.info("Partition test");
    SiddhiApp siddhiApp = SiddhiApp.siddhiApp("Test")
            .defineStream(
                    StreamDefinition.id("streamA")
                            .attribute("symbol", Attribute.Type.STRING)
                            .attribute("price", Attribute.Type.INT)
            );
    Query query = Query.query();
    query.from(
            InputStream.stream("streamA")
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("price", Expression.variable("price")));
    query.insertInto("StockQuote");
    Partition partition = Partition.partition()
            .annotation(Annotation.annotation("info").element("name", "partitionA"))
            .with("streamA", Expression.variable("symbol")).addQuery(query);
    siddhiApp.addPartition(partition);

    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    StreamCallback streamCallback = new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            AssertJUnit.assertTrue("IBM".equals(events[0].getData(0)) || "WSO2".equals(events[0].getData(0)));
            count.addAndGet(events.length);
            eventArrived = true;
        }
    };
    siddhiAppRuntime.addCallback("StockQuote", streamCallback);

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("streamA");
    siddhiAppRuntime.start();
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"IBM", 700}));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"WSO2", 60}));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"WSO2", 60}));
    Thread.sleep(1000);
    AssertJUnit.assertEquals(3, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example 18
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testFilterQuery52() throws InterruptedException {
    log.info("Filter test52");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.EQUAL, Expression.value(60))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")));
    query.insertInto("outputStream");


    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    siddhiAppRuntime.addCallback("query1", new QueryCallback() {
        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();

    inputHandler.send(new Object[]{"WSO2", 50f, 60d});
    inputHandler.send(new Object[]{"WSO2", 70f, 40d});
    inputHandler.send(new Object[]{"WSO2", 44f, 200d});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();


}
 
Example 19
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void test8() {
    Query query = SiddhiCompiler.parseQuery("from  StockStream[7-9.5 > price and 100 >= volume]#window.length(50)" +
            "#[symbol=='WSO2']  " +
            "select symbol, avg(price) as avgPrice " +
            "group by symbol " +
            "having avgPrice  >= 50 " +
            "insert into OutStockStream ;"
    );
    AssertJUnit.assertNotNull(query);

    Query api = Query.query();
    api.from(
            InputStream.stream("StockStream").
                    filter(Expression.and(
                            Expression.compare(
                                    Expression.subtract(Expression.value(7), Expression.value(9.5)),
                                    Compare.Operator.GREATER_THAN,
                                    Expression.variable("price")),
                            Expression.compare(
                                    Expression.value(100),
                                    Compare.Operator.GREATER_THAN_EQUAL,
                                    Expression.variable("volume")
                            )
                    )).
                    window("length", Expression.value(50)).
                    filter(
                            Expression.compare(
                                    Expression.variable("symbol"),
                                    Compare.Operator.EQUAL,
                                    Expression.value("WSO2")
                            )
                    )
    );
    api.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(Expression.variable("avgPrice"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(50)
                    ))
    );
    api.insertInto("OutStockStream");

    AssertJUnit.assertEquals(api, query);
}
 
Example 20
Source File: PartitionTestCase1.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testPartitionQuery17() throws InterruptedException {
    log.info("Partition test17");


    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiApp siddhiApp = new SiddhiApp("plan17");

    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);

    siddhiApp.defineStream(streamDefinition);

    Partition partition = Partition.partition().
            with("cseEventStream",
                    Partition.range("LessValue",
                            Expression.compare(
                                    Expression.value(200),
                                    Compare.Operator.GREATER_THAN,
                                    Expression.variable("volume"))
                    ),
                    Partition.range("HighValue",
                            Expression.compare(
                                    Expression.value(200),
                                    Compare.Operator.LESS_THAN_EQUAL,
                                    Expression.variable("volume"))
                    ));

    Query query = Query.query();
    query.from(InputStream.stream("cseEventStream"));

    query.select(
            Selector.selector().
                    select("sumvolume", Expression.function("sum", Expression.variable("volume")))

    );
    query.insertInto("StockStream");


    partition.addQuery(query);


    siddhiApp.addPartition(partition);


    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);


    siddhiAppRuntime.addCallback("StockStream", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                count.incrementAndGet();
                if (count.get() == 1) {
                    AssertJUnit.assertEquals(100L, event.getData()[0]);
                } else if (count.get() == 2) {
                    AssertJUnit.assertEquals(600L, event.getData()[0]);
                } else if (count.get() == 3) {
                    AssertJUnit.assertEquals(200L, event.getData()[0]);
                } else if (count.get() == 4) {
                    AssertJUnit.assertEquals(250L, event.getData()[0]);
                }
                eventArrived = true;
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"IBM", 75.6f, 100});
    inputHandler.send(new Object[]{"WSO2", 75.6f, 600});
    inputHandler.send(new Object[]{"IBM", 75.6f, 100});
    Thread.sleep(100);
    inputHandler.send(new Object[]{"ORACLE", 75.6f, 50});
    SiddhiTestHelper.waitForEvents(100, 4, count, 60000);
    AssertJUnit.assertEquals(4, count.get());
    siddhiAppRuntime.shutdown();

}