谁能给我解释一下map和flatMap之间的区别,以及它们各自的良好用例是什么?

“flatten the results”是什么意思? 它有什么好处?


当前回答

map(func)返回一个新的分布式数据集,该数据集通过func声明的函数传递源的每个元素。map()是单个项

其间

flatMap(func)类似于map,但是每个输入项可以映射到0个或多个输出项,因此func应该返回一个Sequence而不是单个项。

其他回答

抽样。Map返回单个数组中的所有元素

抽样。flatMap返回数组数组中的元素

让我们假设在text.txt文件中有文本

Spark is an expressive framework
This text is to understand map and faltMap functions of Spark RDD

使用地图

val text=sc.textFile("text.txt").map(_.split(" ")).collect

输出:

text: **Array[Array[String]]** = Array(Array(Spark, is, an, expressive, framework), Array(This, text, is, to, understand, map, and, faltMap, functions, of, Spark, RDD))

使用flatMap

val text=sc.textFile("text.txt").flatMap(_.split(" ")).collect

输出:

 text: **Array[String]** = Array(Spark, is, an, expressive, framework, This, text, is, to, understand, map, and, faltMap, functions, of, Spark, RDD)

map返回相同数量元素的RDD,而flatMap可能不会。

flatMap过滤丢失或不正确数据的示例用例。

map在各种各样的情况下使用,其中输入和输出的元素数量是相同的。

number.csv

1
2
3
-
4
-
5

Map.py添加add.csv中的所有数字。

from operator import *

def f(row):
  try:
    return float(row)
  except Exception:
    return 0

rdd = sc.textFile('a.csv').map(f)

print(rdd.count())      # 7
print(rdd.reduce(add))  # 15.0

py使用flatMap在添加之前过滤掉缺失的数据。与以前的版本相比,增加的数字更少。

from operator import *

def f(row):
  try:
    return [float(row)]
  except Exception:
    return []

rdd = sc.textFile('a.csv').flatMap(f)

print(rdd.count())      # 5
print(rdd.reduce(add))  # 15.0

地图:

是一种高阶方法,它接受一个函数作为输入,并将其应用于源RDD中的每个元素。

http://commandstech.com/difference-between-map-and-flatmap-in-spark-what-is-map-and-flatmap-with-examples/

flatMap:

接受输入函数的高阶方法和转换操作。

map(func)返回一个新的分布式数据集,该数据集通过func声明的函数传递源的每个元素。map()是单个项

其间

flatMap(func)类似于map,但是每个输入项可以映射到0个或多个输出项,因此func应该返回一个Sequence而不是单个项。

通常我们在hadoop中使用字数计算示例。我将使用相同的用例,将使用map和flatMap,我们将看到它如何处理数据的区别。

下面是示例数据文件。

hadoop is fast
hive is sql on hdfs
spark is superfast
spark is awesome

上面的文件将使用map和flatMap进行解析。

使用地图

>>> wc = data.map(lambda line:line.split(" "));
>>> wc.collect()
[u'hadoop is fast', u'hive is sql on hdfs', u'spark is superfast', u'spark is awesome']

输入有4行,输出大小也是4,即N个元素==> N个元素。

使用flatMap

>>> fm = data.flatMap(lambda line:line.split(" "));
>>> fm.collect()
[u'hadoop', u'is', u'fast', u'hive', u'is', u'sql', u'on', u'hdfs', u'spark', u'is', u'superfast', u'spark', u'is', u'awesome']

输出与map不同。


让我们为每个键赋值1以获得单词计数。

fm:使用flatMap创建的RDD wc:使用map创建RDD

>>> fm.map(lambda word : (word,1)).collect()
[(u'hadoop', 1), (u'is', 1), (u'fast', 1), (u'hive', 1), (u'is', 1), (u'sql', 1), (u'on', 1), (u'hdfs', 1), (u'spark', 1), (u'is', 1), (u'superfast', 1), (u'spark', 1), (u'is', 1), (u'awesome', 1)]

然而,RDD wc上的flatMap将给出以下不希望看到的输出:

>>> wc.flatMap(lambda word : (word,1)).collect()
[[u'hadoop', u'is', u'fast'], 1, [u'hive', u'is', u'sql', u'on', u'hdfs'], 1, [u'spark', u'is', u'superfast'], 1, [u'spark', u'is', u'awesome'], 1]

如果使用map而不是flatMap,则无法获得单词计数。

根据定义,map和flatMap的区别是:

map:它通过对每个元素应用给定的函数来返回一个新的RDD RDD。函数在map中只返回一个项。 flatMap:与map类似,它通过应用函数返回一个新的RDD 到RDD的每个元素,但输出是平坦的。