![](/img/trans.png)
[英]Reading files from HDFS directory and creating a RDD in Spark using Python
[英]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.