【问题标题】:sorting collection of values from a text file and saving sorted values back to text file with pyspark对文本文件中的值集合进行排序,并使用 pyspark 将排序后的值保存回文本文件
【发布时间】:2016-07-01 17:30:28
【问题描述】:

我正在尝试使用以下格式的记录对文本文件进行排序 输入.txt:

b1 xy
a2 pq

这是我的 pySpark 代码:

distFile = sc.textFile(input.txt)
words = distFile.map(lambda x: [x[:2],x[2:]])
words.saveAsTextFile("output")

这就是我得到的输出内容

输出/part-00000

[u'a2', u'pq']
[u'b1', u'xy']

我希望内容是:

a2 pq
b1 xy

我做错了什么?

当我使用words.saveAsPickleFile("output")时,我也会得到垃圾值

【问题讨论】:

    标签: apache-spark pyspark rdd bigdata


    【解决方案1】:

    您需要将所有strings 合并为一个。类似于:

    rdd = sc.parallelize([("Roger", "Andrew"),
                          ("Melissa", "Goldsmith")])
    
    words = rdd.map(lambda (n, ln): n + " " + ln)
    
    words.repartition(1).saveAsTextFile("output")
    

    这是结果:

    【讨论】:

      猜你喜欢
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多