如何从Esper EPL中的查询中检索和分组多个列?

时间:2016-03-10 17:20:12

标签: java sql esper

我正在编写一个查询来获取inputStream的title字段的第一个字符,并计算返回的字符类型。

单独地,这两个查询起作用并给出结果的计数和子串之后的字符。

"select count(*) as count as character from Data.win:time_batch(5 sec) ";

"select title.substring(0,1) as character from Data.win:time_batch(5 sec) ";

但是当我将它们和逐个字符组合在一起时,我得到了分组错误。

public String getStatement() {

    return "select count(*) as count, title.substring(0,1) as character from 
                                    Data.win:time_batch(5 sec) group by character";
}

/**
 * Listener method called when Esper has detected a pattern match.
 */
public void update(Map<String, String> eventMap) {
    System.out.println(eventMap);
    // Titles in the last 5 seconds
    String character = (String) eventMap.get("character");
    String count = (String) eventMap.get("count");
    StringBuilder sb = new StringBuilder();
    sb.append("---------------------------------");
    sb.append("\n- [MONITOR] Char = " + character + " Count =" + count);
    sb.append("\n---------------------------------");

    LOG.debug(sb.toString());
}

任何人都可以建议如何分组并返回select语句中的两个字段。

1 个答案:

答案 0 :(得分:1)

EPL中的select-clause不会全局命名表达式。

为了在EPL或全局内共享命名表达式,Esper有表达式(见5.2.9)的例子。

另一种选择是:

insert into Character select title.substring(0,1) as character from Data

select count(*) as count, character from Character.win:time_batch(5 sec) group by character