原文: http://blog.csdn.net/csfreebird/article/details/49104777

-------------------------------------------------------------------------------------------------

本文将在本地开发环境创建一个storm程序,力求简单。

首先用mvn创建一个简单的工程hello_storm

[plain] view plaincopy print?
  1. mvn archetype:generate -DgroupId=org.csfreebird -DartifactId=hello_storm -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

编辑pom.xml,添加dependency

[html] view plaincopy print?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>org.csfreebird</groupId>
  5. <artifactId>hello_storm</artifactId>
  6. <version>0.9.5</version>
  7. <packaging>jar</packaging>
  8. <name>hello_storm</name>
  9. <url>http://maven.apache.org</url>
  10. <dependencies>
  11. <dependency>
  12. <groupId>org.apache.storm</groupId>
  13. <artifactId>storm-core</artifactId>
  14. <version>${project.version}</version>
  15. <!-- keep storm out of the jar-with-dependencies -->
  16. <scope>provided</scope>
  17. </dependency>
  18. </dependencies>
  19. </project>

provided 表示storm-core的jar包只作为编译和测试时使用,在集群环境下运行时完全依赖集群环境的storm-core的jar包。

然后重命名App.Java为HelloTopology.java文件,开始编码。模仿之前的Example, 这里将所有的spout/bolt类都作为静态类定义,就放在HelloTopology.java文件。

功能如下

编写HelloTopology.java代码,spout代码来自于TestWordSpout,去掉了log的代码,改变了_引导的成员变量命名方法

[plain] view plaincopy print?
  1. package org.csfreebird;
  2. import backtype.storm.Config;
  3. import backtype.storm.LocalCluster;
  4. import backtype.storm.StormSubmitter;
  5. import backtype.storm.task.OutputCollector;
  6. import backtype.storm.task.TopologyContext;
  7. import backtype.storm.testing.TestWordSpout;
  8. import backtype.storm.topology.OutputFieldsDeclarer;
  9. import backtype.storm.topology.TopologyBuilder;
  10. import backtype.storm.topology.base.BaseRichBolt;
  11. import backtype.storm.topology.base.BaseRichSpout;
  12. import backtype.storm.tuple.Fields;
  13. import backtype.storm.tuple.Tuple;
  14. import backtype.storm.tuple.Values;
  15. import backtype.storm.utils.Utils;
  16. import backtype.storm.spout.SpoutOutputCollector;
  17. import java.util.Map;
  18. import java.util.TreeMap;
  19. import java.util.Random;
  20. public class HelloTopology {
  21. public static class HelloSpout extends BaseRichSpout {
  22. boolean isDistributed;
  23. SpoutOutputCollector collector;
  24. public HelloSpout() {
  25. this(true);
  26. }
  27. public HelloSpout(boolean isDistributed) {
  28. this.isDistributed = isDistributed;
  29. }
  30. public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
  31. this.collector = collector;
  32. }
  33. public void close() {
  34. }
  35. public void nextTuple() {
  36. Utils.sleep(100);
  37. final String[] words = new String[] {"china", "usa", "japan", "russia", "england"};
  38. final Random rand = new Random();
  39. final String word = words[rand.nextInt(words.length)];
  40. this.collector.emit(new Values(word));
  41. }
  42. public void ack(Object msgId) {
  43. }
  44. public void fail(Object msgId) {
  45. }
  46. public void declareOutputFields(OutputFieldsDeclarer declarer) {
  47. declarer.declare(new Fields("word"));
  48. }
  49. @Override
  50. public Map<String, Object> getComponentConfiguration() {
  51. if(!this.isDistributed) {
  52. Map<String, Object> ret = new TreeMap<String, Object>();
  53. ret.put(Config.TOPOLOGY_MAX_TASK_PARALLELISM, 1);
  54. return ret;
  55. } else {
  56. return null;
  57. }
  58. }
  59. }
  60. public static class HelloBolt extends BaseRichBolt {
  61. OutputCollector collector;
  62. @Override
  63. public void prepare(Map conf, TopologyContext context, OutputCollector collector) {
  64. this.collector = collector;
  65. }
  66. @Override
  67. public void execute(Tuple tuple) {
  68. this.collector.emit(tuple, new Values("hello," + tuple.getString(0)));
  69. this.collector.ack(tuple);
  70. }
  71. @Override
  72. public void declareOutputFields(OutputFieldsDeclarer declarer) {
  73. declarer.declare(new Fields("word"));
  74. }
  75. }
  76. public static void main(String[] args) throws Exception {
  77. TopologyBuilder builder = new TopologyBuilder();
  78. builder.setSpout("a", new HelloSpout(), 10);
  79. builder.setBolt("b", new HelloBolt(), 5).shuffleGrouping("a");
  80. Config conf = new Config();
  81. conf.setDebug(true);
  82. if (args != null && args.length > 0) {
  83. conf.setNumWorkers(3);
  84. StormSubmitter.submitTopologyWithProgressBar(args[0], conf, builder.createTopology());
  85. } else {
  86. String test_id = "hello_test";
  87. LocalCluster cluster = new LocalCluster();
  88. cluster.submitTopology(test_id, conf, builder.createTopology());
  89. Utils.sleep(10000);
  90. cluster.killTopology(test_id);
  91. cluster.shutdown();
  92. }
  93. }
  94. }

编译成功

[plain] view plaincopy print?
  1. mvn clean compile

为了能够在本地模式运行,需要在pom.xml中添加如下:

[html] view plaincopy print?
  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.codehaus.mojo</groupId>
  5. <artifactId>exec-maven-plugin</artifactId>
  6. <version>1.2.1</version>
  7. <executions>
  8. <execution>
  9. <goals>
  10. <goal>exec</goal>
  11. </goals>
  12. </execution>
  13. </executions>
  14. <configuration>
  15. <executable>java</executable>
  16. <includeProjectDependencies>true</includeProjectDependencies>
  17. <includePluginDependencies>false</includePluginDependencies>
  18. <classpathScope>compile</classpathScope>
  19. <mainClass>${storm.topology}</mainClass>
  20. </configuration>
  21. </plugin>
  22. </plugins>
  23. </build>

然后运行命令

[plain] view plaincopy print?
  1. mvn compile exec:java -Dstorm.topology=org.csfreebird.HelloTopology

【转】storm 开发系列一 第一个程序相关推荐

  1. Java零基础系列001——第一个程序

    Java零基础系列001--第一个程序 public class Welcome {public static void main(String[] args) {//args为arguments的缩 ...

  2. 嵌入式linux/鸿蒙开发板(IMX6ULL)开发(八)IMX6ULL开发板编译第一个程序以及驱动

    文章目录 1. IMX6ULL开发板初次操作 1.1 100ask_imx6ull开发板硬件资源简介 1.1.1 100ask imx6ull mini开发板 1.2 100ask_imx6ull开发 ...

  3. 手机安全卫士开发系列(6)——程序主界面

    主界面的布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...

  4. 微信小程序开发系列七:微信小程序的页面跳转

    微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...

  5. 微信小程序开发系列四:微信小程序之控制器的初始化逻辑

    微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 这个教程的前两篇文章,介绍了如何 ...

  6. 微信小程序开发系列六:微信框架API的调用

    微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...

  7. 微信小程序开发系列五:微信小程序中如何响应用户输入事件

    微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...

  8. 微信小程序开发系列教程三:微信小程序的调试方法

    微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 这个教程的前两篇文章,介绍了如何用下图所示的微信开发者工具自动生成一个Hel ...

  9. 蓝桥ROS机器人之C++基础开发第一个程序

    C++学习资料_zhangrelay的博客-CSDN博客 简介/入门 0.1这些教程的介绍 0.2编程语言简介 0.3C/C++ 简介 0.4C++开发简介 0.5编译器.链接器和库简介 0.6安装集 ...

最新文章

  1. 开发日记-20190404
  2. Delphi数据类型及转换(附:源码)
  3. Android 5.0 Usb调试拦截分析及修改
  4. track文件 什么是git_git常用命令
  5. drools规则引擎因为内存泄露导致的内存溢出
  6. uva11361数位dp
  7. C/C++定时器制作
  8. Visio studio 2015企业版,汉语版下载,安装,破解,搞定了
  9. 【转】64位ORACLE客户端上plsql无法识别ORACLE_HOME解决方案
  10. amos看拟合度在哪里看_AMOS分析技术:结构方程模型的拟合度评价指标
  11. 基于java的教师教学评价管理系统
  12. EKT多链技术丨多链会形成新的中心化吗
  13. js 格式化prettier配置_代码格式化工具---prettier配置
  14. 为什么走线选择50欧姆阻抗
  15. python 添加半透明水印_如何利用python给图片添加半透明水印
  16. 南京工业大学乐学python答案_乐学Python
  17. Lamport 逻辑时钟
  18. 五粮液:绩优蓝筹稳步填权
  19. Flutter高仿微信-第31篇-单聊-表情
  20. openstack cinder

热门文章

  1. ribbon客户端的负载均衡
  2. python六:常见数据类型以及常见用法
  3. 年报系统课堂讨论记录
  4. Zookeeper详解(一):分布式与Zookeeper
  5. 说说JavaScriptCore
  6. 关于linux特殊重定向的理解
  7. Word中的字体大小
  8. mysql-connector-net不同版本下载
  9. linux中间隔10ping一次脚本,linux批量ping脚本shell
  10. php5.2.10安装_安装 | 起步 | Laravel 5.2 中文文档