作者:sfktrd | 来源:互联网 | 2023-01-23 11:56
我想了解在哪种情况下我应该使用FlatMap或Map.我的文档似乎并不清楚.
我仍然不明白在哪种情况下我应该使用FlatMap或Map的转换.
有人能给我一个例子,这样我才能理解他们的不同之处吗?
我理解Spark中FlatMap与Map的区别,但不确定是否有相似之处?
1> Pablo..:
Beam中的这些变换与Spark(Scala也是如此)完全相同.
甲Map
变换,映射从PCollection
N个元素的到另一个PCollection
N个元素的.
FlatMap
变换将PCollections
N个元素中的一个映射到零个或多个元素的N个集合中,然后将其展平为单个元素PCollection
.
举个简单的例子,会发生以下情况:
beam.Create([1, 2, 3]) | beam.Map(lambda x: [x, 'any'])
# The result is a collection of THREE lists: [[1, 'any'], [2, 'any'], [3, 'any']]
鉴于:
beam.Create([1, 2, 3]) | beam.FlatMap(lambda x: [x, 'any'])
# The lists that are output by the lambda, are then flattened into a
# collection of SIX single elements: [1, 'any', 2, 'any', 3, 'any']
巴勃罗 - 明白了.感谢您的详细解释和示例.:)