2017-08-14 68 views
5

我想了解在哪种情况下我应该使用FlatMap或Map。 The documentation对我来说似乎不太清楚。Apache Beam:FlatMap vs Map?

我仍然不明白在哪种情况下我应该使用FlatMap或Map的转换。

有人能给我一个例子,所以我可以理解他们的区别吗?

我明白FlatMap的VS地图Spark中的差异,但不知道是否有任何相似之处吗?

回答

8

Beam中的这些转换和Spark(斯卡拉)完全相同。

Map变换,从N个元素的PCollection映射成N个元件的另一PCollection

FlatMap变换映射的N个元素到零个或多个元件,其然后压平成单个PCollection N个集合的PCollections

一个简单的例子,发生以下情况:

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'] 
+0

Pablo-明白了。感谢您的详细解释和示例。 :) – EmmaYang

+0

您可以接受的答案,如果它是适当的:) – Pablo

+0

优秀解释+1 – codebrotherone