python写spark_使用Python的Spark:将RDD输出保存到文本文件中

我正在使用

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")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值