官方對Siddhi的介紹如下:
Siddhi CEP is a lightweight, easy-to-use Open Source Complex Event Processing Engine (CEP) under Apache Software License v2.0.
Siddhi是一個輕量級的,簡單的開源的復雜事件流程引擎。它使用類SQL的語言描述事件流任務,可以很好的支撐開發一個可擴展的,可配置的流式任務執行引擎。
性能管理系統之中,告警模塊采用storm作為告警生成組件。傳統設計之中,為了支持不同的告警規則類型,我們需要編寫不同的業務邏輯代碼,但是使用了Siddhi之后,我們只需要配置不同的流任務Siddhiql,即可以支持不同的告警業務。
作為Siddhi入門文章,本文只介紹如何引用Siddhi核心類庫以及HelloWorld代碼。
1、 maven依賴
<dependencies> <dependency> <groupId>org.wso2.siddhi</groupId> <artifactId>siddhi-core</artifactId> <version>4.0.0-M7</version> </dependency> <dependency> <groupId>org.wso2.siddhi</groupId> <artifactId>siddhi-query-api</artifactId> <version>4.0.0-M7</version> </dependency> <dependency> <groupId>org.wso2.siddhi</groupId> <artifactId>siddhi-query-compiler</artifactId> <version>4.0.0-M7</version> </dependency> </dependencies>
2、 HelloWorld代碼(簡單的進行數據過濾)
/* * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package com.coshaho.learn.siddhi; import org.wso2.siddhi.core.SiddhiAppRuntime; import org.wso2.siddhi.core.SiddhiManager; import org.wso2.siddhi.core.event.Event; import org.wso2.siddhi.core.query.output.callback.QueryCallback; import org.wso2.siddhi.core.stream.input.InputHandler; //import org.wso2.siddhi.core.util.EventPrinter; /** * * SimpleFilterSample.java Create on 2017年6月19日 下午10:54:41 * * 類功能說明: siddhi官方例子,數據過濾 * * Copyright: Copyright(c) 2013 * Company: COSHAHO * @Version 1.0 * @Author coshaho */ public class SimpleFilterSample { public static void main(String[] args) throws InterruptedException { // Creating Siddhi Manager SiddhiManager siddhiManager = new SiddhiManager(); String siddhiApp = "" + "define stream cseEventStream (symbol string, price float, volume long); " + "" + "@info(name = 'query1') " + "from cseEventStream[volume < 150] " + "select symbol,price " + "insert into outputStream ;"; // Generating runtime SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); // Adding callback to retrieve output events from query siddhiAppRuntime.addCallback("query1", new QueryCallback() { @Override public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) { // EventPrinter.print(timeStamp, inEvents, removeEvents); System.out.print(inEvents[0].getData(0) + " "); } }); // Retrieving InputHandler to push events into Siddhi InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream"); // Starting event processing siddhiAppRuntime.start(); // Sending events to Siddhi inputHandler.send(new Object[]{"Welcome", 700f, 100L}); inputHandler.send(new Object[]{"WSO2", 60.5f, 200L}); inputHandler.send(new Object[]{"to", 50f, 30L}); inputHandler.send(new Object[]{"IBM", 76.6f, 400L}); inputHandler.send(new Object[]{"siddhi!", 45.6f, 50L}); Thread.sleep(500); // Shutting down the runtime siddhiAppRuntime.shutdown(); // Shutting down Siddhi siddhiManager.shutdown(); } }
3、 執行結果

4、 Siddhi倉庫
<repositories> <repository> <id>wso2.releases</id> <name>WSO2 internal Repository</name> <url>http://maven.wso2.org/nexus/content/repositories/releases/</url> <releases> <enabled>true</enabled> <updatePolicy>daily</updatePolicy> <checksumPolicy>ignore</checksumPolicy> </releases> </repository> </repositories>
