一、词频统计
A. 分步骤实现
- 准备文件
- 下载小说或长篇新闻稿
- 上传到hdfs上
- 读文件创建RDD
- 分词
4.排除大小写lower(),map()
标点符号re.split(pattern,str),flatMap(),
停用词,可网盘下载stopwords.txt,filter(),
长度小于2的词filter()
5.统计词频
6.按词频排序
7.输出到文件
8.查看结果
B. 一句话实现:文件入文件出
lines = sc.textFile("hdfs://localhost:9000/user/hadoop/test.txt").flatMap(lambda line: line.split()).flatMap(lambda line: re.split(r'\W', line)).flatMap(lambda line: line.split()).map(lambda word: word.lower()).filter(lambda x: x not in stopwords).filter(lambda x: len(x) > 2).map(lambda a: (a, 1)).reduceByKey(lambda a, b: a + b).sortBy(lambda x: x[1], False)
C.和作业2的“二、Python编程练习:英文文本的词频统计 ”进行比较,理解Spark编程的特点。
Spark运行速度快、易用性好、通用性强和随处运行。
二、求Top值