future__xyh
future__ll
采纳率100%
2016-04-26 12:03 阅读 1.6k
已采纳

esper计算问题,有会的大神麻烦贴下代码,非常感谢

100

1.字段是sbn,me,NodeB,cellID,都是int类型,time是long类型
2.100个计数器,分别是C1-C100,其中C1-C50是求sum,C51-C80是求avg,C81-C90是求max,C91-C100是求min
3.sendEvent(),发送事件1W条
4.汇总,time_batch(1 min)
5.updateListener 输出

实体类:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter @Setter
public class CounterInfo {
private int sbn;
private int me;
private int nodeB;
private int cellId;
private long time;
}

updateListener监听:
public void update(EventBean[] newEvents, EventBean[] oldEvents) {
//EsCounter esCounter = new EsCounter();
if(newEvents != null){
int sumC1 = (Integer) newEvents[0].get("C1");
Double avgC2 = (Double) newEvents[0].get("C2");
int maxC3 = (Integer) newEvents[0].get("C3");
int minC4 = (Integer) newEvents[0].get("C4");
System.out.println("C1 Sum is:"+sumC1);
System.out.println("C2 Avg is:"+avgC2);
System.out.println("C3 Max is:"+maxC3);
System.out.println("C4 Max is:"+minC4);
}

        main方法测试类:
        public static void main(String[] args) {

    EPServiceProvider provider = EPServiceProviderManager.getDefaultProvider();
    EPAdministrator administrator =provider.getEPAdministrator();
    EPRuntime runtime = provider.getEPRuntime();

    String esCounter = EsCounter.class.getName();
    String counterInfo = CounterInfo.class.getName();
    Map<String, Object> counterMap = null;

    String epl ="select sbn,me,nodeB,cellID,sum(C1) as C1,avg(C2) as C2,max(C3) as C3,min(C4) as C4 from "+ counterInfo +".win:time_batch(1 min) group by sbn,me,nodeB,cellID";
    /*String epl ="select sum(C1) as C1,avg(C2) as C2,max(C3) as C3,min(C4) as C4 "
            + "from "+counterInfo+".win:time_batch(1 min)";*/
    EPStatement statement = administrator.createEPL(epl);
    statement.addListener(new CounterListener());

    for (int i = 1; i < 10000; i++) {
        counterMap = new HashMap<String, Object>();
        counterMap.put("C" +  (new Random().nextInt(100)+1), new Random().nextInt(6)+1);
        System.out.println("send event"+i+":"+counterMap);
        runtime.sendEvent(counterMap);
    }

}

循环的1万条事件放入到100个计数器里面。

有会的大神帮忙回复下,非常感谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

9条回答 默认 最新

  • 已采纳
    antibodies antibodies 2016-04-27 10:58

    虽然没搞明白你要干嘛, 但是从模糊的描述中我大概差不多知道了你的意思...
    贴下代码. 自己吧c1-c50补充完整就可以了. 下面的示例是 1sec 自己改1 min吧
    直接可运行

     package test.esper_epl;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Random;
    import java.util.concurrent.TimeUnit;
    
    import com.espertech.esper.client.EPAdministrator;
    import com.espertech.esper.client.EPRuntime;
    import com.espertech.esper.client.EPServiceProvider;
    import com.espertech.esper.client.EPServiceProviderManager;
    import com.espertech.esper.client.EPStatement;
    import com.espertech.esper.client.EventBean;
    import com.espertech.esper.client.UpdateListener;
    
    public class MapEpl {
    
    
        public static void main(String[] args) throws InterruptedException {
    
            EPServiceProvider provider = EPServiceProviderManager.getDefaultProvider();
            EPAdministrator administrator =provider.getEPAdministrator();
            EPRuntime runtime = provider.getEPRuntime();
    
            String counterInfo = CounterInfo.class.getName();
    
            String epl ="select sbn,me,nodeB,cellId, sum(cccc('C1')) as c1_sum, sum(cccc('C2')) as c2_sum, avg(cccc('C1')) as c1_avg, avg(cccc('C2')) as c2_avg  from "+ counterInfo +".win:time_batch(1 sec) group by sbn,me,nodeB,cellId";
            EPStatement statement = administrator.createEPL(epl);
            statement.addListener(new CounterListener());
    
            for (int j=0;j<10000;j++){
                CounterInfo counterinfo=new CounterInfo();
    
                counterinfo.setCellId(1);
                counterinfo.setMe(2);
                counterinfo.setNodeB(3);
                counterinfo.setSbn(4);
                counterinfo.setTime(5);
    
                //搞计数器??? 计数器是什么  ?
                Map<String, Integer> counterMap = new HashMap<String, Integer>();
                for (int i = 1; i < 10000; i++) {
                    counterMap.put("C" +  (new Random().nextInt(100)+1), new Random().nextInt(6)+1);
                }
                counterinfo.setCccc(counterMap);
                runtime.sendEvent(counterinfo);
            }
    
            TimeUnit.SECONDS.sleep(6000);
        }
    }
    
    class CounterListener implements UpdateListener {
    
        @Override
        public void update(EventBean[] newEvents, EventBean[] oldEvents) {
            if (newEvents != null)
            {
                for (int i=0;i<newEvents.length;i++){
                    Object event = newEvents[i].getUnderlying();
                    System.out.println("输出............计数器什么的...");
                    System.out.println(event);
                }
            }
        }
    
    }
    
    class CounterInfo {
        private int sbn;
        private int me;
        private int nodeB;
        private int cellId;
        private long time;
        Map<String, Integer> 计数器什么的esper里面没有啊;  
        Map<String, Integer> cccc;  
    
        public int getSbn() {
            return sbn;
        }
        public void setSbn(int sbn) {
            this.sbn = sbn;
        }
        public int getMe() {
            return me;
        }
        public void setMe(int me) {
            this.me = me;
        }
        public int getNodeB() {
            return nodeB;
        }
        public void setNodeB(int nodeB) {
            this.nodeB = nodeB;
        }
        public int getCellId() {
            return cellId;
        }
        public void setCellId(int cellId) {
            this.cellId = cellId;
        }
        public long getTime() {
            return time;
        }
        public void setTime(long time) {
            this.time = time;
        }
        public Map<String, Integer> get计数器什么的esper里面没有啊() {
            return 计数器什么的esper里面没有啊;
        }
        public void set计数器什么的esper里面没有啊(Map<String, Integer> 计数器什么的esper里面没有啊) {
            this.计数器什么的esper里面没有啊 = 计数器什么的esper里面没有啊;
        }
        public Map<String, Integer> getCccc() {
            return cccc;
        }
        public void setCccc(Map<String, Integer> cccc) {
            this.cccc = cccc;
        }
    
        }
    
    
    
    点赞 1 评论 复制链接分享
  • devmiao devmiao 2016-04-26 15:03
  • antibodies antibodies 2016-04-27 00:57

    你指的计数器是什么? 为什么不在一条epl中求出所有的计算, select sum(),avg(), max(),min() from event.win:time_batch(60 sec)?

    点赞 评论 复制链接分享
  • future__xyh future__ll 2016-04-27 02:14

    求大神们指教下啊:
    1,字段该怎么使用,直接用于epl里面会报错,
    2.创建一个map集合,把计数器当key,事件当做value,循环一万次,值该怎么添加到计数器里面
    3.map集合循环中,新的key,value会把之前同样的覆盖掉吧
    4.epl语句提交后,updatelistener该怎么去输出
    5.epl语句中的sum(C1)到sum(50)该如何去简化

    点赞 评论 复制链接分享
  • future__xyh future__ll 2016-04-27 02:15

    epl ="select sbn,me,nodeB,cellID,sum(C1) as C1,avg(C2) as C2,max(C3) as C3,min(C4) as C4 from "+ counterInfo +".win:time_batch(1 min) group by sbn,me,nodeB,cellID";

    点赞 评论 复制链接分享
  • future__xyh future__ll 2016-04-27 02:19

    antibodies 2016.04.27 08:57
    你指的计数器是什么? 为什么不在一条epl中求出所有的计算, select sum(),avg(), max(),min() from event.win:time_batch(60 sec)?

    大神,该怎么写啊,求教。。

    点赞 评论 复制链接分享
  • antibodies antibodies 2016-04-27 03:27

    什么场景? 需求是什么? 其实看完二楼提供的epl应该就可以搞定大多数情况了.

    点赞 评论 复制链接分享
  • future__xyh future__ll 2016-04-27 06:42

    没什么需求,就是要用一条epl语句,对计数器C1-C50这50个计数器分别求和,就是sum(C1),sum(C2)...sum(C50),还有C51到C80求平均数,avg(C51),avg(C52)..avg(C80),max(C81)..max(C90),min(C91)...min(C100).
    做全部的计算,主要现在我不知道给的字段干嘛的,说是直接写到epl语句,但问题没值,运行就报错的,我想知道刚开始的类该怎么去写,然后1W条事件怎么分发到100个计数器里面去。
    我是用的map方法

    for (int i = 1; i < 10000; i++) {
    counterMap = new HashMap();
    //counterMap.put("C0" , 1);
    counterMap.put("C" + (new Random().nextInt(100)+1), new Random().nextInt(6)+1);
    System.out.println(counterMap);
    runtime.sendEvent(counterMap);
    }
    这个出来新的map(key,value),会把上一个同样key的值覆盖掉的吧。

    点赞 评论 复制链接分享
  • future__xyh future__ll 2016-04-27 08:18

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Getter @Setter
    public class CounterInfo {
    private int sbn;
    private int me;
    private int nodeB;
    private int cellId;
    private long time;

    }
    String counterInfo = CounterInfo.class.getName();
    String epl="select sbn,me,nodeB,cellID,sum(C1) as C1,avg(C2) as C2,max(C3) as C3,min(C4) as C4 from "+ esCounter +".win:time_batch(1 min) group by sbn,me,nodeB,cellID";
    EPStatement statement = administrator.createEPL(epl);
    statement.addListener(new CounterListener());

    报错:log4j:WARN No appenders could be found for logger (com.espertech.esper.core.service.EPServiceProviderImpl).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    Exception in thread "main" com.espertech.esper.client.EPStatementException: Error starting statement: Property named 'cellID' is not valid in any stream (did you mean 'cellId'?) [select sbn,me,nodeB,cellID,sum(C1) as C1,avg(C2) as C2,max(C3) as C3,min(C4) as C4 from com.zte.entity.CounterInfo.win:time_batch(1 min) group by sbn,me,nodeB,cellID]
    at com.espertech.esper.core.service.StatementLifecycleSvcImpl.startInternal(StatementLifecycleSvcImpl.java:637)
    at com.espertech.esper.core.service.StatementLifecycleSvcImpl.start(StatementLifecycleSvcImpl.java:595)
    at com.espertech.esper.core.service.StatementLifecycleSvcImpl.createAndStart(StatementLifecycleSvcImpl.java:137)
    at com.espertech.esper.core.service.EPAdministratorImpl.createEPLStmt(EPAdministratorImpl.java:117)
    at com.espertech.esper.core.service.EPAdministratorImpl.createEPL(EPAdministratorImpl.java:66)
    at com.zte.esper.CounterForTest.main(CounterForTest.java:39)
    Caused by: com.espertech.esper.epl.expression.ExprValidationPropertyException: Property named 'cellID' is not valid in any stream (did you mean 'cellId'?)
    at com.espertech.esper.epl.expression.ExprIdentNodeUtil.getSuggestionException(ExprIdentNodeUtil.java:183)
    at com.espertech.esper.epl.expression.ExprIdentNodeUtil.getTypeFromStream(ExprIdentNodeUtil.java:59)
    at com.espertech.esper.epl.expression.ExprIdentNodeImpl.validate(ExprIdentNodeImpl.java:150)
    at com.espertech.esper.epl.expression.ExprNodeUtility.getValidatedSubtreeInternal(ExprNodeUtility.java:147)
    at com.espertech.esper.epl.expression.ExprNodeUtility.getValidatedSubtree(ExprNodeUtility.java:122)
    at com.espertech.esper.epl.core.ResultSetProcessorFactoryFactory.getProcessorPrototype(ResultSetProcessorFactoryFactory.java:114)
    at com.espertech.esper.core.start.EPStatementStartMethodSelectUtil.prepare(EPStatementStartMethodSelectUtil.java:278)
    at com.espertech.esper.core.start.EPStatementStartMethodSelect.startInternal(EPStatementStartMethodSelect.java:51)
    at com.espertech.esper.core.start.EPStatementStartMethodBase.start(EPStatementStartMethodBase.java:53)
    at com.espertech.esper.core.service.StatementLifecycleSvcImpl.startInternal(StatementLifecycleSvcImpl.java:625)
    ... 5 more

    类的问题,该怎么去做的了

    点赞 评论 复制链接分享

相关推荐