storm Tuple详解
Storm Tuple 是 Apache Storm 拓扑中数据流动的最小不可分单元,本质为带名称的有序值列表,承载组件间传递的所有实时数据 。
核心定义与结构
- 本质:命名的值列表(Named List of Values),字段无需显式声明类型,动态解析;逻辑上类似数据库一行记录或 Key-Value 映射,但底层按索引顺序存储值列表 。
- 组成:由字段名(Fields)和字段值(Values)构成,支持基本类型(int, long, double, boolean, string 等)、字节数组及实现
Serializable接口的自定义对象 。 - 定位:Stream(流)由无限 Tuple 序列组成,Tuple 是 Spout 产生、Bolt 处理与转发的唯一数据载体 。
数据类型与序列化
- 默认支持:integer, float, double, long, short, string, byte, boolean, byte[] (binary)。
- 自定义类型:需实现 Java
Serializable接口,Storm 通过序列化机制在网络间传输 。 - 取值方式:支持通过索引(
getInteger(0))或字段名(getStringByField("log"))获取值,后者更可读且不易出错 。
生命周期与可靠性机制
- 生成:Spout 的
nextTuple()通过OutputCollector.emit()发射 Tuple,并关联唯一的MessageId。 - 追踪:Storm 内部 Acker 组件构建“元组树”,追踪该 Tuple 及其衍生 Tuple 的处理状态。
- 确认:Bolt 处理完必须调用
collector.ack(input)(IBasicBolt可自动完成);超时或调用fail()则触发 Spout 重发 。 - 终结:成功则
ack,失败或超时则fail,由源头 Spout 决定重试或丢弃 。
关键 API 操作示例
// 1. 定义输出字段 (在 Bolt/Spout 的 declareOutputFields 中) declarer.declare(new Fields("field_name_1", "field_name_2")); // 2. 发射 Tuple (在 Bolt/Spout 中) collector.emit(new Values(val1, val2)); // 3. 读取输入 Tuple (在 Bolt execute 方法中) int id = input.getIntegerByField("id"); // 按名取值 String data = input.getString(0); // 按索引取值 Values output = new Values(id * 2, data); // 构造新值 collector.emit(input, output); // 锚定原 Tuple 以确认可靠性 collector.ack(input); // 确认处理成功 ```:ml-citation{ref="1,8" appearance="aggregated" data="citationList"} ### 注意事项 - ‌**线程安全**‌:`OutputCollector` 非线程安全,emit/ack/fail 必须在同一线程执行 。 - ‌**性能**‌:频繁创建大对象或复杂自定义类型会增加序列化开销;优先使用基本类型。 - ‌**Trident 扩展**‌:Trident 抽象层使用 `TridentTuple`,提供批处理语义,接口与原生 Tuple 类似但处于不同包路径 。:ml-citation{ref="9,7" appearance="aggregated" data="citationList"} :::ml-data{name=citationList} ```json [{"source":{"logo":"https://search-operate.cdn.bcebos.com/175c73f4973c6168ed6acae5b638fa15.png","name":"博客园"},"isVideo":false,"title":"Storm概念学习系列之Tuple元组(数据载体) - 大数据和AI躺过的坑 - 博客园","thumbnail":"http://t7.baidu.com/it/u=4129332916,2995855404&fm=217&app=126&f=JPEG?w=800&h=423&s=45F43C7211C2694B5C61D8CB030010B2","linkInfo":{"data-click-info":"{}","href":"https://www.cnblogs.com/zlslch/p/5989281.html","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"1\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"","name":"看云"},"isVideo":false,"title":"2.Storm 核心概念详解","thumbnail":"http://t7.baidu.com/it/u=3576538536,1727813310&fm=217&app=126&f=JPEG?w=757&h=317&s=8CEEFC12A70C7D431C7C085903005072","linkInfo":{"data-click-info":"{}","href":"https://www.kancloud.cn/imnotdown1019/java_core_full/2157757","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"2\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"https://search-operate.cdn.bcebos.com/175c73f4973c6168ed6acae5b638fa15.png","name":"博客园"},"isVideo":false,"title":"storm并发度理解","thumbnail":"http://t8.baidu.com/it/u=478955224,3561615887&fm=217&app=126&f=JPEG?w=563&h=341&s=1A9EEC021DDF41CA4A692CDA030040B3","linkInfo":{"data-click-info":"{}","href":"https://www.cnblogs.com/sunfie/p/7274145.html","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"3\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"https://search-operate.cdn.bcebos.com/175c73f4973c6168ed6acae5b638fa15.png","name":"博客园"},"isVideo":false,"title":"Storm 核心组件详解及storm编程核心Api","thumbnail":"http://t8.baidu.com/it/u=1871709672,1595958830&fm=217&app=126&f=JPEG?w=799&h=454&s=00727532035240650CD4D4DE0300C0B3","linkInfo":{"data-click-info":"{}","href":"https://www.cnblogs.com/Transkai/p/10891242.html","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"4\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3385518650,4195902768&fm=195&app=88&f=JPEG?w=200&h=200","name":"51CTO博客"},"isVideo":false,"title":"2、Storm中的一些概念理解","thumbnail":"","linkInfo":{"data-click-info":"{}","href":"https://blog.51cto.com/u_15300875/3090664","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"5\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3385518650,4195902768&fm=195&app=88&f=JPEG?w=200&h=200","name":"51CTO博客"},"isVideo":false,"title":"storm tuple 结构 storm的原理","thumbnail":"http://t8.baidu.com/it/u=626157831,954645765&fm=217&app=137&f=JPEG?w=753&h=411&s=08887C321B02404B0E4D2CDA030080B2","linkInfo":{"data-click-info":"{}","href":"https://blog.51cto.com/u_16099190/6724295","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"6\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"","name":"storm.apache.org"},"isVideo":false,"title":"TridentTuple (Storm 2.6.0 API)","thumbnail":"","linkInfo":{"data-click-info":"{}","href":"https://storm.apache.org/releases/2.6.0/javadocs/org/apache/storm/trident/tuple/TridentTuple.html","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"7\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"https://search-operate.cdn.bcebos.com/175c73f4973c6168ed6acae5b638fa15.png","name":"博客园"},"isVideo":false,"title":"Storm—Storm常用API - 花未全开*月未圆 - 博客园","thumbnail":"http://t9.baidu.com/it/u=3831325093,3352837103&fm=217&app=126&f=JPEG?w=799&h=381&s=14A67C321109414958D194DA030060B1","linkInfo":{"data-click-info":"{}","href":"https://www.cnblogs.com/tesla-turing/p/13699003.html","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"8\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3385518650,4195902768&fm=195&app=88&f=JPEG?w=200&h=200","name":"51CTO博客"},"isVideo":false,"title":"Storm常用API storm approaching","thumbnail":"","linkInfo":{"data-click-info":"{}","href":"https://blog.51cto.com/u_16213609/7812923","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"9\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"","name":"storm.apache.org"},"isVideo":false,"title":"TupleValueMapper (Storm 2.6.0 API)","thumbnail":"","linkInfo":{"data-click-info":"{}","href":"https://storm.apache.org/releases/2.6.0/javadocs/org/apache/storm/streams/operations/mappers/TupleValueMapper.html","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"10\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"https://search-operate.cdn.bcebos.com/175c73f4973c6168ed6acae5b638fa15.png","name":"博客园"},"isVideo":false,"title":"Storm storm API(二) - houstao - 博客园","thumbnail":"http://t7.baidu.com/it/u=1723174321,3892021495&fm=217&app=126&f=JPEG?w=800&h=504&s=7AB2C04C9AAF936C06D1E4070300E0C3","linkInfo":{"data-click-info":"{}","href":"https://www.cnblogs.com/houstao/p/11473880.html","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"11\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"","name":"storm.apache.org"},"isVideo":false,"title":"TupleValueMappers (Storm 2.6.2 API)","thumbnail":"","linkInfo":{"data-click-info":"{}","href":"https://storm.apache.org/releases/2.6.2/javadocs/org/apache/storm/streams/operations/mappers/TupleValueMappers.html","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"12\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"https://search-operate.cdn.bcebos.com/175c73f4973c6168ed6acae5b638fa15.png","name":"博客园"},"isVideo":false,"title":"Storm的数据处理编程单元:Bolt 学习整理","thumbnail":"","linkInfo":{"data-click-info":"{}","href":"https://www.cnblogs.com/yangsy0915/p/5136331.html","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"13\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3385518650,4195902768&fm=195&app=88&f=JPEG?w=200&h=200","name":"51CTO博客"},"isVideo":false,"title":"storm ui参数","thumbnail":"http://t7.baidu.com/it/u=816220488,671848165&fm=217&app=137&f=JPEG?w=800&h=398&s=D196EE3A935B41C84E4930D2030090B2","linkInfo":{"data-click-info":"{}","href":"https://blog.51cto.com/u_14731/14224758","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"14\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"","name":"storm.apache.org"},"isVideo":false,"title":"Storm Metrics","thumbnail":"","linkInfo":{"data-click-info":"{}","href":"https://storm.apache.org/releases/2.6.1/Metrics.html","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"15\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3385518650,4195902768&fm=195&app=88&f=JPEG?w=200&h=200","name":"51CTO博客"},"isVideo":false,"title":"storm教程 stormst","thumbnail":"http://t8.baidu.com/it/u=1635551756,1339453275&fm=217&app=126&f=JPEG?w=495&h=351&s=B9A87C332952D9CE0EFC94CE0300A0B3","linkInfo":{"data-click-info":"{}","href":"https://blog.51cto.com/u_16213641/7011334","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"16\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"https://search-operate.cdn.bcebos.com/175c73f4973c6168ed6acae5b638fa15.png","name":"博客园"},"isVideo":false,"title":"Storm概念学习系列之Tuple元组(数据载体) - 大数据和AI躺过的坑 - 博客园","thumbnail":"http://t7.baidu.com/it/u=4129332916,2995855404&fm=217&app=126&f=JPEG?w=800&h=423&s=45F43C7211C2694B5C61D8CB030010B2","linkInfo":{"data-click-info":"{}","href":"https://www.cnblogs.com/zlslch/p/5989281.html","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"17\",\"component_content\":{\"component_name\":\"reference\"}}"}},{"source":{"logo":"https://search-operate.cdn.bcebos.com/175c73f4973c6168ed6acae5b638fa15.png","name":"博客园"},"isVideo":false,"title":"Storm Trident API - 木易森林 - 博客园","thumbnail":"","linkInfo":{"data-click-info":"{}","href":"https://www.cnblogs.com/senlinyang/p/8081447.html","target":"_blank","data-noblank":true,"data-show":"list","data-show-ext":"{\"pos\":\"18\",\"component_content\":{\"component_name\":\"reference\"}}"}}]