资讯详情

资讯详情

Apache Beam Python ToString 转换详解:Element / Kvs / Iterables 三种字符串化方式与 delimiter 自定义

【免费下载链接】beamApache Beam is a unified programming model for Batch and Streaming data processing.项目地址https://gitcode.com/gh_mirrors/beam18/beam点击查看免费下载Apache Beam Python SDK 提供的ToString是一组专用于将PCollection中的任意元素转换为字符串的复合转换。它在需要把非字符串类型的数据键值对、字典、列表等送入只接受字符串的 I/O 写入器如textio.WriteToText时非常实用。读完本文你将掌握ToString.Element、ToString.Kvs、ToString.Iterables三种子转换的适用场景、delimiter参数的用法以及它们背后的源码实现原理。一、ToString 是什么ToString是 Apache Beam Python SDK 中的一个 PTransform 家族它的作用是把输入集合中的每一个元素转换为字符串。官方文档对其的定位是Transforms every element in an input collection to a string.它位于apache_beam.transforms.util模块中。从源码看ToString是一个普通类而非PTransform子类类体内通过三个静态方法分别返回不同的PTransform实例见 util.pyclass ToString(object): PTransform for converting a PCollection element, KV or PCollection Iterable to string. staticmethod def Element(): return ElementToString Map(str) staticmethod def Iterables(delimiterNone): if delimiter is None: delimiter , return ( IterablesToString Map(lambda xs: delimiter.join(str(x) for x in xs)).with_input_types( Iterable[Any]).with_output_types(str)) # An alias for Iterables. Kvs Iterables从实现可以归纳出三种形态的用途子转换输入元素形态输出核心等价操作ToString.Element()任意单个元素数字、字典、列表等str(element)Map(str)ToString.Kvs()键值对(key, value)key,value可自定义分隔符Iterables的别名ToString.Iterables()任意可迭代对象列表、元组、字典的items()等delimiter.join(str(x) for x in xs)内部实现核心其中Kvs在源码中是Iterables的直接别名二者共享同一实现——因为键值对在 Python 中就是一个二元组天然可迭代。二、为什么需要 ToStringI/O 转换的前置准备文档明确指出任何非字符串元素都可以借助 Python 标准函数转换为字符串而很多 I/O 转换例如textio.WriteToText期望输入元素必须是字符串。WriteToText的构造参数见 textio.py包括file_path_prefix、file_name_suffix、append_trailing_newlines、num_shards、shard_name_template、coder、compression_type、header、footer等它把每个元素按行写入文本文件。如果上游元素是数字、元组或字典直接写入会失败或产生非预期内容。因此典型用法是pipeline | Create records beam.Create([(apple, 3), (banana, 5)]) | To string beam.ToString.Kvs() | Write to file beam.io.WriteToText(fruit_count.csv)先做字符串化再交给文本写入器这是数据落盘前最常用的一条流水线。三、三种用法的完整示例与输出以下三个示例均来自仓库中的官方代码片段位于 elementwise/tostring_kvs.py、tostring_element.py、tostring_iterables.py可直接在本地 Python 环境执行验证也可在 Beam Playground 中在线运行。示例 1键值对转字符串ToString.Kvs把(key, value)键值对转换为字符串默认以英文逗号,分隔可以用delimiter参数指定其他分隔符import apache_beam as beam with beam.Pipeline() as pipeline: plants ( pipeline | Garden plants beam.Create([ (, Strawberry), (, Carrot), (, Eggplant), (, Tomato), (, Potato), ]) | To string beam.ToString.Kvs() | beam.Map(print))输出每行一个元素,Strawberry ,Carrot ,Eggplant ,Tomato ,Potato在 tostring_test.py 中check_plants断言了完全相同的输出可作为结果验证依据。示例 2任意元素转字符串ToString.Element把元素本身转换成字符串输出等价于str(element)。例如把元素为列表的PCollection字符串化import apache_beam as beam with beam.Pipeline() as pipeline: plant_lists ( pipeline | Garden plants beam.Create([ [, Strawberry, perennial], [, Carrot, biennial], [, Eggplant, perennial], [, Tomato, annual], [, Potato, perennial], ]) | To string beam.ToString.Element() | beam.Map(print))输出为 Python 列表的字符串表示形式[, Strawberry, perennial] [, Carrot, biennial] [, Eggplant, perennial] [, Tomato, annual] [, Potato, perennial]对应的测试断言check_plant_lists位于 tostring_test.py。示例 3可迭代对象转字符串ToString.Iterables把可迭代对象文档示例中为字符串列表转换为以,分隔的字符串同样可用delimiter指定分隔符。输出等价于iterable.join(delimiter)import apache_beam as beam with beam.Pipeline() as pipeline: plants_csv ( pipeline | Garden plants beam.Create([ [, Strawberry, perennial], [, Carrot, biennial], [, Eggplant, perennial], [, Tomato, annual], [, Potato, perennial], ]) | To string beam.ToString.Iterables() | beam.Map(print))输出,Strawberry,perennial ,Carrot,biennial ,Eggplant,perennial ,Tomato,annual ,Potato,perennial测试check_plants_csv见 tostring_test.py。四、delimiter 参数默认值与自定义行为ToString.Iterables以及它的别名ToString.Kvs接受关键字参数delimiter默认值为,staticmethod def Iterables(delimiterNone): if delimiter is None: delimiter ,也就是说即使不传任何参数分隔符也是逗号一旦传入其他值如制表符、空字符串则以传入值为准。仓库的单元测试 util_test.py 用五种场景验证了该行为测试用例输入调用期望输出test_tostring_elements[1, 1, 2, 3]ToString.Element()[1, 1, 2, 3]test_tostring_iterables(one, two, three)ToString.Iterables()one,two,threetest_tostring_iterables_with_delimeter同上ToString.Iterables(\t)one\ttwo\tthreetest_tostring_kvs(one, 1)ToString.Kvs()one,1test_tostring_kvs_delimeter同上ToString.Kvs(\t)one\t1test_tostring_kvs_empty_delimeter同上ToString.Kvs()one1值得注意的细节空字符串分隔符Kvs()会把(one, 1)拼成one1说明delimiter可以是空串用来做无缝拼接。元素逐个str()化Iterables内部对每个子元素执行str(x)因此键值对里的整数1也被正确转换为1不存在类型不匹配问题。无尾随分隔符源码注释明确指出There is no trailing delimiter.即拼接结果末尾不会多出分隔符适合直接写入 CSV 等格式。五、源码实现原理从调用链看 ToStringToString的实现非常轻量本质上是若干Map的语法糖包装ToString.Element()等价于Map(str)。而Map本身见 core.py是对FlatMap的封装它要求fn是可调用对象否则抛出TypeError随后把fn包装为“对每个输入返回单元素列表”的 lambda最终交给FlatMap执行。这就是“1 对 1 映射”语义的来源。ToString.Iterables(delimiter)使用带 lambda 的Maplambda xs: delimiter.join(str(x) for x in xs)并通过with_input_types(Iterable[Any])与with_output_types(str)声明类型提示——输入必须是可迭代对象输出必然是str。这种类型标注在 Beam 执行前可以做类型校验也有利于运行时的类型推断优化。ToString.Kvs直接是Iterables的别名。因为(key, value)键值对本身就是可迭代的二元组delimiter.join天然能把它拼成key,value。它专门面向键值对场景命名语义更清晰实际行为与Iterables完全一致。此外ToString使用的管道符号ElementToString Map(str)是 Beam 的标准 PTransform 组合写法为转换赋予可读标签label便于在作业图中定位与调试。六、与其他转换的关系官方文档将ToString归入元素级Elementwise转换系列并指出它和 Map 的关系Map applies a simple 1-to-1 mapping function over each element in the collectionMap对集合中的每个元素应用一个简单的 1 对 1 映射函数是整个 elementwise 家族的基础能力ToString的三个子转换本质上都是对Map的定制包装Map(str)或带 lambda 的Map。当你需要“只做字符串化”这一件事时优先选择ToString而非手写Map(lambda x: ...)代码意图更明确、更不易出错。若需要更自由的映射逻辑多参数、side input 等则应直接使用Map。七、小结ToString是 Apache Beam Python SDK 中元素 → 字符串的标准解决方案ToString.Element()任意元素整体字符串化等价于str(element)ToString.Kvs()键值对拼接默认逗号分隔适合生成 CSV 行ToString.Iterables()任意可迭代对象拼接同样支持delimiter且无尾随分隔符三个子转换底层均基于Map实现核心代码位于 util.py行为由 util_test.py 与示例片段测试双重验证。在将数据写入WriteToText等只接受字符串的 I/O 之前先经过ToString做一次转换是保证流水线正确、输出格式可控的推荐做法。赞分享【免费下载链接】beamApache Beam is a unified programming model for Batch and Streaming data processing.项目地址https://gitcode.com/gh_mirrors/beam18/beam点击查看免费下载相关推荐Apache Beam Java 的 ToString 转换将 PCollection 元素统一转为字符串的三种实用模式Apache Beam Java 的 ToString 转换将 PCollection 元素统一转为字符串的三种实用模式 导读 在 Apache Beam 的Apache Beam Java SDK ToString 转换实战将任意 PCollection 元素、KV 与 Iterable 序列化为字符串Apache Beam Java SDK ToString 转换实战将任意 PCollection 元素、KV 与 Iterable 序列化为字符串 ToSt大数据批处理流处理数据工程Apache Beam Python Count 聚合变换详解Globally / PerKey / PerElement 三种计数方式Apache Beam Python Count 聚合变换详解Globally / PerKey / PerElement 三种计数方式 Count 是 Ap上一篇Loop终极免费开源Mac窗口管理器5分钟告别桌面杂乱下一篇HowToCook 雪花酥零失败实操指南棉花糖与坚果的黄金配比与火候控制创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
觉得有用,分享给同行:

为您的企业打造数字门面

稳重轻奢商务风格,端正雅致视觉,长效耐看不易过时。

立即咨询 →