[Spark][Python]获得 key,value形式的 RDD


[training@localhost ~]$ cat users.txt
user001 Fred Flintstone
user090 Bugs Bunny
user111 Harry Potter
[training@localhost ~]$ hdfs dfs -put users.txt
[training@localhost ~]$
[training@localhost ~]$
[training@localhost ~]$ hdfs dfs -cat users.txt
user001 Fred Flintstone  <<<<<<<<<<<<<<<<<<,  tab 符 分隔
user090 Bugs Bunny
user111 Harry Potter
[training@localhost ~]$

 user01 = sc.textFile("users.txt")

user02 = user01.map(lambda line : line.split("\t"))

In [16]: user02.take(3)
Out[16]:
[[u'user001', u'Fred Flintstone'],
[u'user090', u'Bugs Bunny'],
[u'user111', u'Harry Potter']]

user03 = user02.map(lambda fields: (fields[0],fields[1]))

user03.take(3)

Out[20]:
[(u'user001', u'Fred Flintstone'), <<<<<<<<<<<<<<<< 此处构筑了 key-value pair
(u'user090', u'Bugs Bunny'),
(u'user111', u'Harry Potter')]

相关文章:

  • 2021-05-06
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
  • 2021-10-27
  • 2021-04-23
  • 2022-01-19
猜你喜欢
  • 2021-04-21
  • 2021-05-29
  • 2021-10-27
  • 2022-12-23
  • 2022-12-23
  • 2021-08-24
  • 2022-12-23
相关资源
相似解决方案