future__ll 2016-04-26 04:03 采纳率: 100%
浏览 1668
已采纳

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

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 2016-04-27 02: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;
        }
    
        }
    
    
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 navicat解析mysql密码
  • ¥15 SDAPI(关键词-table)
  • ¥15 unity安卓打包出现问题
  • ¥15 爱快路由器端口更改错误导致无法访问
  • ¥20 安装catkin时遇到了如下问题请问该如何解决呢
  • ¥15 VAE模型如何输出结果
  • ¥15 编译python程序为pyd文件报错:{"source code string cannot contain null bytes"
  • ¥20 关于#r语言#的问题:广义加行模型拟合曲线后如何求拐点
  • ¥15 fluent设置了自动保存后,会有几个时间点不保存
  • ¥20 激光照射到四象线探测器,通过液晶屏显示X、Y值
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部