原文: 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. aop 切面demo

    /** * 必须要@Aspect 和 @Component一起使用否则没法拦截通知 * 搞了好久才明白刚刚开始以为时execution里面的配置的问题 * AOP使用很简单的 */@Aspect@Co ...

  2. [ HAOI 2010 ] 最长公共子序列

    \(\\\) \(Description\) 求两个长度\(\le5000\)的大写字母串的\(LCS\)长度及个数,定义两\(LCS\)中某一字符在两序列出现位置有一处不同就视为不同. \(\\\) ...

  3. 去除IOS苹果手机自带按钮样式的问题~

    input[type="button"], input[type="submit"], input[type="reset"] { -web ...

  4. STL之vector篇

    #include<iostream> #include<cstdio> #include<cstring> #include<vector> #incl ...

  5. Caffe RPN :error C2220: warning treated as error - no 'object' file generated

    在 caffe里面添加rpn_layer.cpp之后,总是出现 error C2220: warning treated as error - no 'object' file generated 这 ...

  6. struts2_validate表单验证

    使用代码实现 验证功能 (也就是重写ActionSupport中的validate()方法) 在validate方法中进行编写我们需要的验证功能 这里需要提几点的就是: 1.当我们需要对action中 ...

  7. 用python写一个百度翻译

    运行环境: python 3.6.0 今天处于练习的目的,就用 python 写了一个百度翻译,是如何做到的呢,其实呢就是拿到接口,通过这个接口去访问,不过中间确实是出现了点问题,不过都解决掉了 先晾 ...

  8. Oracle行转列/列转行

    1.oracle的pivot函数 原表 使用pivot函数: with temp as(select '四川省' nation ,'成都市' city,'第一' ranking from dual u ...

  9. 集成学习_Bagging 和随机森林(rf)

       集成学习方式总共有3种:bagging-(RF).boosting-(GBDT/Adaboost/XGBOOST).stacking      下面将对Bagging 进行介绍:(如下图所示) ...

  10. Linux系统学习之 一:新手必须掌握的Linux命令1

    2018-10-03 16:04:12 一.常用系统工作命令 1.wget 命令 作用:用于在终端中下载网络文件. 格式:wget [参数] 下载地址 参数及作用: -b : 后台下载模式 -d:显示 ...