Storm完整例子
- import backtype.storm.spout.SpoutOutputCollector;
- import backtype.storm.task.TopologyContext;
- import backtype.storm.topology.base.BaseRichSpout;
- import backtype.storm.utils.Utils;
- import backtype.storm.Config;
- import backtype.storm.LocalCluster;
- import backtype.storm.StormSubmitter;
- import backtype.storm.task.ShellBolt;
- import backtype.storm.topology.BasicOutputCollector;
- import backtype.storm.topology.IRichBolt;
- import backtype.storm.topology.OutputFieldsDeclarer;
- import backtype.storm.topology.TopologyBuilder;
- import backtype.storm.topology.base.BaseBasicBolt;
- import backtype.storm.tuple.Fields;
- import backtype.storm.tuple.Tuple;
- import backtype.storm.tuple.Values;
- import java.util.*;
- //import java.util.HashMap;
- //import java.util.Map;
- //import java.util.Random;
- //import java.util.StringTokenizer;
- /*
- ** WordCountTopolopgyAllInJava类(单词计数)
- */
- public class WordCountTopolopgyAllInJava{
- // 定义一个喷头,用于产生数据。该类继承自BaseRichSpout
- public static class RandomSentenceSpout extends BaseRichSpout {
- SpoutOutputCollector _collector;
- Random _rand;
- @Override
- public void open(Map conf, TopologyContext context, SpoutOutputCollector collector){
- _collector = collector;
- _rand = new Random();
- }
- @Override
- public void nextTuple(){
- // 睡眠一段时间后再产生一个数据
- Utils.sleep(100);
- // 句子数组
- String[] sentences = new String[]{ "the cow jumped over the moon", "an apple a day keeps the doctor away",
- "four score and seven years ago", "snow white and the seven dwarfs", "i am at two with nature" };
- // 随机选择一个句子
- String sentence = sentences[_rand.nextInt(sentences.length)];
- // 发射该句子给Bolt
- _collector.emit(new Values(sentence));
- }
- // 确认函数
- @Override
- public void ack(Object id){
- }
- // 处理失败的时候调用
- @Override
- public void fail(Object id){
- }
- @Override
- public void declareOutputFields(OutputFieldsDeclarer declarer){
- // 定义一个字段word
- declarer.declare(new Fields("word"));
- }
- }
- // 定义个Bolt,用于将句子切分为单词
- public static class SplitSentence extends BaseBasicBolt{
- @Override
- public void execute(Tuple tuple, BasicOutputCollector collector){
- // 接收到一个句子
- String sentence = tuple.getString(0);
- // 把句子切割为单词
- StringTokenizer iter = new StringTokenizer(sentence);
- // 发送每一个单词
- while(iter.hasMoreElements()){
- collector.emit(new Values(iter.nextToken()));
- }
- }
- @Override
- public void declareOutputFields(OutputFieldsDeclarer declarer){
- // 定义一个字段
- declarer.declare(new Fields("word"));
- }
- }
- // 定义一个Bolt,用于单词计数
- public static class WordCount extends BaseBasicBolt {
- Map<String, Integer> counts = new HashMap<String, Integer>();
- @Override
- public void execute(Tuple tuple, BasicOutputCollector collector){
- // 接收一个单词
- String word = tuple.getString(0);
- // 获取该单词对应的计数
- Integer count = counts.get(word);
- if(count == null)
- count = 0;
- // 计数增加
- count++;
- // 将单词和对应的计数加入map中
- counts.put(word,count);
- System.out.println("hello word!");
- System.out.println(word +" "+count);
- // 发送单词和计数(分别对应字段word和count)
- collector.emit(new Values(word, count));
- }
- @Override
- public void declareOutputFields(OutputFieldsDeclarer declarer){
- // 定义两个字段word和count
- declarer.declare(new Fields("word","count"));
- }
- }
- public static void main(String[] args) throws Exception
- {
- // 创建一个拓扑
- TopologyBuilder builder = new TopologyBuilder();
- // 设置Spout,这个Spout的名字叫做"Spout",设置并行度为5
- builder.setSpout("Spout", new RandomSentenceSpout(), 5);
- // 设置slot——“split”,并行度为8,它的数据来源是spout的
- builder.setBolt("split", new SplitSentence(), 8).shuffleGrouping("spout");
- // 设置slot——“count”,你并行度为12,它的数据来源是split的word字段
- builder.setBolt("count", new WordCount(), 12).fieldsGrouping("split", new Fields("word"));
- Config conf = new Config();
- conf.setDebug(false);
- //if(args != null && args.length > 0){
- //if(false){
- // conf.setNumWorkers(3);
- // StormSubmitter.submitTopology(args[0], conf, builder.createTopology());
- //}else{
- conf.setMaxTaskParallelism(3);
- // 本地集群
- LocalCluster cluster = new LocalCluster();
- // 提交拓扑(该拓扑的名字叫word-count)
- cluster.submitTopology("word-count", conf, builder.createTopology() );
- Thread.sleep(10000);
- // cluster.shutdown();
- //}
- }
- }
使用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文件定义如下
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>storm-yqj</groupId>
- <artifactId>word-count</artifactId>
- <version>1.0</version>
- <packaging>jar</packaging>
- <name>word-count</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.testng</groupId>
- <artifactId>testng</artifactId>
- <version>6.8.5</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.mockito</groupId>
- <artifactId>mockito-all</artifactId>
- <version>1.9.0</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.easytesting</groupId>
- <artifactId>fest-assert-core</artifactId>
- <version>2.0M8</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.jmock</groupId>
- <artifactId>jmock</artifactId>
- <version>2.6.0</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.storm</groupId>
- <artifactId>storm-core</artifactId>
- <version>0.9.1-incubating</version>
- </dependency>
- <dependency>
- <groupId>commons-collections</groupId>
- <artifactId>commons-collections</artifactId>
- <version>3.2.1</version>
- </dependency>
- <dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- <version>15.0</version>
- </dependency>
- </dependencies>
- <build>
- <resources>
- <resource>
- <directory>${basedir}/multilang</directory>
- </resource>
- </resources>
- <plugins>
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <configuration>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- <archive>
- <manifest>
- <mainClass></mainClass>
- </manifest>
- </archive>
- </configuration>
- <executions>
- <execution>
- <id>make-assembly</id>
- <phase>package</phase>
- <goals>
- <goal>single</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- <plugin>
- <groupId>com.theoryinpractise</groupId>
- <artifactId>clojure-maven-plugin</artifactId>
- <version>1.3.12</version>
- <extensions>true</extensions>
- <configuration>
- <sourceDirectories>
- <sourceDirectory>src/clj</sourceDirectory>
- </sourceDirectories>
- </configuration>
- <executions>
- <execution>
- <id>compile</id>
- <phase>compile</phase>
- <goals>
- <goal>compile</goal>
- </goals>
- </execution>
- <execution>
- <id>test</id>
- <phase>test</phase>
- <goals>
- <goal>test</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>exec-maven-plugin</artifactId>
- <version>1.2.1</version>
- <executions>
- <execution>
- <goals>
- <goal>exec</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <executable>java</executable>
- <includeProjectDependencies>true</includeProjectDependencies>
- <includePluginDependencies>false</includePluginDependencies>
- <classpathScope>compile</classpathScope>
- <mainClass>${storm.topology}</mainClass>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
Storm完整例子的更多相关文章
- C#调用存储过程简单完整例子
CREATE PROC P_TEST@Name VARCHAR(20),@Rowcount INT OUTPUTASBEGIN SELECT * FROM T_Customer WHERE NAME= ...
- 使用Connector/C++(VS2015)连接MySQL的完整例子
完整示例代码1 /* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved. This program is ...
- 基于Shiro,JWT实现微信小程序登录完整例子
小程序官方流程图如下,官方地址 : https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html ...
- C#操作XML的完整例子——XmlDocument篇
这是一个用c#控制台程序下, 用XmlDocument 进行XML操作的的例子,包含了查询.增加.修改.删除.保存的基本操作.较完整的描述了一个XML的整个操作流程.适合刚入门.net XML操作的 ...
- Android Loader详解四:回调及完整例子
onLoadFinished 这个方法是在前面已创建的装载器已经完成其加载过程后被调用.这个方法保证会在应用到装载器上的数据被释放之前被调用.在此方法中,你必须删除所有对旧数据的使用(因为它将很快会被 ...
- C#操作XML的完整例子——XmlDocument篇(转载,仅做学习之用)
原文地址:http://www.cnblogs.com/serenatao/archive/2012/09/05/2672621.html 这是一个用c#控制台程序下, 用XmlDocument 进 ...
- 朱晔和你聊Spring系列S1E8:凑活着用的Spring Cloud(含一个实际业务贯穿所有组件的完整例子)
本文会以一个简单而完整的业务来阐述Spring Cloud Finchley.RELEASE版本常用组件的使用.如下图所示,本文会覆盖的组件有: Spring Cloud Netflix Zuul网关 ...
- Ubuntu上运行tensorflow C++的完整例子
个人博客原文:http://www.bearoom.xyz/2019/08/25/ubuntu-tensorflow-cc-example/ 之前记录的运行Tensorflow的C++接口的例子都是零 ...
- jquery mobile上传图片完整例子(包含ios图片横向问题处理和C#后台图片压缩)
上传图片本身是个基本的小功能,但是到了移动端就不那么简单了,相信找到这篇文章的你一定有深深的同感. 本文实例是:在(移动端)页面中点击图片,然后选择文件,然后保存.使用Asp.net 难点一:后台获取 ...
随机推荐
- keepalive和脑裂问题
keepalive keepalive起初专门为lvs负载均衡软件设计的,用来管理监控lvs集群系统中各个服务节点的状态,后来又加入了可以实现高可用的vrrp功能. keepalive软件通过vrrp ...
- [NOI2008] 志愿者招募[流量平衡]
288. [NOI2008] 志愿者招募 ★★★★ 输入文件:employee.in 输出文件:employee.out 简单对比时间限制:2 s 内存限制:512 MB [问题描述] ...
- ios 对日期的处理(包括计算昨天时间、明天时间)
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. 如 ...
- Windows7 x64系统下安装Nodejs并在WebStorm下搭建编译less环境
1. 打开Nodejs官网http://www.nodejs.org/,点“DOWNLOADS”,点64-bit下载“node-v0.10.33-x64.msi”. 2. 下载好后,双击“node-v ...
- SqueezeNet
虽然网络性能得到了提高,但随之而来的就是效率问题(AlexNet VGG GoogLeNet Resnet DenseNet) 效率问题主要是模型的存储问题和模型进行预测的速度问题. Model Co ...
- 如何让thrift0.9.2 在macos上面编译通过?
为将来跨语言通信预研,选择了thrift来试试.结果在mac os上面安装遇到种种困难,不知道是我选择方法错误还是咋的,不管怎样,总算是编译过去了. 首先,我们来参考官网的安装步骤:https://t ...
- Django自定义模板函数
Django自定义模板函数 https://www.cnblogs.com/SunsetSunrise/p/7680491.html 在django中新建一个应用:listpage在listpage中 ...
- 机器学习算法(优化)之二:期望最大化(EM)算法
EM算法概述 (1)数学之美的作者吴军将EM算法称之为上帝的算法,EM算法也是大家公认的机器学习十大经典算法之一.EM是一种专门用于求解参数极大似然估计的迭代算法,具有良好的收敛性和每次迭代都能使似然 ...
- 脚本其实很简单-windows配置核查程序(1)
先上成品图 需求描述 我们电脑上都安装各种过监控软件,比如360.鲁大师等等...其中有一个功能就是性能监控,在安全行业里面通常叫做"配置核查",目的就是将主机的各种性能指标展示, ...
- ibatis打印sql
###显示SQL语句部分log4j.logger.com.ibatis=DEBUGlog4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUGl ...