谁能给我解释一下map和flatMap之间的区别,以及它们各自的良好用例是什么?
“flatten the results”是什么意思? 它有什么好处?
谁能给我解释一下map和flatMap之间的区别,以及它们各自的良好用例是什么?
“flatten the results”是什么意思? 它有什么好处?
当前回答
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
其他回答
map:它通过对RDD的每个元素应用函数来返回一个新的RDD。.map中的函数只能返回一个项。
flatMap:与map类似,它通过对RDD的每个元素应用函数来返回一个新的RDD,但输出是扁平的。
同样,flatMap中的函数可以返回一个元素列表(0或更多)
例如:
sc.parallelize([3,4,5]).map(lambda x: range(1,x)).collect()
输出:[[1,2],[1,2,3],[1,2,3,4]]
sc.parallelize([3,4,5]).flatMap(lambda x: range(1,x)).collect()
输出:注意o/p在单个列表[1,2,1,2,3, 1,2,3,4]
来源:https://www.linkedin.com/pulse/difference-between-map-flatmap-transformations-spark-pyspark-pandey/
使用测试。以Md为例:
➜ spark-1.6.1 cat test.md
This is the first line;
This is the second line;
This is the last line.
scala> val textFile = sc.textFile("test.md")
scala> textFile.map(line => line.split(" ")).count()
res2: Long = 3
scala> textFile.flatMap(line => line.split(" ")).count()
res3: Long = 15
scala> textFile.map(line => line.split(" ")).collect()
res0: Array[Array[String]] = Array(Array(This, is, the, first, line;), Array(This, is, the, second, line;), Array(This, is, the, last, line.))
scala> textFile.flatMap(line => line.split(" ")).collect()
res1: Array[String] = Array(This, is, the, first, line;, This, is, the, second, line;, This, is, the, last, line.)
如果您使用映射方法,您将得到测试线。md,对于flatMap方法,您将得到字数。
map方法类似于flatMap,它们都返回一个新的RDD。map方法经常使用返回一个新的RDD, flatMap方法经常使用分割词。
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
map
通过将函数应用到该RDD的每个元素,返回一个新的RDD。
>>> rdd = sc.parallelize([2, 3, 4])
>>> sorted(rdd.map(lambda x: [(x, x), (x, x)]).collect())
[[(2, 2), (2, 2)], [(3, 3), (3, 3)], [(4, 4), (4, 4)]]
flatMap
返回一个新的RDD,首先对该RDD的所有元素应用一个函数,然后将结果平摊。 在这里,一个元素转化为多个元素是可能的
>>> rdd = sc.parallelize([2, 3, 4])
>>> sorted(rdd.flatMap(lambda x: [(x, x), (x, x)]).collect())
[(2, 2), (2, 2), (3, 3), (3, 3), (4, 4), (4, 4)]
Flatmap和Map都转换集合。
的区别:
地图(函数) 返回一个新的分布式数据集,该数据集通过函数func传递源的每个元素。
flatMap(函数) 类似于map,但是每个输入项可以映射到0个或多个输出项(因此func应该返回Seq而不是单个项)。
变换函数为: map:输入一个元素->输出一个元素。 flatMap:输入一个元素->输出0个或更多元素(一个集合)。