开始JStorm学习之前需要搭建集群环境,这里演示搭建单机JStorm环境,仅供学习使用,生产环境部署大同小异,但建议参考JStorm社区及相关说明文档。
一、前提
JStorm核心代码均用Java实现,所以依赖Java Runtime,另外,JStorm有脚本采用Python实现,所以还需要Python的支持。
1、JAVA环境

2、Python环境

这里选择Java版本1.6.0_35及Python版本2.6.5,如果默认没有安装可以参考相关文档(www.java.comwww.python.org)。
二、版本选择
zeromq-3.2.4
zookeeper-3.4.5
jstorm-0.7.1
三、JStorm环境搭建
与Storm一样,JStorm的底层消息通信机制依赖zeromq/jzmq,另外,JStorm通过zookeeper实现数据共享和协调服务。
1、安装zeromq
wget http://download.zeromq.org/zeromq-3.2.4.tar.gz
tar zxf zeromq-3.2.4.tar.gz
cd zeromq-3.2.4
./configure
make
sudo make install
sudo ldconfig
2、安装jzmq
wget https://github.com/zeromq/jzmq/tarball/master -O jzmq.tar.gz
tar zxf jzmq.tar.gz
cd jzmq
./autogen.sh
./configure
make
make install
3、安装zookeeper
wget http://apache.dataguru.cn/zookeeper/zookeeper-3.4.5/zookeeper-3.4.5.tar.gz
tar zxf zookeeper-3.4.5.tar.gz
cd zookeeper-3.4.5
./bin/zkServer.sh start
./bin/zkServer.sh stop
4、安装jstorm
wget http://42.121.19.155/jstorm/jstorm-0.7.1.zip
unzip jstorm-0.7.1.zip
编辑配置文件conf/storm.yaml
storm.zookeeper.servers:
- “localhost”
nimbus.host: “localhost”
storm.zookeeper.root: “/jstorm”
storm.local.dir: “/tmp/jstorm”
drpc.servers:
- “localhost”
如果是开发环境本地内存不足情况时启动nimbus可能会抛出异常:
Error occurred during initialization of VM
Could not reserve enough space for object heap
只需要在conf/storm.yaml里配置:
nimbus.childopts: “-Xmx256m”
supervisor.childopts: “-Xmx256m”
worker.childopts: “-Xmx128m”
其中大小可根据实际情况配置
5、UI
前提:tomcat 7.0 或以上版本;
将jstorm-ui-0.7.1.war复制到tomcat的webapps目录下;
6、启动JStorm
启动zookeeper:进入zookeeper目录,执行bin/zkServer.sh start
启动Nimbus:进入JStorm目录,执行bin/jstorm nimbus
启动Supervisor:进入JStorm目录,执行bin/jstorm supervisor
启动Tomcat:进入Tomcat目录,执行bin/startup.sh
四、JStorm HelloWorld
1、编写源码
这个例子取自:github
HelloWorldTopology.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package storm.cookbook;
import backtype.storm.Config;
import backtype.storm.StormSubmitter;
import backtype.storm.generated.AlreadyAliveException;
import backtype.storm.generated.InvalidTopologyException;
import backtype.storm.topology.TopologyBuilder;
import backtype.storm.utils.Utils;
/**
* Author: ashrith
* Date: 8/26/13
* Time: 12:03 PM
* Desc: setup the topology and submit it to either a local of remote Storm cluster depending on the arguments
* passed to the main method.
*/
public class HelloWorldTopology {
/*
* main class in which to define the topology and a LocalCluster object (enables you to test and debug the
* topology locally). In conjunction with the Config object, LocalCluster allows you to try out different
* cluster configurations.
*
* Create a topology using 'TopologyBuilder' (which will tell storm how the nodes area arranged and how they
* exchange data)
* The spout and the bolts are connected using 'ShuffleGroupings'
*
* Create a 'Config' object containing the topology configuration, which is merged with the cluster configuration
* at runtime and sent to all nodes with the prepare method
*
* Create and run the topology using 'createTopology' and 'submitTopology'
*/
public static void main(String[] args) throws AlreadyAliveException, InvalidTopologyException {
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("randomHelloWorld", new HelloWorldSpout(), 10);
builder.setBolt("HelloWorldBolt", new HelloWorldBolt(), 1).shuffleGrouping("randomHelloWorld");
Config conf = new Config();
conf.put(Config.NIMBUS_HOST, "localhost");
conf.put(Config.NIMBUS_THRIFT_PORT, 6627);
conf.setDebug(true);
if(args!=null && args.length > 0) {
conf.setNumWorkers(3);
StormSubmitter.submitTopology(args[0], conf, builder.createTopology());
} else {
}
}
}

HelloWorldSpout.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package storm.cookbook;
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;
import java.util.Map;
import java.util.Random;
/**
* Author: ashrith
* Date: 8/21/13
* Time: 8:33 PM
* Desc: spout essentially emits a stream containing 1 of 2 sentences 'Other Random Word' or 'Hello World' based on
* random probability. It works by generating a random number upon construction and then generating subsequent
* random numbers to test against the original member variable's value. When it matches "Hello World" is emitted,
* during the remaining executions the other sentence is emitted.
*/
public class HelloWorldSpout extends BaseRichSpout{
private SpoutOutputCollector collector;
private int referenceRandom;
private static final int MAX_RANDOM = 10;
public HelloWorldSpout() {
final Random rand = new Random();
referenceRandom = rand.nextInt(MAX_RANDOM);
}
/*
* declareOutputFields() => you need to tell the Storm cluster which fields this Spout emits within the
* declareOutputFields method.
*/
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("sentence"));
}
/*
* open() => The first method called in any spout is 'open'
* TopologyContext => contains all our topology data
* SpoutOutputCollector => enables us to emit the data that will be processed by the bolts
* conf => created in the topology definition
*/
@Override
public void open(Map conf, TopologyContext topologyContext, SpoutOutputCollector collector) {
this.collector = collector;
}
/*
* nextTuple() => Storm cluster will repeatedly call the nextTuple method which will do all the work of the spout.
* nextTuple() must release the control of the thread when there is no work to do so that the other methods have
* a chance to be called.
*/
@Override
public void nextTuple() {
final Random rand = new Random();
int instanceRandom = rand.nextInt(MAX_RANDOM);
if(instanceRandom == referenceRandom){
collector.emit(new Values("Hello World"));
} else {
collector.emit(new Values("Other Random Word"));
}
}
}

HelloWorldBolt.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package storm.cookbook;
import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichBolt;
import backtype.storm.tuple.Tuple;
import java.util.Map;
/**
* Author: ashrith
* Date: 8/26/13
* Time: 11:48 AM
* Desc: This bolt will consume the produced Tuples from HelloWorldSpout and implement the required counting logic
*/
public class HelloWorldBolt extends BaseRichBolt {
private int myCount = 0;
/*
* prepare() => on create
*/
@Override
public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {
}
/*
* execute() => most important method in the bolt is execute(Tuple input), which is called once per tuple received
* the bolt may emit several tuples for each tuple received
*/
@Override
public void execute(Tuple tuple) {
String test = tuple.getStringByField("sentence");
if(test == "Hello World"){
myCount++;
System.out.println("Found a Hello World! My Count is now: " + Integer.toString(myCount));
}
}
/*
* declareOutputFields => This bolt emits nothing hence no body for declareOutputFields()
*/
@Override
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
}
}

2、提交Topology
上述源码编译打包Helloworld.jar后提交到jstorm集群:
bin/jstorm jar Helloworld.jar storm.cookbook.HelloWorldTopology HelloWorld
其中参数[HelloWorld]为TopologyName
3.查看Topology运行状况
通过ui等途径可以查看Topology的执行情况。
五、结语
本节简单介绍了JStorm单机环境的搭建,用供初学者搭建单机JStorm,并能够编写HelloWolrd,生产环境集群搭建仅做参考,详细配置建议查询相关文档。
六、参考文档
[1]https://github.com/alibaba/jstorm/wiki
[2]https://github.com/nathanmarz/storm/wiki

http://hexiaoqiao.sinaapp.com/2014/06/09/jstorm%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/

JStorm环境搭建的更多相关文章

  1. 分布式实时日志系统(一)环境搭建之 Jstorm 集群搭建过程/Jstorm集群一键安装部署

    最近公司业务数据量越来越大,以前的基于消息队列的日志系统越来越难以满足目前的业务量,表现为消息积压,日志延迟,日志存储日期过短,所以,我们开始着手要重新设计这块,业界已经有了比较成熟的流程,即基于流式 ...

  2. 分布式实时日志系统(二) 环境搭建之 flume 集群搭建/flume ng资料

    最近公司业务数据量越来越大,以前的基于消息队列的日志系统越来越难以满足目前的业务量,表现为消息积压,日志延迟,日志存储日期过短,所以,我们开始着手要重新设计这块,业界已经有了比较成熟的流程,即基于流式 ...

  3. .NET Core系列 : 1、.NET Core 环境搭建和命令行CLI入门

    2016年6月27日.NET Core & ASP.NET Core 1.0在Redhat峰会上正式发布,社区里涌现了很多文章,我也计划写个系列文章,原因是.NET Core的入门门槛相当高, ...

  4. Azure Service Fabric 开发环境搭建

    微服务体系结构是一种将服务器应用程序构建为一组小型服务的方法,每个服务都按自己的进程运行,并通过 HTTP 和 WebSocket 等协议相互通信.每个微服务都在特定的界定上下文(每服务)中实现特定的 ...

  5. rnandroid环境搭建

    react-native 环境搭建具体步骤这个大家已经玩烂了,这个主要是记录下来自己做win7系统遇到的坑 1.com.android.ddmlib.installexception 遇到这个问题,在 ...

  6. python开发环境搭建

    虽然网上有很多python开发环境搭建的文章,不过重复造轮子还是要的,记录一下过程,方便自己以后配置,也方便正在学习中的同事配置他们的环境. 1.准备好安装包 1)上python官网下载python运 ...

  7. springMVC初探--环境搭建和第一个HelloWorld简单项目

    注:此篇为学习springMVC时,做的笔记整理. MVC框架要做哪些事情? a,将url映射到java类,或者java类的方法上 b,封装用户提交的数据 c,处理请求->调用相关的业务处理—& ...

  8. 【定有惊喜】android程序员如何做自己的API接口?php与android的良好交互(附环境搭建),让前端数据动起来~

    一.写在前面 web开发有前端和后端之分,其实android还是有前端和后端之分.android开发就相当于手机app的前端,一般都是php+android或者jsp+android开发.androi ...

  9. Nexus(一)环境搭建

    昨天,成功搭建了自己的 Maven 环境(详见:Maven(一)环境搭建),今天就来研究和探讨下 Nexus 的搭建! 使用背景: 安装环境:Windows 10 -64位 JDK版本:1.7 Mav ...

随机推荐

  1. python笔记19-获取当前运行函数名称和类方法名称

    前言 写完代码之后,一般为了方便查看日志,可以在日志输出中加入当前运行的函数名称或类和方法名称,以便于代码报错的时候能快速找到报错的是哪个函数或方法. 那么如何获取当前运行函数(或方法)的名称呢? 获 ...

  2. 关于linux的进程中的各个线程cpu占用情况的分析和查看

    我们常常会在新开的服搭建一个游戏的server,有时候要进行压力測试,那么怎样来看呢,一般我们会通过top命令查看各个进程的cpu和内存占用情况,获得到了我们的进程id,然后我们或许会通过pstack ...

  3. [Gradle] Gradle 构建工具的未来

    转载地址:http://www.infoq.com/cn/news/2011/04/xxb-maven-6-gradle Maven面临的挑战 软件行业新旧交替的速度之快往往令人咂舌,不用多少时间,你 ...

  4. linux如何后台运行进程,而且不随终端关闭而关闭

    参考:http://www.cnblogs.com/kaituorensheng/p/3980334.html 使用命令nohup:no hang up,表示关闭终端后,进程并不随着终端关闭而关闭 f ...

  5. 当Windows Server 2012的主DC出了问题, 如何迁移其上的FSMO角色?

    步骤如下: 1. 遵循https://support.microsoft.com/kb/255504中的transfer FSMO的步骤. 2. 这样做之后还没完. 因为Windows Server ...

  6. 使用javascript比较任意两个日期相差天数(代码)

    avascript比较两个日期相差天数,可返回负值,表示后者大于前者. 该函数在IE,firefox下均通过测试. /** 比较两个日期相差的天数,可为负值 **/ function DateDiff ...

  7. JS性能优化之创建文档碎片(document.createDocumentFragment)

    讲这个方法之前,我们应该先了解下插入节点时浏览器会做什么.         在浏览器中,我们一旦把节点添加到document.body(或者其他节点)中,页面就会更新并反映出这个变化,对于少量的更新, ...

  8. linux 基本命令学习

    原文: https://www.oschina.net/translate/useful-linux-commands-for-newbies?lang=chs&p=2

  9. (转)akka Router实例

    通常在分布式任务调度系统中会有这样的需求:一组actor提供相同的服务,我们在调用任务的时候只需要选择其中一个actor进行处理即可. 其实这就是一个负载均衡或者说路由策略,akka作为一个高性能支持 ...

  10. centos版本7以上网卡名修改

    1.初始状态网卡 2.首先,先编辑网卡的配置文件 vi /etc/sysconfig/network-scripts/ifcfg-eno16777736 将里面的NAME项修改为eth0 3.然后,禁 ...