针对不同用户的Esper规则

时间:2016-03-13 17:39:03

标签: java rule-engine complex-event-processing esper

我最近开始使用Esper编程,我有一个智能穿戴设备,可将计步器数据发送到我的笔记本电脑。然后我使用esper处理这些数据。但是假设我有多个智能可穿戴设备,每个设备都有一个唯一的MAC地址。我使用时间窗口,我的问题是如何更改我的规则文件,以便规则只针对具有相同macaddress的事件触发,并根据此MAC地址采取适当的操作。我的初始化和规则是:

    Configuration cepConfig = new Configuration();

    cepConfig.addEventType("Steps", Steps.class.getName());

    // We setup the engine
    EPServiceProvider cep = EPServiceProviderManager.getProvider("myCEPEngine", cepConfig);
    EPRuntime cepRT = cep.getEPRuntime();

    // We register an EPL statement
    EPAdministrator cepSteps1 = cep.getEPAdministrator();
    EPStatement cepStatementSteps1 = cepSteps1.createEPL("select * from "
            + "Steps().win:time(1 hour) "
            + "group by macAddress "
            + "having sum(max(steps)-min(steps)) < 100");
    cepStatementSteps1.addListener(new rule1Listener());

我的Steps类包含以下字段:

double steps;
String stepsTimestamp;
String macAddress;

这就是我插入事件的方式:

Steps steps0 = new Steps(0, new Date(timeStamp).toString(), "K5E45H778");
cepRT.sendEvent(steps0);
Steps steps00 = new Steps(0, new Date(timeStamp).toString(), "LD24ESF74");
cepRT.sendEvent(steps00);
Steps steps1 = new Steps(25, new Date(timeStamp).toString(), "K5E45H778");
cepRT.sendEvent(steps1);
Steps steps2 = new Steps(50, new Date(timeStamp).toString(), "LD24ESF74");
cepRT.sendEvent(steps2);
Steps steps3 = new Steps(55, new Date(timeStamp).toString(), "K5E45H778");
cepRT.sendEvent(steps3);
Steps steps4 = new Steps(105, new Date(timeStamp).toString(), "LD24ESF74");
cepRT.sendEvent(steps4);
Steps steps5 = new Steps(75, new Date(timeStamp).toString(), "K5E45H778");
cepRT.sendEvent(steps5);
Steps steps6 = new Steps(110, new Date(timeStamp).toString(), "K5E45H778");
cepRT.sendEvent(steps6);

这是我的输出:

Sending tick:    Steps: 0.0 Timestamp: Mon Mar 14 18:13:23 CET 2016 Mac Address: K5E45H778
->Rule 1 fired: K5E45H778
Sending tick:    Steps: 0.0 Timestamp: Mon Mar 14 18:18:23 CET 2016 Mac Address: LD24ESF7474
->Rule 1 fired: LD24ESF7474
Sending tick:    Steps: 25.0 Timestamp: Mon Mar 14 18:23:23 CET 2016 Mac Address: K5E45H778
->Rule 1 fired: K5E45H778
Sending tick:    Steps: 105.0 Timestamp: Mon Mar 14 18:28:23 CET 2016 Mac Address: LD24ESF7474
Sending tick:    Steps: 55.0 Timestamp: Mon Mar 14 18:33:23 CET 2016 Mac Address: K5E45H778
->Rule 1 fired: K5E45H778
Sending tick:    Steps: 75.0 Timestamp: Mon Mar 14 18:38:23 CET 2016 Mac Address: K5E45H778
Sending tick:    Steps: 110.0 Timestamp: Mon Mar 14 18:43:23 CET 2016 Mac Address: K5E45H778

为什么规则不会触发75步的最后一次事件?

1 个答案:

答案 0 :(得分:1)

SQL标准“group by”子句用于每个组的聚合。因此,只需添加“group by macAddress”就可以完成它。