2019独角兽企业重金招聘Python工程师标准>>>
Storm中,Spout和Bolt都是Component。Storm定义了一个名叫IComponent的总接口
ØSpout的最顶层抽象是ISpout接口。
通常情况下(Shell和事务型的除外),实现一个Spout,可以直接实现接口IRichSpout,
如果不想写多余的代码,可以直接继承BaseRichSpout。
ØBolt的最顶层抽象是IBolt接口
Storm提供了IBasicBolt接口,其目的就是实现该接口的Bolt不用在代码中提供反馈结果了,Storm内部会自动反馈成功
主要开发流程:
1、实现Spout接口IRichSpout或者继承类BaseRichSpout
2、实现Bolt接口IRichBolt或者IBasicBolt或者继承类BaseBasicBout
3、使用TopologyBuilder定义Topology(setSpout/ setBolt/ Grouping,
每一个Spout和Bolt的并发执行单元数和并发任务数在此设置);
4、配置TOPOLOGY_WORKERS等参数;
5、利用TopologyBuilder方法createTopology创建拓扑;
6、提交拓扑 StormSubmitter.submitTopology。
Topology
/*** 定义了一个简单的topology,包括一个数据喷发节点spout和一个数据处理节点bolt。*/
public class SimpleTopology
{public static void main(String[] args){try{// 实例化TopologyBuilder类。TopologyBuilder topologyBuilder = new TopologyBuilder();// 设置喷发节点并分配并发数,该并发数将会控制该对象在集群中的线程数。topologyBuilder.setSpout("SimpleSpout", new SimpleSpout(), 1);// 设置数据处理节点并分配并发数。指定该节点接收喷发节点的策略为随机方式。topologyBuilder.setBolt("SimpleBolt", new SimpleBolt(), 3).shuffleGrouping("SimpleSpout");Config config = new Config();config.setDebug(true);if (args != null && args.length > 0){config.setNumWorkers(1);StormSubmitter.submitTopology(args[0], config,topologyBuilder.createTopology());}else{// 这里是本地模式下运行的启动代码。config.setMaxTaskParallelism(1);LocalCluster cluster = new LocalCluster();cluster.submitTopology}}}
}
spout
/*** Spout起到和外界沟通的作用,他可以从一个数据库中按照某种规则取数据,也可以从分布式队列中取任务*/
@SuppressWarnings("serial")
public class SimpleSpout extends BaseRichSpout
{// 用来发射数据的工具类private SpoutOutputCollector collector;private static String[] info = new String[] {"comaple\t,12424,44w46,654,12424,44w46,654,","lisi\t,435435,6537,12424,44w46,654,","lipeng\t,45735,6757,12424,44w46,654,","hujintao\t,45735,6757,12424,44w46,654,","jiangmin\t,23545,6457,2455,7576,qr44453","beijing\t,435435,6537,12424,44w46,654,","xiaoming\t,46654,8579,w3675,85877,077998,","xiaozhang\t,9789,788,97978,656,345235,09889,","ceo\t,46654,8579,w3675,85877,077998,","cto\t,46654,8579,w3675,85877,077998,","zhansan\t,46654,8579,w3675,85877,077998," };Random random = new Random();/*** 初始化collector*/public void open(Map conf, TopologyContext context, SpoutOutputCollector collector){this.collector = collector;}/*** 在SpoutTracker类中被调用,每调用一次就可以向storm集群中发射一条数据(一个tuple元组),该方法会被不停的调用*/@Overridepublic void nextTuple(){try{String msg = info[random.nextInt(11)];// 调用发射方法collector.emit(new Values(msg));// 模拟等待100msThread.sleep(100);}catch (InterruptedException e){e.printStackTrace();}}/*** 定义字段id,该id在简单模式下没有用处,但在按照字段分组的模式下有很大的用处。* 该declarer变量有很大作用,我们还可以调用declarer.declareStream();来定义stramId,该id可以用来定义更加复杂的流拓扑结构*/@Overridepublic void declareOutputFields(OutputFieldsD
bolt
package com.zmq.helloword;/*** @author zhangmq(工号:68598) Tel:☎* @version 1.0* @since 2014-8-26 下午2:46:53* @category com.ljq.helloword* @copyright 南京联创科技 网管科技部*/
import backtype.storm.topology.BasicOutputCollector;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseBasicBolt;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
import backtype.storm.tuple.Values;/*** 接收喷发节点(Spout)发送的数据进行简单的处理后,发射出去。*/
@SuppressWarnings("serial")
public class SimpleBolt extends BaseBasicBolt
{public void execute(Tuple input, BasicOutputCollector collector){try{String msg = input.getString(0);if (msg != null){// System.out.println("msg="+msg);collector.emit(new Values(msg + "msg is processed!"));}}catch (Exception e){e.printStackTrace();}}