RDD flatMap 操作例子:

flatMap,对原RDD的每个元素(行)执行函数操作,然后把每行都“拍扁”

[training@localhost ~]$ hdfs dfs -put cats.txt
[training@localhost ~]$ hdfs dfa -cat cats.txt
Error: Could not find or load main class dfa
[training@localhost ~]$ 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']

 

相关文章:

  • 2021-10-30
  • 2021-08-17
  • 2022-12-23
  • 2022-12-23
  • 2021-08-26
  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
猜你喜欢
  • 2022-12-23
  • 2021-11-09
  • 2021-11-08
  • 2021-07-19
  • 2021-11-23
  • 2021-05-29
  • 2021-06-09
相关资源
相似解决方案