2.安装Spark与Python练习

一、安装Spark

1.检查基础环境hadoop,jdk

 

2.下载spark

(已完成,略过)

3.解压,文件夹重命名、权限

(已完成,略过)

4.配置文件

(已完成,略过)

5.环境变量

 

 

 6.试运行Python代码

 

 

 

二、Python编程练习:英文文本的词频统计

1.准备文本文件

 

 

 2.读文件

path='/home/hadoop/test.txt'
with open(path) as f:
    text=f.read()
 
3.预处理:大小写,标点符号,停用词,分词
words = text.lower().split()
for c in '!"#$%^&*()_+-=@[]{}|\?/<>,.:;~·`、“”‘’':
      text = text.replace(c, " ")
t={}
 
4.统计每个单词出现的次数,词频大小排序
for word in words:
    t[word]=t.get(word,0)+1
tlist=list(t.items())
tlist.sort(key=lambda x:x[1],reverse=True)
print(tlist)
 
5.结果写文件
text = open("ttest.txt", "w", encoding='UTF-8')
text.write(str(tlist))

 

 

 
 
posted on 2022-03-06 17:06  ITY21  阅读(36)  评论(0编辑  收藏  举报