【九】storm+redis集成

15 篇文章 0 订阅

redis安装

官网介绍

测试一下storm写redis。

具体功能描述:随机生成单词,单词计数(word count),结果写道redis中。

maven pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.sid.bigdata</groupId>
  <artifactId>storm</artifactId>
  <version>0.0.1</version>
  <packaging>jar</packaging>

  <name>storm</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <storm.version>1.1.1</storm.version>
  </properties>

  <dependencies>
    
  <dependency>
  	<groupId>org.apache.storm</groupId>
  	<artifactId>storm-core</artifactId>
  	<version>${storm.version}</version> 
  </dependency>
  
  <dependency>
  	<groupId>org.apache.storm</groupId>
  	<artifactId>storm-redis</artifactId>
  	<version>${storm.version}</version> 
  </dependency>
  
  </dependencies>
</project>

代码

package integration.redis;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.storm.Config;
import org.apache.storm.LocalCluster;
import org.apache.storm.redis.bolt.RedisStoreBolt;
import org.apache.storm.redis.common.config.JedisPoolConfig;
import org.apache.storm.redis.common.mapper.RedisDataTypeDescription;
import org.apache.storm.redis.common.mapper.RedisStoreMapper;
import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.TopologyBuilder;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.ITuple;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;
import org.apache.storm.utils.Utils;

/**
 * @author liyijie
 * @date 2018年6月13日上午1:01:08
 * @email 37024760@qq.com
 * @remark
 * @version 
 */
public class LocalWordCountStormRedisTopology {
	 public static class DataSourceSpout extends BaseRichSpout{  
		  
	        private SpoutOutputCollector collector;  
	          
	        public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {  
	            this.collector = collector;  
	        }  
	        
	        public static final String[] words = new String[]{"aaa","bbb","ccc","aa","bb","a"};
	        
	        /** 
	         * 1.把每一行数据发射出去 
	         * */  
	        public void nextTuple() { 
	        	Random random = new Random();
	        	String word =words[random.nextInt(words.length)];	                    //获取文件中的每行内容  
	            //发射出去  
	            this.collector.emit(new Values(word));
	            
	            System.out.println("emit: "+word);
	                      
	            Utils.sleep(1000L);   
	            }  
	         
	  
	        public void declareOutputFields(OutputFieldsDeclarer declarer) {  
	            declarer.declare(new Fields("word"));  
	        }  
	          
	    }    
	      
	    /** 
	     * 词频汇总Bolt 
	     * */  
	    public static class CountBolt extends BaseRichBolt{  
	    	
	    	private OutputCollector collector;
	          
	        public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {  
	        	this.collector = collector;
	        }  
	          
	        Map<String,Integer> map = new HashMap<String,Integer>();  
	        /** 
	         * 业务逻辑 
	         * 1.获取每个单词 
	         * 2.对所有单词进行汇总 
	         * 3.输出 
	         * */  
	        public void execute(Tuple input) {  
	            String word = input.getStringByField("word");  
	            Integer count = map.get(word);  
	            if(count==null){  
	                count=0;  
	            }  
	                count++;  
	              
	            map.put(word, count);  
	              
	            collector.emit(new Values(word,map.get(word))); 
	        }  
	  
	        public void declareOutputFields(OutputFieldsDeclarer declarer) { 
	        	declarer.declare(new Fields("word","count"));
	        }  
	          
	    }  
	    
	    public static class WordCountStoreMapper implements RedisStoreMapper {
	        private RedisDataTypeDescription description;
	        private final String hashKey = "wordCount";

	        public WordCountStoreMapper() {
	            description = new RedisDataTypeDescription(
	                RedisDataTypeDescription.RedisDataType.HASH, hashKey);
	        }

	        public RedisDataTypeDescription getDataTypeDescription() {
	            return description;
	        }

	        public String getKeyFromTuple(ITuple tuple) {
	            return tuple.getStringByField("word");
	        }

	        public String getValueFromTuple(ITuple tuple) {
	            return tuple.getIntegerByField("count").toString();
	        }
	    }
	      
	    public static void main(String[] args) {  
	        //本地模式,没有提交到服务器集群上,不需要搭建storm集群  
	        LocalCluster cluster = new LocalCluster();  
	          
	        //TopologyBuilder根据spout和bolt来构建Topology  
	        //storm中任何一个作业都是通过Topology方式进行提交的  
	        //Topology中需要指定spout和bolt的执行顺序  
	        TopologyBuilder tb = new TopologyBuilder();  
	        tb.setSpout("DataSourceSpout", new DataSourceSpout());  
	        //SumBolt以随机分组的方式从DataSourceSpout中接收数据  
	        tb.setBolt("CountBolt", new CountBolt()).shuffleGrouping("DataSourceSpout");  
	  
	        //写到redis
	        JedisPoolConfig poolConfig = new JedisPoolConfig.Builder()
	                .setHost("node1").setPort(6379).build();
	        RedisStoreMapper storeMapper = new WordCountStoreMapper();
	        RedisStoreBolt storeBolt = new RedisStoreBolt(poolConfig, storeMapper);
	        tb.setBolt("RedisStoreBolt", storeBolt).shuffleGrouping("CountBolt");
	        
	        //第一个参数是topology的名称,第三个参数是Topology  
	        cluster.submitTopology("LocalWordCountStormRedisTopology", new Config(), tb.createTopology());  
	      
	    }  
}

结果



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值