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. 【BZOJ4818】[Sdoi2017]序列计数 DP+矩阵乘法

    [BZOJ4818][Sdoi2017]序列计数 Description Alice想要得到一个长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数.Alice还希望 ,这n个数 ...

  2. jquery如何把一个html元素替换成另外一个html元素?

    1.replaceWith( )   使用括号内的内容替换所选择的内容.              $("#div").replaceWith("<p id=&qu ...

  3. 160405、quartz持久化所需表结构

    delete from qrtz_fired_triggers;   delete from qrtz_simple_triggers;   delete from qrtz_simprop_trig ...

  4. Exception in thread "main" java.lang.IllegalArgumentException: System memory 202768384 must be at least 4.718592E8. Please use a larger heap size.

    Spark-submit 提交任务时候报错 Exception in thread "main" java.lang.IllegalArgumentException: Syste ...

  5. ubuntu mysql 数据库备份以及恢复[命令行]

    之所以加了个ubuntu,其实也没什么,就是恢复数据库的时候给幽默了一下,所以特地加上.   写在前面:一直很想好好的学linux命令行.shell编程,幻想自己能够通过学习进而成为命令行高手,游刃于 ...

  6. git 学习(3)文件删除恢复

    git学习(3) 撤销编辑 如果我们在编辑版本a的时候,如果在没有add之前,发现需要重新编辑版本a怎么办呢,可以通过git reset --hard comm_id, commit_id是版本a的提 ...

  7. LeetCode之Symmetric Tree

    </pre>Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ce ...

  8. 使用Standford coreNLP进行中文命名实体识别

    因为工作需要,调研了一下Stanford coreNLP的命名实体识别功能. Stanford CoreNLP是一个比较厉害的自然语言处理工具,很多模型都是基于深度学习方法训练得到的. 先附上其官网链 ...

  9. MySQL优化(二):SQL优化

    一.SQL优化 1.优化SQL一般步骤 1.1 查看SQL执行频率 SHOW STATUS LIKE 'Com_%'; Com_select:执行SELECT操作的次数,一次查询累加1.其他类似 以下 ...

  10. Install Haskell on Ubuntu and CentOS

    For Ubuntu: Step one: Install GHC If you don't want to install curl you can skip step 1 and just dir ...