2.安装Spark与Python练习

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 @ 2022-03-07 12:01  上上签c  阅读(9)  评论(0编辑  收藏  举报