谁能给我解释一下map和flatMap之间的区别,以及它们各自的良好用例是什么?
“flatten the results”是什么意思? 它有什么好处?
谁能给我解释一下map和flatMap之间的区别,以及它们各自的良好用例是什么?
“flatten the results”是什么意思? 它有什么好处?
当前回答
通常我们在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的每个元素,但输出是平坦的。
其他回答
map和flatMap是相似的,从某种意义上说,它们从输入RDD中获取一行并在其上应用一个函数。它们的不同之处在于map中的函数只返回一个元素,而flatMap中的函数可以返回一个元素列表(0或更多)作为迭代器。
同样,flatMap的输出是扁平的。尽管flatMap中的函数返回一个元素列表,但flatMap返回一个RDD,其中以平面方式(而不是列表)包含列表中的所有元素。
抽样。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)
如果您正在询问RDD之间的区别。map和RDD。在Spark中,map将一个大小为N的RDD转换为另一个大小为N的RDD。如。
myRDD.map(x => x*2)
例如,如果myRDD由double组成。
而flatMap可以将RDD转换为另一个不同大小的RDD: 如:
myRDD.flatMap(x =>new Seq(2*x,3*x))
这将返回一个大小为2*N的RDD 或
myRDD.flatMap(x =>if x<10 new Seq(2*x,3*x) else new Seq(x) )
这可以归结为你最初的问题:你所说的扁平化是什么意思?
当您使用flatMap时,“多维”集合就变成了“一维”集合。
val array1d = Array ("1,2,3", "4,5,6", "7,8,9")
//array1d is an array of strings
val array2d = array1d.map(x => x.split(","))
//array2d will be : Array( Array(1,2,3), Array(4,5,6), Array(7,8,9) )
val flatArray = array1d.flatMap(x => x.split(","))
//flatArray will be : Array (1,2,3,4,5,6,7,8,9)
当你想使用flatMap时,
你的地图功能的结果是创建多层结构 但所有你想要的是一个简单的-平面-一维结构,通过删除所有的内部分组
对于所有想要PySpark相关的人:
示例转换:flatMap
>>> a="hello what are you doing"
>>> a.split()
['hello', 'what', 'are', 'you', 'doing']
>>> b=["hello what are you doing","this is rak"]
>>> b.split()
回溯(最近一次调用): 文件“”,第1行,在 AttributeError: 'list'对象没有属性'split'
>>> rline=sc.parallelize(b)
>>> type(rline)
>>> def fwords(x):
... return x.split()
>>> rword=rline.map(fwords)
>>> rword.collect()
[[‘你好’,‘什么’,‘是’,‘你’,‘做’],[‘这个’,‘是’,'爱你']]
>>> rwordflat=rline.flatMap(fwords)
>>> rwordflat.collect()
[‘你好’,‘什么’,‘是’,‘你’,‘做’,‘这’,‘是’,‘爱’)
希望能有所帮助。