流式处理新玩法,Esper和Storm的联合统计分析

一、首先是一个开源项目(storm-ester):
https://github.com/tomdz/storm-esper

二、导入工程项目

自带的TwitterEsperSample.java例子好像不太好用,反正运行着没什么效果

自己写一个例子

总的来说就是spout模拟发送5次数据,EsperBolt接收后根据规则进行统计输出(这里是每两次计算平均值),然后最后一个bolt展示结果

1、首先是一个Spout:

public class Esper_Spout extends BaseRichSpout {

    SpoutOutputCollector collector;
    Map toSend;


    @Override
    public void open(Map map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) {
        this.collector = spoutOutputCollector;
        System.out.println(">Spout-->>open<<");
        toSend = new HashMap();

        for (int i = 5; i < 10; i++) {
            toSend.put(i, "test--" + i);
        }
    }

    @Override
    public void nextTuple() {
        if (!toSend.isEmpty()) {
            for (Map.Entry transactionEntry : toSend.entrySet()) {
                Integer transactionId = transactionEntry.getKey();
                System.out.println(">Spout->send:" + transactionId);
                //发送需要esper计算的数据,可多个
                collector.emit(tuple(transactionId));
            }
            toSend.clear();
        }
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        outputFieldsDeclarer.declare(new Fields("price"));
    }
}

2、然后写一个Bolt接收Esper处理后的结果输出

public class Esper_Bolt implements IRichBolt {

    private OutputCollector collector;

    @Override
    public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {
        this.collector = outputCollector;
    }

    @Override
    public void execute(Tuple tuple) {
        //这里就是结果啦
        Object result = tuple.getValueByField("tps");
        System.out.println("-->>bolt esper result--->"+result);
    }

    @Override
    public void cleanup() {

    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        outputFieldsDeclarer.declare(new Fields("word"));
    }

    @Override
    public Map getComponentConfiguration() {
        return null;
    }
}

3、最后来个main 方法运行起来

public class Esper_Sample {

    public static void main(String[] args) {

        Logger.getRootLogger().removeAllAppenders();

        EsperBolt bolt = new EsperBolt.Builder()
  .inputs().aliasStream("spout1").toEventType("Apple")
                .outputs().onDefaultStream().emit("tps")
                .statements().add("select avg(price) as tps from Apple.win:length_batch(2)")
                .build();

        TopologyBuilder builder = new TopologyBuilder();

        builder.setSpout("spout1", new Esper_Spout());

        builder.setBolt("get-categ", bolt) .shuffleGrouping("spout1");

        builder.setBolt("get-result", new Esper_Bolt()) .shuffleGrouping("get-categ");

        Config conf = new Config();
        conf.setDebug(true);

        LocalCluster cluster = new LocalCluster();

        cluster.submitTopology("test", conf, builder.createTopology());
//        Utils.sleep(3000);
//        cluster.shutdown();
    }
}

然后就可以看到输出结果啦

你可能感兴趣的:(Storm)