storm 引入redis ,主要是使用redis缓存库暂存storm的计算结果,然后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>storm07</groupId>
<artifactId>storm07</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>storm07</name>
<url>http://maven.apache.org</url>
<repositories>
<!-- Repository where we can found the storm dependencies -->
<repository>
<id>clojars.org</id>
<url>http://clojars.org/repo</url>
</repository>
</repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.storm</groupId>
<artifactId>storm-core</artifactId>
<version>0.9.2-incubating</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.1.1</version>
</dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.0-beta9</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.0-beta9</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.10</version>
</dependency>
</dependencies>
<build>
<finalName>storm07</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<!-- 单元测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
<includes>
<include>**/*Test*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<!-- 绑定到特定的生命周期之后,运行maven-source-pluin 运行目标为jar-no-fork -->
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Topology

package bhz.storm.redis.example;

import java.util.ArrayList;
import java.util.List; import backtype.storm.Config;
import backtype.storm.LocalCluster;
import backtype.storm.generated.AlreadyAliveException;
import backtype.storm.generated.InvalidTopologyException;
import backtype.storm.topology.TopologyBuilder; public class Topology {
public static void main(String[] args) throws AlreadyAliveException, InvalidTopologyException { TopologyBuilder builder = new TopologyBuilder(); List<String> zks = new ArrayList<String>();
zks.add("192.168.1.114"); List<String> cFs = new ArrayList<String>();
cFs.add("personal");
cFs.add("company"); // set the spout class
builder.setSpout("spout", new SampleSpout(), 2);
// set the bolt class
builder.setBolt("bolt", new StormRedisBolt("192.168.1.114",6379), 2).shuffleGrouping("spout"); Config conf = new Config();
conf.setDebug(true);
// create an instance of LocalCluster class for
// executing topology in local mode.
LocalCluster cluster = new LocalCluster(); // StormRedisTopology is the name of submitted topology.
cluster.submitTopology("StormRedisTopology", conf, builder.createTopology());
try {
Thread.sleep(10000);
} catch (Exception exception) {
System.out.println("Thread interrupted exception : " + exception);
}
// kill the StormRedisTopology
cluster.killTopology("StormRedisTopology");
// shutdown the storm test cluster
cluster.shutdown();
}
}

spout

package bhz.storm.redis.example;

import java.util.HashMap;
import java.util.Map;
import java.util.Random; import backtype.storm.spout.SpoutOutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichSpout;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Values; public class SampleSpout extends BaseRichSpout {
private static final long serialVersionUID = 1L;
private SpoutOutputCollector spoutOutputCollector; private static final Map<Integer, String> FIRSTNAMEMAP = new HashMap<Integer, String>();
static {
FIRSTNAMEMAP.put(0, "john");
FIRSTNAMEMAP.put(1, "nick");
FIRSTNAMEMAP.put(2, "mick");
FIRSTNAMEMAP.put(3, "tom");
FIRSTNAMEMAP.put(4, "jerry");
} private static final Map<Integer, String> LASTNAME = new HashMap<Integer, String>();
static {
LASTNAME.put(0, "anderson");
LASTNAME.put(1, "watson");
LASTNAME.put(2, "ponting");
LASTNAME.put(3, "dravid");
LASTNAME.put(4, "lara");
} private static final Map<Integer, String> COMPANYNAME = new HashMap<Integer, String>();
static {
COMPANYNAME.put(0, "abc");
COMPANYNAME.put(1, "dfg");
COMPANYNAME.put(2, "pqr");
COMPANYNAME.put(3, "ecd");
COMPANYNAME.put(4, "awe");
} public void open(Map conf, TopologyContext context,
SpoutOutputCollector spoutOutputCollector) {
// Open the spout
this.spoutOutputCollector = spoutOutputCollector;
} public void nextTuple() {
// Storm cluster repeatedly call this method to emit the continuous //
// stream of tuples.
final Random rand = new Random();
// generate the random number from 0 to 4.
int randomNumber = rand.nextInt(5);
spoutOutputCollector.emit (new Values(FIRSTNAMEMAP.get(randomNumber),LASTNAME.get(randomNumber),COMPANYNAME.get(randomNumber)));
} public void declareOutputFields(OutputFieldsDeclarer declarer) {
// emit the field site.
declarer.declare(new Fields("firstName","lastName","companyName"));
}
}

StormRedisBolt

package bhz.storm.redis.example;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import backtype.storm.task.TopologyContext;
import backtype.storm.topology.BasicOutputCollector;
import backtype.storm.topology.IBasicBolt;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.tuple.Tuple; public class StormRedisBolt implements IBasicBolt{ private static final long serialVersionUID = 2L;
private RedisOperations redisOperations = null;
private String redisIP = null;
private int port;
public StormRedisBolt(String redisIP, int port) {
this.redisIP = redisIP;
this.port = port;
} public void execute(Tuple input, BasicOutputCollector collector) {
Map<String, Object> record = new HashMap<String, Object>();
//"firstName","lastName","companyName")
record.put("firstName", input.getValueByField("firstName"));
record.put("lastName", input.getValueByField("lastName"));
record.put("companyName", input.getValueByField("companyName"));
redisOperations.insert(record, UUID.randomUUID().toString());
} public void declareOutputFields(OutputFieldsDeclarer declarer) { } public Map<String, Object> getComponentConfiguration() {
return null;
} public void prepare(Map stormConf, TopologyContext context) {
redisOperations = new RedisOperations(this.redisIP, this.port);
} public void cleanup() { } }
package bhz.storm.redis.example;

import java.io.Serializable;
import java.util.Map; import com.fasterxml.jackson.databind.ObjectMapper; import redis.clients.jedis.Jedis; public class RedisOperations implements Serializable { private static final long serialVersionUID = 1L;
Jedis jedis = null; public RedisOperations(String redisIP, int port) {
// Connecting to Redis on localhost
jedis = new Jedis(redisIP, port);
} public void insert(Map<String, Object> record, String id) {
try {
jedis.set(id, new ObjectMapper().writeValueAsString(record));
} catch (Exception e) {
System.out.println("Record not persist into datastore : ");
}
}
}

大数据处理框架之Strom:redis storm 整合的更多相关文章

  1. 大数据处理框架之Strom: Storm----helloword

    大数据处理框架之Strom: Storm----helloword Storm按照设计好的拓扑流程运转,所以写代码之前要先设计好拓扑图.这里写一个简单的拓扑: 第一步:创建一个拓扑类含有main方法的 ...

  2. 大数据处理框架之Strom:认识storm

    Storm是分布式实时计算系统,用于数据的实时分析.持续计算,分布式RPC等. (备注:5种常见的大数据处理框架:· 仅批处理框架:Apache Hadoop:· 仅流处理框架:Apache Stor ...

  3. 大数据处理框架之Strom:Flume+Kafka+Storm整合

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 storm-0.9 apache-flume-1.6.0 ...

  4. 大数据处理框架之Strom:kafka storm 整合

    storm 使用kafka做数据源,还可以使用文件.redis.jdbc.hive.HDFS.hbase.netty做数据源. 新建一个maven 工程: pom.xml <project xm ...

  5. 大数据处理框架之Strom: Storm拓扑的并行机制和通信机制

    一.并行机制 Storm的并行度 ,通过提高并行度可以提高storm程序的计算能力. 1.组件关系:Supervisor node物理节点,可以运行1到多个worker,不能超过supervisor. ...

  6. 大数据处理框架之Strom:Storm集群环境搭建

    搭建环境 Red Hat Enterprise Linux Server release 7.3 (Maipo)      zookeeper-3.4.11 jdk1.7.0_80      Pyth ...

  7. 大数据处理框架之Strom:DRPC

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 storm-0.9 一.DRPC DRPC:Distri ...

  8. 大数据处理框架之Strom:容错机制

    1.集群节点宕机Nimbus服务器 单点故障,大部分时间是闲置的,在supervisor挂掉时会影响,所以宕机影响不大,重启即可非Nimbus服务器 故障时,该节点上所有Task任务都会超时,Nimb ...

  9. 大数据处理框架之Strom:事务

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 storm-0.9 apache-flume-1.6.0 ...

随机推荐

  1. java 之程序中的http请求

    背景 java程序中有时需要我们发起http级别的请求,例如抓数据或者第三方对接时,一般分为两种:一种是只需我们发起请求,还有一种是我们不但要发起请求,还要拿到请求后的数据来进行下一步处理 实现 针对 ...

  2. Python开发【模块】:aiohttp(一)

    AIOHTTP 用于asyncio和Python的异步HTTP客户端/服务器 主要特点: 支持客户端和HTTP服务器. 支持服务器WebSockets和 客户端WebSockets开箱即用,没有回调地 ...

  3. .NET Core 用 VS Code新建各种类型的项目

    用命令行找个地方, 建立目录, 然后执行一下dotnet new --help命令, 查看一下建项目的帮助: 那我建立一个不带用户验证的mvc项目: dotnet new mvc --auth Non ...

  4. 教你使用SQL数据库复制系列(1-7)

    SQL Server 复制系列(文章索引) 一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 前言(Introduction) 复制逻辑结构图(Construction) ...

  5. 在Ubuntu上实现局域网共享文件夹

    在Ubuntu上实现局域网共享文件夹如果你的系统是Ubuntu 14.04.14.10或12.04,有两个方法可以使你通过局域网在搭载Windows或其他Linux的电脑上共享本地文件.对局域网中的每 ...

  6. 20181017 PL/SQL 记录

    1. 配置DB 链接文件,帮助中找到tnsnames.ora文件路径,进行注册数据库信息,登陆即可. 这只是个客户端,具体服务器段数据库情况不清楚. 2.写法区别PL/SQL 和SQL 变量定义 赋值 ...

  7. [转载]MACD 各周期指标状态

    MACD指标:MACD指标是一个非常好用的指标,它与均线.量价关系配合使用对判断行情很有效.这里有必要再深一点讲MACD级别之间的作用. 一.首先,必须明白的是任何指标中都是大级别包含小级别,小级别对 ...

  8. 修改vs2010中html的默认模板

    用vs2010开发,新建html时,html页面会生成HTML 4 XHTML的header,如何把它改成干净的html5风格? 步骤: 修改 你的安装目录\Microsoft Visual Stud ...

  9. wx事件处理二

    wxPython首先在触发对象中查找匹配事件类型的被绑定的处理器函数,如果找到,刚相应方法被执行.如果没找到,wxPython将检查该事件是否传送到了上一级的容器,如果是,父窗口被检查,如此一级级向上 ...

  10. testetest

    resumeLoad renren静态 foolday \ swImg activity01