1. 程式人生 > >[Spark][Python]RDD flatMap 操作例子

[Spark][Python]RDD flatMap 操作例子

line var 元素 bsp ini atd 執行函數 clas park

RDD flatMap 操作例子:

flatMap,對原RDD的每個元素(行)執行函數操作,然後把每行都“拍扁”

[[email protected] ~]$ hdfs dfs -put cats.txt
[[email protected] ~]$ hdfs dfa -cat cats.txt
Error: Could not find or load main class dfa
[[email protected] ~]$ hdfs dfs -cat cats.txt
The cat on the mat
The aardvark sat on the sofa


mydata=sc.textFile("cats.txt")

mydata.count()
Out[14]: 2

mydata.take(2)
Out[15]: [u‘The cat on the mat‘, u‘The aardvark sat on the sofa‘]


myflatdata=mydata.flatMap(lambda line: line.split(‘ ‘))
myflatdta.count()
Out[19]: 11

myflatdata.take(2)
Out[20]: [u‘The‘, u‘cat‘]

myflatdata.take(11)
Out[21]:
[u‘The‘,

u‘cat‘,
u‘on‘,
u‘the‘,
u‘mat‘,
u‘The‘,
u‘aardvark‘,
u‘sat‘,
u‘on‘,
u‘the‘,
u‘sofa‘]

[Spark][Python]RDD flatMap 操作例子