簡體   English   中英

Spark 使用 Python:將 RDD 輸出保存到文本文件中

[英]Spark using Python : save RDD output into text files

我正在嘗試使用 python 在 spark 中解決字數問題。 但是當我嘗試使用 .saveAsTextFile 命令將輸出 RDD 保存在文本文件中時,我遇到了這個問題。 這是我的代碼。 請幫我。 我被困住了。 感謝您的時間。

import re

from pyspark import SparkConf , SparkContext

def normalizewords(text):
    return re.compile(r'\W+',re.UNICODE).split(text.lower())

conf=SparkConf().setMaster("local[2]").setAppName("sorted result")
sc=SparkContext(conf=conf)

input=sc.textFile("file:///home/cloudera/PythonTask/sample.txt")

words=input.flatMap(normalizewords)

wordsCount=words.map(lambda x: (x,1)).reduceByKey(lambda x,y: x+y)

sortedwordsCount=wordsCount.map(lambda (x,y):(y,x)).sortByKey()

results=sortedwordsCount.collect()

for result in results:
    count=str(result[0])
    word=result[1].encode('ascii','ignore')

    if(word):
        print word +"\t\t"+ count

results.saveAsTextFile("/var/www/myoutput")

因為你收集了results=sortedwordsCount.collect()所以,它不是 RDD。 它將是普通的 python 列表或元組。

如您所知list是 python 對象/數據結構, append是添加元素的方法。

>>> x = []
>>> x.append(5)
>>> x
[5]

同樣, RDD是火花對象/數據結構,而saveAsTextFile是寫入文件的方法。 重要的是它的分布式數據結構。

因此,我們不能在 RDD 上使用append或在列表上使用saveAsTextFile collect是 RDD 上的方法,用於獲取 RDD 到驅動程序內存。

如評論中所述,使用 saveAsTextFile 保存sortedwordsCount或在 python 中打開文件並使用results寫入文件

results=sortedwordsCount.collect()更改為results=sortedwordsCount ,因為使用.collect()結果將是一個列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM