STORM入门之(TridentAPI,Aggregation)

基本介绍

Aggregation是Trident的基本基本api 主要作用是聚合,如下聚合方法作用是记录单词出现的次数

package com.storm.trident;
import org.apache.storm.shade.org.apache.commons.exec.util.MapUtils;
import org.apache.storm.trident.operation.BaseAggregator;
import org.apache.storm.trident.operation.TridentCollector;
import org.apache.storm.trident.tuple.TridentTuple;
import org.apache.storm.tuple.Values;
import java.util.HashMap;
import java.util.Map;

public  class WordAggregat extends BaseAggregator<Map<String, Integer>> {

    public static  Map<String, Integer> map =  new HashMap<String, Integer>();

    @Override
    public Map<String, Integer> init(Object batchId, TridentCollector collector) {
        return new HashMap<String, Integer>();
    }

    @Override
    public void aggregate(Map<String, Integer> val, TridentTuple tuple,TridentCollector collector) {
        String location = tuple.getString(0);
        Integer i = map.get(location);
        if(null == i){
               i = 0;
        }else{
            i = i+1;
        }
        map.put(location, i);
    }

    @Override
    public void complete(Map<String, Integer> val, TridentCollector collector) {
        for (String key : map.keySet()) {
            System.out.println("key= "+ key + " and value= " + map.get(key));
        }
        collector.emit(new Values(map));
    }
}


(1)init 接收第一batch 执行

(2)aggregate循环执行

(3)complet结束循环执行

Trident构建

package com.storm.topology;

import com.storm.spout.FixedBatchSpout;
import com.storm.trident.Split;
import com.storm.trident.WordAggregat;
import com.storm.trident.WordFilter;
import org.apache.storm.Config;
import org.apache.storm.LocalCluster;
import org.apache.storm.generated.StormTopology;
import org.apache.storm.redis.common.config.JedisClusterConfig;
import org.apache.storm.redis.trident.state.RedisClusterState;
import org.apache.storm.trident.TridentTopology;
import org.apache.storm.trident.operation.builtin.Count;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;

import java.net.InetSocketAddress;
import java.util.HashSet;
import java.util.Set;

public class TTopology {

    public static void main(String[] args){
        TridentTopology topology = new TridentTopology();
        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(true);
        topology.newStream("batch-spout",spout).parallelismHint(2)
                .each(new Fields("sentence"), new Split(), new Fields("word")).project(new Fields("word")) //project方法只发送word字段
                .aggregate(new Fields("word"),new WordAggregat(),new Fields("agg"));
        StormTopology stormTopology = topology.build();
        LocalCluster cluster = new LocalCluster();
        Config conf = new Config();
        conf.setDebug(true);
        cluster.submitTopology("soc", conf,stormTopology);
    }
   
}

project方法作用是摒弃tuple其他消息体,之发送word一个field

结果

出现单词的次数



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值