Bolt关键的接口为execute,
Tuple的真正处理逻辑, 通过OutputCollector.emit发出新的tuples, 调用ack或fail处理的tuple

  1. /**
  2. * An IBolt represents a component that takes tuples as input and produces tuples
  3. * as output. An IBolt can do everything from filtering to joining to functions
  4. * to aggregations. It does not have to process a tuple immediately and may
  5. * hold onto tuples to process later.
  6. *
  7. * <p>A bolt's lifecycle is as follows:</p>
  8. *
  9. * <p>IBolt object created on client machine. The IBolt is serialized into the topology
  10. * (using Java serialization) and submitted to the master machine of the cluster (Nimbus).
  11. * Nimbus then launches workers which deserialize the object, call prepare on it, and then
  12. * start processing tuples.</p>
  13. *
  14. * <p>If you want to parameterize an IBolt, you should set the parameter's through its
  15. * constructor and save the parameterization state as instance variables (which will
  16. * then get serialized and shipped to every task executing this bolt across the cluster).</p>
  17. *
  18. * <p>When defining bolts in Java, you should use the IRichBolt interface which adds
  19. * necessary methods for using the Java TopologyBuilder API.</p>
  20. */
  21. public interface IBolt extends Serializable {
  22. /**
  23. * Called when a task for this component is initialized within a worker on the cluster.
  24. * It provides the bolt with the environment in which the bolt executes.
  25. *
  26. * <p>This includes the:</p>
  27. *
  28. * @param stormConf The Storm configuration for this bolt. This is the configuration provided to the topology merged in with cluster configuration on this machine.
  29. * @param context This object can be used to get information about this task's place within the topology, including the task id and component id of this task, input and output information, etc.
  30. * @param collector The collector is used to emit tuples from this bolt. Tuples can be emitted at any time, including the prepare and cleanup methods. The collector is thread-safe and should be saved as an instance variable of this bolt object.
  31. */
  32. void prepare(Map stormConf, TopologyContext context, OutputCollector collector);
  33.  
  34. /**
  35. * Process a single tuple of input. The Tuple object contains metadata on it
  36. * about which component/stream/task it came from. The values of the Tuple can
  37. * be accessed using Tuple#getValue. The IBolt does not have to process the Tuple
  38. * immediately. It is perfectly fine to hang onto a tuple and process it later
  39. * (for instance, to do an aggregation or join).
  40. *
  41. * <p>Tuples should be emitted using the OutputCollector provided through the prepare method.
  42. * It is required that all input tuples are acked or failed at some point using the OutputCollector.
  43. * Otherwise, Storm will be unable to determine when tuples coming off the spouts
  44. * have been completed.</p>
  45. *
  46. * <p>For the common case of acking an input tuple at the end of the execute method,
  47. * see IBasicBolt which automates this.</p>
  48. *
  49. * @param input The input tuple to be processed.
  50. */
  51. void execute(Tuple input);
  52.  
  53. /**
  54. * Called when an IBolt is going to be shutdown. There is no guarentee that cleanup
  55. * will be called, because the supervisor kill -9's worker processes on the cluster.
  56. *
  57. * <p>The one context where cleanup is guaranteed to be called is when a topology
  58. * is killed when running Storm in local mode.</p>
  59. */
  60. void cleanup();
  61. }

 

首先OutputCollector, 主要是emit和emitDirect接口

List<Integer> emit(String streamId, Tuple anchor, List<Object> tuple)

emit, 3个参数, 发送到的streamid, anchors(来源tuples), tuple(values list)

        如果streamid为空, 则发送到默认stream, Utils.DEFAULT_STREAM_ID

        如果anchors为空, 则为unanchored tuple

        1个返回值, 最终发送到的task ids

对比一下SpoutOutputCollector中的emit, 参数变化, 没有message-id, 多了anchors

 

而在在Bolt中, ack和fail接口在IOutputCollector中, 用于在execute中完成对上一级某tuple的处理和emit, 调用ack或fail

而在Spout中, ack和fail接口在ISpout中, 用于spout收到ack或fail tuple时调用

 

  1. /**
  2. * This output collector exposes the API for emitting tuples from an IRichBolt.
  3. * This is the core API for emitting tuples. For a simpler API, and a more restricted
  4. * form of stream processing, see IBasicBolt and BasicOutputCollector.
  5. */
  6. public class OutputCollector implements IOutputCollector {
  7. private IOutputCollector _delegate;
  8.  
  9. public OutputCollector(IOutputCollector delegate) {
  10. _delegate = delegate;
  11. }
  12.  
  13. /**
  14. * Emits a new tuple to a specific stream with a single anchor. The emitted values must be
  15. * immutable.
  16. *
  17. * @param streamId the stream to emit to
  18. * @param anchor the tuple to anchor to
  19. * @param tuple the new output tuple from this bolt
  20. * @return the list of task ids that this new tuple was sent to
  21. */
  22. public List<Integer> emit(String streamId, Tuple anchor, List<Object> tuple) {
  23. return emit(streamId, Arrays.asList(anchor), tuple);
  24. }
  25.  
  26. /**
  27. * Emits a tuple directly to the specified task id on the specified stream.
  28. * If the target bolt does not subscribe to this bolt using a direct grouping,
  29. * the tuple will not be sent. If the specified output stream is not declared
  30. * as direct, or the target bolt subscribes with a non-direct grouping,
  31. * an error will occur at runtime. The emitted values must be
  32. * immutable.
  33. *
  34. * @param taskId the taskId to send the new tuple to
  35. * @param streamId the stream to send the tuple on. It must be declared as a direct stream in the topology definition.
  36. * @param anchor the tuple to anchor to
  37. * @param tuple the new output tuple from this bolt
  38. */
  39. public void emitDirect(int taskId, String streamId, Tuple anchor, List<Object> tuple) {
  40. emitDirect(taskId, streamId, Arrays.asList(anchor), tuple);
  41. }
  42.  
  43. @Override
  44. public List<Integer> emit(String streamId, Collection<Tuple> anchors, List<Object> tuple) {
  45. return _delegate.emit(streamId, anchors, tuple);
  46. }
  47.  
  48. @Override
  49. public void emitDirect(int taskId, String streamId, Collection<Tuple> anchors, List<Object> tuple) {
  50. _delegate.emitDirect(taskId, streamId, anchors, tuple);
  51. }
  52.  
  53. @Override
  54. public void ack(Tuple input) {
  55. _delegate.ack(input);
  56. }
  57.  
  58. @Override
  59. public void fail(Tuple input) {
  60. _delegate.fail(input);
  61. }
  62.  
  63. @Override
  64. public void reportError(Throwable error) {
  65. _delegate.reportError(error);
  66. }
  67. }

Storm-源码分析- bolt (backtype.storm.task)的更多相关文章

  1. storm源码分析之任务分配--task assignment

    在"storm源码分析之topology提交过程"一文最后,submitTopologyWithOpts函数调用了mk-assignments函数.该函数的主要功能就是进行topo ...

  2. Storm源码分析--Nimbus-data

    nimbus-datastorm-core/backtype/storm/nimbus.clj (defn nimbus-data [conf inimbus] (let [forced-schedu ...

  3. JStorm与Storm源码分析(四)--均衡调度器,EvenScheduler

    EvenScheduler同DefaultScheduler一样,同样实现了IScheduler接口, 由下面代码可以看出: (ns backtype.storm.scheduler.EvenSche ...

  4. JStorm与Storm源码分析(三)--Scheduler,调度器

    Scheduler作为Storm的调度器,负责为Topology分配可用资源. Storm提供了IScheduler接口,用户可以通过实现该接口来自定义Scheduler. 其定义如下: public ...

  5. JStorm与Storm源码分析(二)--任务分配,assignment

    mk-assignments主要功能就是产生Executor与节点+端口的对应关系,将Executor分配到某个节点的某个端口上,以及进行相应的调度处理.代码注释如下: ;;参数nimbus为nimb ...

  6. JStorm与Storm源码分析(一)--nimbus-data

    Nimbus里定义了一些共享数据结构,比如nimbus-data. nimbus-data结构里定义了很多公用的数据,请看下面代码: (defn nimbus-data [conf inimbus] ...

  7. spark 源码分析之二十一 -- Task的执行流程

    引言 在上两篇文章 spark 源码分析之十九 -- DAG的生成和Stage的划分 和 spark 源码分析之二十 -- Stage的提交 中剖析了Spark的DAG的生成,Stage的划分以及St ...

  8. storm源码分析之topology提交过程

    storm集群上运行的是一个个topology,一个topology是spouts和bolts组成的图.当我们开发完topology程序后将其打成jar包,然后在shell中执行storm jar x ...

  9. Nimbus<三>Storm源码分析--Nimbus启动过程

    Nimbus server, 首先从启动命令开始, 同样是使用storm命令"storm nimbus”来启动看下源码, 此处和上面client不同, jvmtype="-serv ...

随机推荐

  1. javascript跨域訪问探索之旅

    需求:         近期工作负责一个互联网应用A(我公司应用)与还有一个互联网应用B进行通讯.通讯的方式是这种:还有一个互联网应用某些表单信息须要从我公司的互联网应用获取.首先用户訪问互联网应用B ...

  2. struts2 在拦截器进行注入(依据Action是否实现自己定义接口)

    比如:经常在Action中都须要获取当前登录的User,就须要获取Session.然后从Session获取当前登录的User,由于这些步骤都是反复操作,能够想办法在拦截器中进行实现.能够自己定义一个接 ...

  3. [git]git命令行自动补齐

    1. 下载Git-completion.bash github地址:https://github.com/markgandolfo/git-bash-completion.git 2. copy到用户 ...

  4. Android实现微信自己主动抢红包的程序

    简单实现了微信自己主动抢红包的服务,原理就是依据keyword找到对应的View, 然后自己主动点击.主要是用到AccessibilityService这个辅助服务,基本能够满足自己主动抢红包的功能, ...

  5. ArcGIS教程:将“替换为模型”工具用于多面体

    替换为模型工具出如今 3D 编辑器 工具条上的 3D 编辑器菜单中.而且仅仅适用于多面体要素.使用此命令可将所选的一个或多个要素的几何替换为磁盘中所保存的 3D 模型文件.受支持的 3D 模型类型包含 ...

  6. UVA812-Trade on Verweggistan(暴力)

    题目链接 题意:商人要去买pruls这样的东西.然后它的价值是一个序列,买的时候要严格从头到尾取,比方你要买第5个,那么前4个也要一起买下来,求商人能获得的最大的利润. 思路:最大利润肯定就是每一个序 ...

  7. java中的Closeable接口

    一.概述 该接口位于java.io包下,声明例如以下:public interface Closeable Closeable 是能够关闭的数据源或目标. 调用 close 方法可释放对象保存的资源( ...

  8. docker动态添加磁盘

    docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter #!/bin/bash #This script is dynamic mount ...

  9. vue的单文件组件

    五. 单文件组件 1. .vue文件 .vue文件,称为单文件组件,是Vue.js自定义的一种文件格式,一个.vue文件就是一个单独的组件,在文件内封装了组件相关的代码:html.css.js .vu ...

  10. php中while($row = $results->fetch_row())调用出错

    php中while($row = $results->fetch_row())调用出错 错误处在sql语句上