【转】storm 开发系列一 第一个程序
原文: http://blog.csdn.net/csfreebird/article/details/49104777
-------------------------------------------------------------------------------------------------
本文将在本地开发环境创建一个storm程序,力求简单。
首先用mvn创建一个简单的工程hello_storm
- mvn archetype:generate -DgroupId=org.csfreebird -DartifactId=hello_storm -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
编辑pom.xml,添加dependency
- <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/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>org.csfreebird</groupId>
- <artifactId>hello_storm</artifactId>
- <version>0.9.5</version>
- <packaging>jar</packaging>
- <name>hello_storm</name>
- <url>http://maven.apache.org</url>
- <dependencies>
- <dependency>
- <groupId>org.apache.storm</groupId>
- <artifactId>storm-core</artifactId>
- <version>${project.version}</version>
- <!-- keep storm out of the jar-with-dependencies -->
- <scope>provided</scope>
- </dependency>
- </dependencies>
- </project>
provided 表示storm-core的jar包只作为编译和测试时使用,在集群环境下运行时完全依赖集群环境的storm-core的jar包。
然后重命名App.Java为HelloTopology.java文件,开始编码。模仿之前的Example, 这里将所有的spout/bolt类都作为静态类定义,就放在HelloTopology.java文件。
功能如下
编写HelloTopology.java代码,spout代码来自于TestWordSpout,去掉了log的代码,改变了_引导的成员变量命名方法
- package org.csfreebird;
- import backtype.storm.Config;
- import backtype.storm.LocalCluster;
- import backtype.storm.StormSubmitter;
- import backtype.storm.task.OutputCollector;
- import backtype.storm.task.TopologyContext;
- import backtype.storm.testing.TestWordSpout;
- import backtype.storm.topology.OutputFieldsDeclarer;
- import backtype.storm.topology.TopologyBuilder;
- import backtype.storm.topology.base.BaseRichBolt;
- import backtype.storm.topology.base.BaseRichSpout;
- import backtype.storm.tuple.Fields;
- import backtype.storm.tuple.Tuple;
- import backtype.storm.tuple.Values;
- import backtype.storm.utils.Utils;
- import backtype.storm.spout.SpoutOutputCollector;
- import java.util.Map;
- import java.util.TreeMap;
- import java.util.Random;
- public class HelloTopology {
- public static class HelloSpout extends BaseRichSpout {
- boolean isDistributed;
- SpoutOutputCollector collector;
- public HelloSpout() {
- this(true);
- }
- public HelloSpout(boolean isDistributed) {
- this.isDistributed = isDistributed;
- }
- public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
- this.collector = collector;
- }
- public void close() {
- }
- public void nextTuple() {
- Utils.sleep(100);
- final String[] words = new String[] {"china", "usa", "japan", "russia", "england"};
- final Random rand = new Random();
- final String word = words[rand.nextInt(words.length)];
- this.collector.emit(new Values(word));
- }
- public void ack(Object msgId) {
- }
- public void fail(Object msgId) {
- }
- public void declareOutputFields(OutputFieldsDeclarer declarer) {
- declarer.declare(new Fields("word"));
- }
- @Override
- public Map<String, Object> getComponentConfiguration() {
- if(!this.isDistributed) {
- Map<String, Object> ret = new TreeMap<String, Object>();
- ret.put(Config.TOPOLOGY_MAX_TASK_PARALLELISM, 1);
- return ret;
- } else {
- return null;
- }
- }
- }
- public static class HelloBolt extends BaseRichBolt {
- OutputCollector collector;
- @Override
- public void prepare(Map conf, TopologyContext context, OutputCollector collector) {
- this.collector = collector;
- }
- @Override
- public void execute(Tuple tuple) {
- this.collector.emit(tuple, new Values("hello," + tuple.getString(0)));
- this.collector.ack(tuple);
- }
- @Override
- public void declareOutputFields(OutputFieldsDeclarer declarer) {
- declarer.declare(new Fields("word"));
- }
- }
- public static void main(String[] args) throws Exception {
- TopologyBuilder builder = new TopologyBuilder();
- builder.setSpout("a", new HelloSpout(), 10);
- builder.setBolt("b", new HelloBolt(), 5).shuffleGrouping("a");
- Config conf = new Config();
- conf.setDebug(true);
- if (args != null && args.length > 0) {
- conf.setNumWorkers(3);
- StormSubmitter.submitTopologyWithProgressBar(args[0], conf, builder.createTopology());
- } else {
- String test_id = "hello_test";
- LocalCluster cluster = new LocalCluster();
- cluster.submitTopology(test_id, conf, builder.createTopology());
- Utils.sleep(10000);
- cluster.killTopology(test_id);
- cluster.shutdown();
- }
- }
- }
编译成功
- mvn clean compile
为了能够在本地模式运行,需要在pom.xml中添加如下:
- <build>
- <plugins>
- <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>
- </plugins>
- </build>
然后运行命令
- mvn compile exec:java -Dstorm.topology=org.csfreebird.HelloTopology
【转】storm 开发系列一 第一个程序相关推荐
- Java零基础系列001——第一个程序
Java零基础系列001--第一个程序 public class Welcome {public static void main(String[] args) {//args为arguments的缩 ...
- 嵌入式linux/鸿蒙开发板(IMX6ULL)开发(八)IMX6ULL开发板编译第一个程序以及驱动
文章目录 1. IMX6ULL开发板初次操作 1.1 100ask_imx6ull开发板硬件资源简介 1.1.1 100ask imx6ull mini开发板 1.2 100ask_imx6ull开发 ...
- 手机安全卫士开发系列(6)——程序主界面
主界面的布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...
- 微信小程序开发系列七:微信小程序的页面跳转
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...
- 微信小程序开发系列四:微信小程序之控制器的初始化逻辑
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 这个教程的前两篇文章,介绍了如何 ...
- 微信小程序开发系列六:微信框架API的调用
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...
- 微信小程序开发系列五:微信小程序中如何响应用户输入事件
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...
- 微信小程序开发系列教程三:微信小程序的调试方法
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 这个教程的前两篇文章,介绍了如何用下图所示的微信开发者工具自动生成一个Hel ...
- 蓝桥ROS机器人之C++基础开发第一个程序
C++学习资料_zhangrelay的博客-CSDN博客 简介/入门 0.1这些教程的介绍 0.2编程语言简介 0.3C/C++ 简介 0.4C++开发简介 0.5编译器.链接器和库简介 0.6安装集 ...
最新文章
- 开发日记-20190404
- Delphi数据类型及转换(附:源码)
- Android 5.0 Usb调试拦截分析及修改
- track文件 什么是git_git常用命令
- drools规则引擎因为内存泄露导致的内存溢出
- uva11361数位dp
- C/C++定时器制作
- Visio studio 2015企业版,汉语版下载,安装,破解,搞定了
- 【转】64位ORACLE客户端上plsql无法识别ORACLE_HOME解决方案
- amos看拟合度在哪里看_AMOS分析技术:结构方程模型的拟合度评价指标
- 基于java的教师教学评价管理系统
- EKT多链技术丨多链会形成新的中心化吗
- js 格式化prettier配置_代码格式化工具---prettier配置
- 为什么走线选择50欧姆阻抗
- python 添加半透明水印_如何利用python给图片添加半透明水印
- 南京工业大学乐学python答案_乐学Python
- Lamport 逻辑时钟
- 五粮液:绩优蓝筹稳步填权
- Flutter高仿微信-第31篇-单聊-表情
- openstack cinder