1. import backtype.storm.spout.SpoutOutputCollector;
  2. import backtype.storm.task.TopologyContext;
  3. import backtype.storm.topology.base.BaseRichSpout;
  4. import backtype.storm.utils.Utils;
  5. import backtype.storm.Config;
  6. import backtype.storm.LocalCluster;
  7. import backtype.storm.StormSubmitter;
  8. import backtype.storm.task.ShellBolt;
  9. import backtype.storm.topology.BasicOutputCollector;
  10. import backtype.storm.topology.IRichBolt;
  11. import backtype.storm.topology.OutputFieldsDeclarer;
  12. import backtype.storm.topology.TopologyBuilder;
  13. import backtype.storm.topology.base.BaseBasicBolt;
  14. import backtype.storm.tuple.Fields;
  15. import backtype.storm.tuple.Tuple;
  16. import backtype.storm.tuple.Values;
  17. import java.util.*;
  18. //import java.util.HashMap;
  19. //import java.util.Map;
  20. //import java.util.Random;
  21. //import java.util.StringTokenizer;
  22. /*
  23. ** WordCountTopolopgyAllInJava类(单词计数)
  24. */
  25. public class  WordCountTopolopgyAllInJava{
  26. // 定义一个喷头,用于产生数据。该类继承自BaseRichSpout
  27. public static class RandomSentenceSpout extends BaseRichSpout {
  28. SpoutOutputCollector _collector;
  29. Random _rand;
  30. @Override
  31. public void open(Map conf, TopologyContext context, SpoutOutputCollector collector){
  32. _collector = collector;
  33. _rand = new Random();
  34. }
  35. @Override
  36. public void nextTuple(){
  37. // 睡眠一段时间后再产生一个数据
  38. Utils.sleep(100);
  39. // 句子数组
  40. String[] sentences = new String[]{ "the cow jumped over the moon", "an apple a day keeps the doctor away",
  41. "four score and seven years ago", "snow white and the seven dwarfs", "i am at two with nature" };
  42. // 随机选择一个句子
  43. String sentence = sentences[_rand.nextInt(sentences.length)];
  44. // 发射该句子给Bolt
  45. _collector.emit(new Values(sentence));
  46. }
  47. // 确认函数
  48. @Override
  49. public void ack(Object id){
  50. }
  51. // 处理失败的时候调用
  52. @Override
  53. public void fail(Object id){
  54. }
  55. @Override
  56. public void declareOutputFields(OutputFieldsDeclarer declarer){
  57. // 定义一个字段word
  58. declarer.declare(new Fields("word"));
  59. }
  60. }
  61. // 定义个Bolt,用于将句子切分为单词
  62. public static class SplitSentence extends BaseBasicBolt{
  63. @Override
  64. public void execute(Tuple tuple, BasicOutputCollector collector){
  65. // 接收到一个句子
  66. String sentence = tuple.getString(0);
  67. // 把句子切割为单词
  68. StringTokenizer iter = new StringTokenizer(sentence);
  69. // 发送每一个单词
  70. while(iter.hasMoreElements()){
  71. collector.emit(new Values(iter.nextToken()));
  72. }
  73. }
  74. @Override
  75. public void declareOutputFields(OutputFieldsDeclarer declarer){
  76. // 定义一个字段
  77. declarer.declare(new Fields("word"));
  78. }
  79. }
  80. // 定义一个Bolt,用于单词计数
  81. public static class WordCount extends BaseBasicBolt {
  82. Map<String, Integer> counts = new HashMap<String, Integer>();
  83. @Override
  84. public void execute(Tuple tuple, BasicOutputCollector collector){
  85. // 接收一个单词
  86. String word = tuple.getString(0);
  87. // 获取该单词对应的计数
  88. Integer count = counts.get(word);
  89. if(count == null)
  90. count = 0;
  91. // 计数增加
  92. count++;
  93. // 将单词和对应的计数加入map中
  94. counts.put(word,count);
  95. System.out.println("hello word!");
  96. System.out.println(word +"  "+count);
  97. // 发送单词和计数(分别对应字段word和count)
  98. collector.emit(new Values(word, count));
  99. }
  100. @Override
  101. public void declareOutputFields(OutputFieldsDeclarer declarer){
  102. // 定义两个字段word和count
  103. declarer.declare(new Fields("word","count"));
  104. }
  105. }
  106. public static void main(String[] args) throws Exception
  107. {
  108. // 创建一个拓扑
  109. TopologyBuilder builder = new TopologyBuilder();
  110. // 设置Spout,这个Spout的名字叫做"Spout",设置并行度为5
  111. builder.setSpout("Spout", new RandomSentenceSpout(), 5);
  112. // 设置slot——“split”,并行度为8,它的数据来源是spout的
  113. builder.setBolt("split", new SplitSentence(), 8).shuffleGrouping("spout");
  114. // 设置slot——“count”,你并行度为12,它的数据来源是split的word字段
  115. builder.setBolt("count", new WordCount(), 12).fieldsGrouping("split", new Fields("word"));
  116. Config conf = new Config();
  117. conf.setDebug(false);
  118. //if(args != null && args.length > 0){
  119. //if(false){
  120. //  conf.setNumWorkers(3);
  121. //  StormSubmitter.submitTopology(args[0], conf, builder.createTopology());
  122. //}else{
  123. conf.setMaxTaskParallelism(3);
  124. // 本地集群
  125. LocalCluster cluster = new LocalCluster();
  126. // 提交拓扑(该拓扑的名字叫word-count)
  127. cluster.submitTopology("word-count", conf, builder.createTopology() );
  128. Thread.sleep(10000);
  129. //  cluster.shutdown();
  130. //}
  131. }
  132. }

使用maven编译该项目: mvn clean package

运行:storm jar word-count-1.0.jar WordCountTopolopgyAllInJava

结果如下:

hello word!
moon    811
hello word!
an      829
hello word!
apple   829
hello word!
a       829
hello word!
keeps   829
hello word!
day     829
hello word!
score   800
hello word!

pom.xml文件定义如下

    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/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0</modelVersion>
    4. <groupId>storm-yqj</groupId>
    5. <artifactId>word-count</artifactId>
    6. <version>1.0</version>
    7. <packaging>jar</packaging>
    8. <name>word-count</name>
    9. <url>http://maven.apache.org</url>
    10. <properties>
    11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    12. </properties>
    13. <dependencies>
    14. <dependency>
    15. <groupId>junit</groupId>
    16. <artifactId>junit</artifactId>
    17. <version>3.8.1</version>
    18. <scope>test</scope>
    19. </dependency>
    20. <dependency>
    21. <groupId>org.testng</groupId>
    22. <artifactId>testng</artifactId>
    23. <version>6.8.5</version>
    24. <scope>test</scope>
    25. </dependency>
    26. <dependency>
    27. <groupId>org.mockito</groupId>
    28. <artifactId>mockito-all</artifactId>
    29. <version>1.9.0</version>
    30. <scope>test</scope>
    31. </dependency>
    32. <dependency>
    33. <groupId>org.easytesting</groupId>
    34. <artifactId>fest-assert-core</artifactId>
    35. <version>2.0M8</version>
    36. <scope>test</scope>
    37. </dependency>
    38. <dependency>
    39. <groupId>org.jmock</groupId>
    40. <artifactId>jmock</artifactId>
    41. <version>2.6.0</version>
    42. <scope>test</scope>
    43. </dependency>
    44. <dependency>
    45. <groupId>org.apache.storm</groupId>
    46. <artifactId>storm-core</artifactId>
    47. <version>0.9.1-incubating</version>
    48. </dependency>
    49. <dependency>
    50. <groupId>commons-collections</groupId>
    51. <artifactId>commons-collections</artifactId>
    52. <version>3.2.1</version>
    53. </dependency>
    54. <dependency>
    55. <groupId>com.google.guava</groupId>
    56. <artifactId>guava</artifactId>
    57. <version>15.0</version>
    58. </dependency>
    59. </dependencies>
    60. <build>
    61. <resources>
    62. <resource>
    63. <directory>${basedir}/multilang</directory>
    64. </resource>
    65. </resources>
    66. <plugins>
    67. <plugin>
    68. <artifactId>maven-assembly-plugin</artifactId>
    69. <configuration>
    70. <descriptorRefs>
    71. <descriptorRef>jar-with-dependencies</descriptorRef>
    72. </descriptorRefs>
    73. <archive>
    74. <manifest>
    75. <mainClass></mainClass>
    76. </manifest>
    77. </archive>
    78. </configuration>
    79. <executions>
    80. <execution>
    81. <id>make-assembly</id>
    82. <phase>package</phase>
    83. <goals>
    84. <goal>single</goal>
    85. </goals>
    86. </execution>
    87. </executions>
    88. </plugin>
    89. <plugin>
    90. <groupId>com.theoryinpractise</groupId>
    91. <artifactId>clojure-maven-plugin</artifactId>
    92. <version>1.3.12</version>
    93. <extensions>true</extensions>
    94. <configuration>
    95. <sourceDirectories>
    96. <sourceDirectory>src/clj</sourceDirectory>
    97. </sourceDirectories>
    98. </configuration>
    99. <executions>
    100. <execution>
    101. <id>compile</id>
    102. <phase>compile</phase>
    103. <goals>
    104. <goal>compile</goal>
    105. </goals>
    106. </execution>
    107. <execution>
    108. <id>test</id>
    109. <phase>test</phase>
    110. <goals>
    111. <goal>test</goal>
    112. </goals>
    113. </execution>
    114. </executions>
    115. </plugin>
    116. <plugin>
    117. <groupId>org.codehaus.mojo</groupId>
    118. <artifactId>exec-maven-plugin</artifactId>
    119. <version>1.2.1</version>
    120. <executions>
    121. <execution>
    122. <goals>
    123. <goal>exec</goal>
    124. </goals>
    125. </execution>
    126. </executions>
    127. <configuration>
    128. <executable>java</executable>
    129. <includeProjectDependencies>true</includeProjectDependencies>
    130. <includePluginDependencies>false</includePluginDependencies>
    131. <classpathScope>compile</classpathScope>
    132. <mainClass>${storm.topology}</mainClass>
    133. </configuration>
    134. </plugin>
    135. <plugin>
    136. <groupId>org.apache.maven.plugins</groupId>
    137. <artifactId>maven-compiler-plugin</artifactId>
    138. <configuration>
    139. <source>1.6</source>
    140. <target>1.6</target>
    141. </configuration>
    142. </plugin>
    143. </plugins>
    144. </build>
    145. </project>

Storm完整例子的更多相关文章

  1. C#调用存储过程简单完整例子

    CREATE PROC P_TEST@Name VARCHAR(20),@Rowcount INT OUTPUTASBEGIN SELECT * FROM T_Customer WHERE NAME= ...

  2. 使用Connector/C++(VS2015)连接MySQL的完整例子

    完整示例代码1 /* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved. This program is ...

  3. 基于Shiro,JWT实现微信小程序登录完整例子

    小程序官方流程图如下,官方地址 : https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html ...

  4. C#操作XML的完整例子——XmlDocument篇

    这是一个用c#控制台程序下,  用XmlDocument 进行XML操作的的例子,包含了查询.增加.修改.删除.保存的基本操作.较完整的描述了一个XML的整个操作流程.适合刚入门.net XML操作的 ...

  5. Android Loader详解四:回调及完整例子

    onLoadFinished 这个方法是在前面已创建的装载器已经完成其加载过程后被调用.这个方法保证会在应用到装载器上的数据被释放之前被调用.在此方法中,你必须删除所有对旧数据的使用(因为它将很快会被 ...

  6. C#操作XML的完整例子——XmlDocument篇(转载,仅做学习之用)

    原文地址:http://www.cnblogs.com/serenatao/archive/2012/09/05/2672621.html 这是一个用c#控制台程序下,  用XmlDocument 进 ...

  7. 朱晔和你聊Spring系列S1E8:凑活着用的Spring Cloud(含一个实际业务贯穿所有组件的完整例子)

    本文会以一个简单而完整的业务来阐述Spring Cloud Finchley.RELEASE版本常用组件的使用.如下图所示,本文会覆盖的组件有: Spring Cloud Netflix Zuul网关 ...

  8. Ubuntu上运行tensorflow C++的完整例子

    个人博客原文:http://www.bearoom.xyz/2019/08/25/ubuntu-tensorflow-cc-example/ 之前记录的运行Tensorflow的C++接口的例子都是零 ...

  9. jquery mobile上传图片完整例子(包含ios图片横向问题处理和C#后台图片压缩)

    上传图片本身是个基本的小功能,但是到了移动端就不那么简单了,相信找到这篇文章的你一定有深深的同感. 本文实例是:在(移动端)页面中点击图片,然后选择文件,然后保存.使用Asp.net 难点一:后台获取 ...

随机推荐

  1. Shell脚本编程与文件系统修复

    导读 Linux基金会发起了LFCS认证(Linux 基金会认证系统管理员)Linux Foundation Certified Sysadmin,这是一个全新的认证体系,旨在让世界各地的人能够参与到 ...

  2. 【黑金原创教程】【Modelsim】【第四章】激励文本就是仿真环境

      声明:本文为黑金动力社区(http://www.heijin.org)原创教程,如需转载请注明出处,谢谢! 黑金动力社区2013年原创教程连载计划: http://www.cnblogs.com/ ...

  3. ng2-file-upload上传附件同时传参

    由于业务需要,需要的场景是发某条公告的时候能够上传附件,不只是图片,图片的话可以直接用base64传给后台,但上传附件这个就不能这样干了, 与此同时,每条公告都有一个对应的唯一标识id, 附件以文件流 ...

  4. Python全栈day17(文件处理)

    一,文件处理流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 二,文件打开模式 r只读 (默认打开模式是只读) w只写 a追加 三,文件操作实例 1.r读 read读取文 ...

  5. SLAM论文阅读笔记

    [1]陈卫东, 张飞. 移动机器人的同步自定位与地图创建研究进展[J]. 控制理论与应用, 2005, 22(3):455-460. [2]Cadena C, Carlone L, Carrillo ...

  6. 简单安装与配置mysql数据库(绿色版)

    目录 绿色版下载 mysql绿色版(5.7版本的安装与配置) 绿色版下载: mysql官网下载地址:https://www.oracle.com/index.html mysql绿色版(5.7版本的安 ...

  7. HDU_5514_Frogs

    Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  8. smp,numa,mpp,umam,olap,dss,oltp,greenplum,presto

    SMP.NUMA.MPP体系结构介绍 从系统架构来看,目前的商用服务器大体可以分为三类,即对称多处理器结构 (SMP : Symmetric Multi-Processor) ,非一致存储访问结构 ( ...

  9. 解决启动Tomcat时遇到INFO: Destroying ProtocolHandler ["ajp-apr-8009"]

    问题描述: 启动Tomcat时,出现INFO: Destroying ProtocolHandler ["ajp-apr-8009"]等信息 这说明端口号被占用了... 解决方法: ...

  10. VirtualBox中安装Ubuntu12.04/Ubuntu14.04虚拟机(转)

    add by zhj: 如果宿主机是win7,那VirtualBox建议安装4.3.12,再高的版本在Windows7上运行会报错,从4.3.14到5.0.xx版本,一直报错,搞了半天也解决不了.如果 ...