wordcount

介绍开发一个wordcount 计算任务运行在tstorm上

1. 创建maven 项目

生成一个maven 项目。 编译的java 版本 使用1.8

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>UTF8</encoding>
    </configuration>
</plugin>

2. 引入依赖

Tstorm基于storm 0.9.6 版本,所以依赖的storm-core 建议使用0.9.6 如果本地调试模块,依赖scope 必须选用 compile。如果提交到生产环境,依赖scope 使用provided。

<dependency>
    <groupId>org.apache.storm</groupId>
    <artifactId>storm-core</artifactId>
    <version>0.9.6</version>
    <scope>provided</scope>
    <!-- 如果本地调试模块 ,必须选用 compile,如果提交到生产环境使用provided-->
</dependency>

3. 编写topology

3.1 编写入口类

topology 入口就是一个普通的java 程序。

3.2 实现spout

spout 通常继承 BaseRichSpout或者实现IRichSpout接口 并重写 nextTuple 方法

3.3 实现blot

blot 通常是继承 BaseRichBolt,BaseBasicBolt 或实现 IRichBolt 接口 并重写execute 方法

更多开发实例参考:https://github.com/apache/storm/tree/v0.9.6/examples/storm-starter 更多storm 接口参考:http://storm.apache.org/releases/0.9.6/index.html

4. 打包jar

在pom.xml 文件中添加打包插件

通过maven 打包命令,会将项目打包成一个可执行jar 。

5. 本地调试

建议用户使用IntelliJ IDEA 进行本地调试,直接运行main 函数,会在本地嵌入运行一个storm 环境,方便用户调试核心逻辑。

6. 提交topology到集群

通过后台提交topology,需要切到nimbus 节点。 上传打包好的jar,切到系统用户jstorm。 使用提交命令格式如下: /usr/local/jstorm/bin/storm jar topology.jar main.class topologyName 例如:

7. 查看任务和查看日志

通过后台命令(/usr/local/jstorm/bin/storm list) 查看提交topology 状态(使用jstorm 用户)。 也可以使用storm ui 查看topology 运行状态和日志。访问地址为 (http://portalIP:8080/tstorm)

8. 停止top

通过后台命令杀死对应的topology /usr/local/jstorm/bin/storm kill topologyName

9. 更多

更多后台操作命令使用: /usr/local/jstorm/bin/storm help 提交topology也可以通过工作流中的storm任务。

Last updated

Was this helpful?