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

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

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

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

  1. mvn archetype:generate -DgroupId=org.csfreebird -DartifactId=hello_storm -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

编辑pom.xml,添加dependency

  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的代码,改变了_引导的成员变量命名方法

  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. }

编译成功

  1. mvn clean compile

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

  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>

然后运行命令

    1. mvn compile exec:java -Dstorm.topology=org.csfreebird.HelloTopology

【转】storm 开发系列一 第一个程序的更多相关文章

  1. BizTalk开发系列(二) "Hello World" 程序搬运文件

    我们在<QuickLearn BizTalk系列之"Hello World">里讲到了如何快速的开发第一个BizTalk 应用程序.现在我们来讲一下如何把这个程序改成用 ...

  2. windows phone 8 开发系列(三)程序清单说明与配置

    一 清单文件内容介绍 当我们先建了一个项目之后,我们可以看到vs自动会为我们创建了很多文件,正常人都会先一个个去翻看下每个文件都是干啥的,都主要写了些啥,在这些文件中,在Properies目录下面,我 ...

  3. pygame系列_第一个程序_图片代替鼠标移动

    想想现在学校pygame有几个钟了,就写了一个小程序:图片代替鼠标移动 程序的运行效果: 当鼠标移动到窗口内,鼠标不见了,取而代之的是图片..... ========================= ...

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

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

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

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

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

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

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

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

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

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

  9. windows phone 8 开发系列(二)Hello Wp8!

    上篇我们了解了WP8的环境搭建,从今天开始,我们就正式进入WP8的设计,开发阶段. 一. 项目模板介绍 打开vs,选择Windows Phone的项目模板,我们发现如下有很多模板,那么我们就从认识这些 ...

随机推荐

  1. oracle数据库忘记用户名和密码莫着急

    刚安装完Oracle 11g后,登录的时候没有记住用户名和密码,解决方法:新建一个用户 第一步:以系统身份登录 cmd--->sqlplus 提示输入用户名,然后输入sqlplus/as sys ...

  2. centos7安装mysql和mysql-connector-c++

    最近为了搭建自己的开发环境,又一次在centos7上面开始安装mysql和c++的访问环境,特此记录一下搭建过程,方便以后查阅 一.安装mysql centos7 默认安装了mariaDB,导致不能安 ...

  3. Andorid Scrolling Activity(CoordinatorLayout详情)

    1.new project -> Scrolling Activity 2.Layout xml code activity_scrolling.xml <?xml version=&qu ...

  4. 【转】升级还是权谋?从USB PD 2.0到3.0

    原文出处 http://www.eetop.cn/blog/html/43/n-433743.html 如同iPhone的出现,才让智能机真正主导手机市场一样,Type-C口发布后,USB PD才正式 ...

  5. html5——:hover事件触发自己的:afert伪元素事件

    :hover事件触发自己的:afert伪元素事件中间是没有空格的

  6. JS——行内式注册事件

    html中行内调用function的时候,是通过window调用的function,所以打印this等于打印window,所以在使用行内注册事件时务必传入参数this <!DOCTYPE htm ...

  7. (转)Hibernate关联映射——一对多(多对一)

    http://blog.csdn.net/yerenyuan_pku/article/details/70152173 Hibernate关联映射——一对多(多对一) 我们以客户(Customer)与 ...

  8. Style在Android中的继承关系

    Style在Android中的继承关系 Android的Styles(样式)和Themes(主题)非常类似Web开发里的CSS,方便开发者将页面内容和布局呈现分开.Style和Theme在Androi ...

  9. jenkins执行python脚本

    参考: https://blog.csdn.net/qq_39247153/article/details/81003244 https://blog.csdn.net/huashao0602/art ...

  10. iptables详解(2):iptables实际操作之规则查询

    所属分类:IPtables  Linux基础 在阅读这篇文章之前,请确保你已经阅读了如下文章,如下文章总结了iptables的相关概念,是阅读这篇文章的基础. 图文并茂理解iptables 如果你是一 ...