spark 的 map 和 flatMap 应该怎样理解?

conf = SparkConf().setAppName("Simple App").setMaster("local")

sc = SparkContext(conf=conf)

file = "./README.md"

“”“

  111 aaa bcd

  22 qqq www

”“”

dataFile = sc.textFile(file)

test = dataFile.map(lambda s : s)

print test.collect()  # [u'111 aaa bcd', u'22 qqq www'] 

test = dataFile.flatMap(lambda s : s)  

print test.collect()  # [u'1', u'1', u'1', u' ', u'a', u'a', u'a', u' ', u'b', u'c', u'd', u'2', u'2', u' ', u'q', u'q', u'q', u' ', u'w', u'w', u'w'

map文档是这样解释的:Return a new distributed dataset formed by passing each element of the source through a function func.
我的理解是对rdd的每一个element进行func,最后返回的数量应该是等于element的数量的。

flatMap是这样解释:Similar to map, but each input item can be mapped to 0 or more output items (so func should return a Seq rather than a single item).

这里不懂为什么flatMap结果是一个个字母?


不负相思意
浏览 878回答 2
2回答

米脂

map操作我记得的有map(一条对一条),mapToPair(map成键值对),flatMap(一条记录变n条(n>=0))。你可以看看官方的wordCount demo,对flatMap的功能解释得很详尽了
打开App,查看更多内容
随时随地看视频慕课网APP