[spark案例学习] 单词计数
数据准备
数据下载:《莎士比亚全集》
我们先来看看原始数据:首先将数据加载到RDD,然后显示数据框的前15行。
shakespeareDF = sqlContext.read.text(fileName)
shakespeareDF.show(15, truncate=False)
输出如下:
+-------------------------------------------------------+
|value |
+-------------------------------------------------------+
|1609 |
| |
|THE SONNETS |
| |
|by William Shakespeare |
| |
| |
| |
| 1 |
| From fairest creatures we desire increase, |
| That thereby beauty's rose might never die, |
| But as the riper should by time decease, |
| His tender heir might bear his memory: |
| But thou contracted to thine own bright eyes, |
| Feed'st thy light's flame with self-substantial fuel,|
+-------------------------------------------------------+
数据清洗
因为原始数据包括标点符号,大小写字符,空行。所以我们需要对数据进行清洗。所以我提供了一个removePunctuation函数。这个函数将去掉了标点,删除了句子两端的多余的空格,并将字符全部转换为小写。
from pyspark.sql.functions import regexp_replace, trim, lower
def removePunctuation(column):
return lower(trim(regexp_replace(column, '[^\w\s]', '')))
为了使用这个函数,我们先来看一个例子。
sentenceDF = (sqlContext
.createDataFrame([('Hi, you!',),
(' No under_score!',),
(' * Remove punctuation then spaces * ',)], ['sentence']))
sentenceDF.show(truncate=False)
原始的数据框输出如下:
+------------------------------------------+
|sentence |
+------------------------------------------+
|Hi, you! |
| No under_score! |
| * Remove punctuation then spaces * |
+------------------------------------------+
接下来使用removePunctuation进行清洗。
from pyspark.sql.functions import col
(sentenceDF
.select(removePunctuation(col('sentence')).alias('sentence'))
.show(truncate=False))
清洗后的数据框输出如下:
+------------------------------+
|sentence |
+------------------------------+
|hi you |
|no under_score |
|remove punctuation then spaces|
+------------------------------+
有了这个函数,我们就能对《莎士比亚全集》进行清洗了,首先将shakespeare.txt加载到RDD,并使用removePunctuation函数对数据进行清洗.
from pyspark.sql.functions import col
fileName = "shakespeare.txt"
shakespeareDF = (sqlContext
.read
.text(fileName)
.select(removePunctuation(col('value')).alias('value')))
shakespeareDF.show(15, truncate=False)
清洗后的数据框输出如下:
+-------------------------------------------------+
|value |
+-------------------------------------------------+
|1609 |
| |
|the sonnets |
| |
|by william shakespeare |
| |
| |
| |
|1 |
|from fairest creatures we desire increase |
|that thereby beautys rose might never die |
|but as the riper should by time decease |
|his tender heir might bear his memory |
|but thou contracted to thine own bright eyes |
|feedst thy lights flame with selfsubstantial fuel|
+-------------------------------------------------+
接下来,我们使用split函数分隔每一行的句子,然后用explode函数将行转列,得到一个包括所有单词的数据框,最后使用where函数过滤掉数据框的空行。
from pyspark.sql.functions import split, explode
shakeWordsDF = (shakespeareDF
.select(explode(split(shakespeareDF.value, ' ')).alias('word'))
.where("word<>''"))
shakeWordsDF.show()
shakeWordsDFCount = shakeWordsDF.count()
print shakeWordsDFCount
转换后的数据框输出如下:
+-----------+
| word|
+-----------+
| 1609|
| the|
| sonnets|
| by|
| william|
|shakespeare|
| 1|
| from|
| fairest|
| creatures|
| we|
| desire|
| increase|
| that|
| thereby|
| beautys|
| rose|
| might|
| never|
| die|
+-----------+
数据统计
为了统计单词数,我提供一个wordCount函数,它作用是按单词进行分组,然后统计各个分组中单词的个数,最后返回包含word和count列的数据框。
def wordCount(wordListDF):
return wordListDF.groupBy('word').count()
先来看一个使用wordCount函数的例子:
wordsDF = (sqlContext
.createDataFrame([('cat',), ('elephant',), ('rat',), ('rat',), ('cat', )], ['word']))
wordCount(wordsDF).show()
wordCount(words)
wordCount函数返回的数据框输出如下:
+--------+-----+
| word|count|
+--------+-----+
| cat| 2|
| rat| 2|
|elephant| 1|
+--------+-----+
接下来使用wordCount函数统计《莎士比亚全集》的单词数,然后按照count列降序排列。
from pyspark.sql.functions import desc
topWordsAndCountsDF = wordCount(shakeWordsDF).orderBy(desc('count'))
topWordsAndCountsDF.show()
排序后的数据框输出如下所示
+----+-----+
|word|count|
+----+-----+
| the|27361|
| and|26028|
| i|20681|
| to|19150|
| of|17463|
| a|14593|
| you|13615|
| my|12481|
| in|10956|
|that|10890|
| is| 9134|
| not| 8497|
|with| 7771|
| me| 7769|
| it| 7678|
| for| 7558|
| be| 6857|
| his| 6857|
|your| 6655|
|this| 6602|
+----+-----+
总结
可以看到,出现次数较多的单词大都是停用词。
[spark案例学习] 单词计数的更多相关文章
- hadoop笔记之MapReduce的应用案例(WordCount单词计数)
MapReduce的应用案例(WordCount单词计数) MapReduce的应用案例(WordCount单词计数) 1. WordCount单词计数 作用: 计算文件中出现每个单词的频数 输入结果 ...
- 大数据学习——Storm学习单词计数案例
需求:计算单词在文档中出现的次数,每出现一次就累加一次 遇到的问题 这个问题是<scope>provided</scope>作用域问题 https://www.cnblogs. ...
- [spark案例学习] WEB日志分析
数据准备 数据下载:美国宇航局肯尼迪航天中心WEB日志 我们先来看看数据:首先将日志加载到RDD,并显示出前20行(默认). import sys import os log_file_path =' ...
- Storm入门2-单词计数案例学习
[本篇文章主要是通过一个单词计数的案例学习,来加深对storm的基本概念的理解以及基本的开发流程和如何提交并运行一个拓扑] 单词计数拓扑WordCountTopology实现的基本功能就是不停地读入 ...
- Spark学习笔记1——第一个Spark程序:单词数统计
Spark学习笔记1--第一个Spark程序:单词数统计 笔记摘抄自 [美] Holden Karau 等著的<Spark快速大数据分析> 添加依赖 通过 Maven 添加 Spark-c ...
- Spark: 单词计数(Word Count)的MapReduce实现(Java/Python)
1 导引 我们在博客<Hadoop: 单词计数(Word Count)的MapReduce实现 >中学习了如何用Hadoop-MapReduce实现单词计数,现在我们来看如何用Spark来 ...
- Spark基础学习精髓——第一篇
Spark基础学习精髓 1 Spark与大数据 1.1 大数据基础 1.1.1 大数据特点 存储空间大 数据量大 计算量大 1.1.2 大数据开发通用步骤及其对应的技术 大数据采集->大数据预处 ...
- Kafka与Spark案例实践
1.概述 Kafka系统的灵活多变,让它拥有丰富的拓展性,可以与第三方套件很方便的对接.例如,实时计算引擎Spark.接下来通过一个完整案例,运用Kafka和Spark来合理完成. 2.内容 2.1 ...
- 使用Scala实现Java项目的单词计数:串行及Actor版本
其实我想找一门“具有Python的简洁写法和融合Java平台的优势, 同时又足够有挑战性和灵活性”的编程语言. Scala 就是一个不错的选择. Scala 有很多语言特性, 建议先掌握基础常用的: ...
随机推荐
- web前端安全 XSS跨站脚本 CSRF跨站请求伪造 SQL注入
web安全,从前端做起,总结下web前端安全的几种技术: 1,XSS XSS的全称是Cross Site Scripting,意思是跨站脚本,XSS的原理也就是往HTML中注入脚本,HTML指定了脚本 ...
- pdo 事物的处理
- UML类图画法及其之间的几种关系(转)
UML类图画法及其之间的几种关系 最近做重构项目,需要画一下类图,发现类图的画法及其之间的几种关系已经淡忘了很多,所以整理总结一下,有问题的地方大家可以一起讨论下. 文章目录如下: 类图画法 类之间的 ...
- CC1310电源管脚
对于48pin脚的CC1310而言,属于电源类的管脚如下: 上述电源类管脚的关系如下: 1 VDDS类管脚 VDDS类管脚包括VDDS.VDDS2.VDDS3和VDDS_DCDC四个管脚.其中VDDS ...
- Android WIFI 分析(二)
本文介绍Wifi 分析线路二:在Setting中打开WiFi功能.扫描网络以及连接网络的流程. WifiSettings 无线网络设置界面 WifiEnabler 相当于无线网络设置开关 WifiDi ...
- Activity之间传递参数(一)
-------siwuxie095 传递简单数据 (1)首先创建一个项目:SendArgs (2)选择API:21 Android 5.0 (3)选择 Empty Activity (4)默认 (5) ...
- Oracle中的正则表达式
检查约束 --密码的长度必须在3-6 --年龄必须在1-120 --性别只能是男或女 --电话号码必须满足电话的格式: 手机格式,座机格式 drop table test; select * from ...
- 阅读笔记Multi-task Learning for Stock Selection [NIPS1996]
Multi-task Learning for Stock Selection Joumana Ghosn and Yoshua Bengio 摘要 用人工神经网络预测未来回报以便于做出对应的金融决 ...
- 真正解决问题:maven eclipse tomcat java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
在使用eclipse进行整合springmvc时经常会碰到这样的异常: java.lang.ClassNotFoundException:org.springframework.web.context ...
- redis集群安装
1.普通安装 安装环境 centos 6.8 1.安装必要包 yum install gcc yum -y install wget 2.下载解压 wget http://download.redis ...