Trident 基本概念

import org.apache.storm.Config;
import org.apache.storm.LocalCluster;
import org.apache.storm.StormSubmitter;
import org.apache.storm.generated.StormTopology;
import org.apache.storm.trident.Stream;
import org.apache.storm.trident.TridentState;
import org.apache.storm.trident.TridentTopology;
import org.apache.storm.trident.operation.BaseFunction;
import org.apache.storm.trident.operation.Consumer;
import org.apache.storm.trident.operation.TridentCollector;
import org.apache.storm.trident.operation.builtin.Count;
import org.apache.storm.trident.testing.FixedBatchSpout;
import org.apache.storm.trident.testing.MemoryMapState;
import org.apache.storm.trident.tuple.TridentTuple;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;

public class WordCountTrident {
    //构建topology
    private static StormTopology buildTopology() {
        //spout源
        FixedBatchSpout spout = new FixedBatchSpout(new Fields("sentence"),1,
                new Values("the cow jumped over the moon"),
                new Values("the man went to the store and bought some candy"),
                new Values("four score and seven years ago"),
                new Values("how many apples can you eat"));
        //不循环发射数据
        spout.setCycle(false);

        //****请根据提示补全StormTopology程序的创建过程****//

        /*********begin*********/

        //首先创建了一个 名为 topology 的TridentTopology 对象
        TridentTopology topology = new TridentTopology();

        //使用.newStream() 方法从上面定义的输入源中读取数据,并在 topology 中创建一个新的数据流 名为spout1
        //使用.each()方法遍历每一个文本行 sentence ,指定使用split()处理,输出字段名为word的tuple元组
        //使用.groupby()方法对字段名为word的tuple元组进行分组
        //使用.persistentAggregate()方法,指定使用count()方法对word进行统计并保存结果到内存中。
        // 不使用.parallelismHint()方法设置并行度
        TridentState wordCounts =
            topology.newStream("spout1", spout)
                .each(new Fields("sentence"), new Split(), new Fields("word"))
                .groupBy(new Fields("word"))
                .persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count"));
                

        /*********end**********/

        //打印count后结果
        wordCounts.newValuesStream()
                .peek(new Consumer() {
                    @Override
                    public void accept(TridentTuple input) {
                        System.out.println(input);
                    }
                });

        return topology.build();
    }
    public static class Split extends BaseFunction {
        public void execute(TridentTuple tuple, TridentCollector collector) {
            String sentence = tuple.getString(0);
            //根据空格拆分 sentence
            for(String word: sentence.split(" ")) {
                //将拆分出的每个单词作为一个 tuple 输出
                collector.emit(new Values(word));
            }
        }
    }
    public static void main(String[] args) throws Exception {
        Config conf = new Config();
        //本地模式
        //创建一个进程内的集群,只需要使用 LocalCluster 类
        //使用 LocalCluster 对象的 submitTopology 方法提交topologies(拓扑)
        //以 topology 的名称, topology 的配置和 topology 本身作为参数输入
        LocalCluster cluster = new LocalCluster();
        cluster.submitTopology("app0", conf, buildTopology());

    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@Anges

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值