这是CS100.1x第一个提交的有意义的作业,自己一遍做下来对PySpark的基本应用应该是可以掌握的。相关ipynb文件见我github

这次作业的目的如题目一样——word count,作业分成4个部分,遇到不懂的地方,时刻记得查API。

Part 1 Creating a base RDD and pair RDDs

Create a base RDD

wordsList = ['cat', 'elephant', 'rat', 'rat', 'cat']
wordsRDD = sc.parallelize(wordsList, 4)
# Print out the type of wordsRDD
print type(wordsRDD)

我们通过sc.parallelize方法来把python的list创建为RDD,打印的结果是

<class 'pyspark.rdd.RDD'>

我们可以看到,wordsRDD已经变成了pyspark.rdd.RDD类

Pluralize and test

下面几个将介绍map()的用法。注释里面带有TODO的就表示这里是作业,具体的功能实现在函数的注释里有。测试用例我就不在这里写了,大家可以去我的github上下载源文件。我这里重点讲功能实现。

# TODO: Replace <FILL IN> with appropriate code
def makePlural(word):
"""Adds an 's' to `word`. Note:
This is a simple function that only adds an 's'. No attempt is made to follow proper
pluralization rules. Args:
word (str): A string. Returns:
str: A string with 's' added to it.
"""
return word + 's' print makePlural('cat') # One way of completing the function
def makePlural(word):
return word + 's' print makePlural('cat')

这里的函数实现就仅仅把单数变复数,所以在字符串上加's'就行。

Apply makePlural to the base RDD

# TODO: Replace <FILL IN> with appropriate code
pluralRDD = wordsRDD.map(makePlural)
print pluralRDD.collect()

我可以看到map()里放入的是一个函数,作用是对RDD里的每个元素实现该函数的操作。

Pass a lambda function to map

# TODO: Replace <FILL IN> with appropriate code
pluralLambdaRDD = wordsRDD.map(lambda x: x + 's')
print pluralLambdaRDD.collect()

这里的知识点是map和lamda的结合,在后面我们会越来越多的和lamda打交道,因为很多函数我们仅仅只用一次,所以lambda来定义匿名函数是再适合不过了。

这里lamda后面的x可以是任何值,是占位符的作用,表示RDD里的任何一个元素,冒号后面的表示你要对该元素的操作。就这么简单。

Length of each word

# TODO: Replace <FILL IN> with appropriate code
pluralLengths = (pluralRDD
.map(lambda x:len(x))
.collect())
print pluralLengths

这里要返回每个元素的长度,所以直接返回len(x)就行。

Pair RDDs

这里要介绍的是pair RDD,也就是key-value的RDD。python中,用tuple来实现。题目要求是把原来的RDD里的值变成(word, 1)的样子,用lambda很好解决。

# TODO: Replace <FILL IN> with appropriate code
wordPairs = wordsRDD.map(lambda x: (x,1))
print wordPairs.collect()

Part 2 Counting with pair RDDs

groupByKey() approach

我们先用groupByKey()来进行count word的操作。这个函数会把RDD里面相同的key放到一个list里面,然后存在一个数据块上。但是这样存在两个问题:

  1. 这种操作涉及到大量不同机器不同数据块的数据移动
  2. 生成的list会很大,可能对某一个worker来说,负担太重
# TODO: Replace <FILL IN> with appropriate code
# Note that groupByKey requires no parameters
wordsGrouped = wordPairs.groupByKey()
for key, value in wordsGrouped.collect():
print '{0}: {1}'.format(key, list(value))

我们看到,用法其实很简单,直接使用就行。但是最后的结果是这样,并没有把结果相加

[('cat', [1, 1]), ('elephant', [1]), ('rat', [1, 1])]

Use groupByKey() to obtain the counts

我们看到上面的结果是一个key对应了一个list,而并非是真正的结果,所以这里还需要进一步操作,计算list的长度,或者求和都可以。

# TODO: Replace <FILL IN> with appropriate code
wordCountsGrouped = wordsGrouped.mapValues(lambda x : sum(list(x)))
print wordCountsGrouped.collect()

这里我用了一个新的方法mapValues(),这个方法是直接传入value进行计算,不改变key的值。

Counting using reduceByKey

reduceByKey()相比于groupByKey()就有效得多。因为它是在本地计算好了后,进行reduce操作,类似与MapReduce里的combiner。其实这里还可以这么写

wordCounts = wordPairs.reduceByKey(add)

但是这么不让from operator import add,所以写成lambda表达式。

All together

把前面所有的知识点写成一句。。。

# TODO: Replace <FILL IN> with appropriate code
wordCountsCollected = (wordsRDD
.map(lambda x: (x,1))
.reduceByKey(lambda a,b: a+b)
.collect())
print wordCountsCollected

Part 3 Finding unique words and a mean value

Unique words

这里是计算RDD里不同单词的数目。我们在前面已经reduce过了,实际上把相同的合并了,这里只需要计算法RDD里的元素个数就行

# TODO: Replace <FILL IN> with appropriate code
uniqueWords = wordCounts.count()
print uniqueWords

Mean using reduce

这里是要把所有的value加起来,然后除以元素的个数,也就是平均每个元素有多少次。用到了一个新的函数values(),这个方法直接返回所有value的list。

# TODO: Replace <FILL IN> with appropriate code
from operator import add
totalCount = (wordCounts
.values()
.reduce(add))
average = totalCount / float(wordCounts.count())
print totalCount
print round(average, 2)

Part 4 Apply word count to a file

这里是综合了前面所有的知识,来对一个文本文件进行处理,还涉及到文本文件处理的一些方法。

wordCount function

# TODO: Replace <FILL IN> with appropriate code
def wordCount(wordListRDD):
"""Creates a pair RDD with word counts from an RDD of words. Args:
wordListRDD (RDD of str): An RDD consisting of words. Returns:
RDD of (str, int): An RDD consisting of (word, count) tuples.
"""
return wordListRDD.map(lambda x: [x,1]).reduceByKey(lambda a,b: a+b)
print wordCount(wordsRDD).collect()

Capitalization and punctuation

真实的文本文件远比我们之前碰到的例子复杂。我们需要考虑三个问题:

  1. 单词里的大小写,例如Spark和spark应该是同一个单词
  2. 标点符号要去掉
  3. 单词前后的空格也要去掉
# TODO: Replace <FILL IN> with appropriate code
import re
def removePunctuation(text):
"""Removes punctuation, changes to lower case, and strips leading and trailing spaces. Note:
Only spaces, letters, and numbers should be retained. Other characters should should be
eliminated (e.g. it's becomes its). Leading and trailing spaces should be removed after
punctuation is removed. Args:
text (str): A string. Returns:
str: The cleaned up string.
"""
return re.sub(r'[^\w\s]','',text).strip().lower()
print removePunctuation('Hi, you!')
print removePunctuation(' No under_score!')

这里re.sub()里面的正则表达式有点复杂。大概意思是把所有的标点都换成'',这里的r表示原生字符串,也就是不进行转义,^表示否,\w和\s分别表示单词和空格。所以结果是只保留单词和空格。

Load a text file

这里用到了Complete Works of William Shakespeare,来自Project Gutenberg,作为文本。

具体文件从哪里来,我们不用管,运行就行。

# Just run this code
import os.path
baseDir = os.path.join('data')
inputPath = os.path.join('cs100', 'lab1', 'shakespeare.txt')
fileName = os.path.join(baseDir, inputPath) shakespeareRDD = (sc
.textFile(fileName, 8)
.map(removePunctuation))
print '\n'.join(shakespeareRDD
.zipWithIndex() # to (line, lineNum)
.map(lambda (l, num): '{0}: {1}'.format(num, l)) # to 'lineNum: line'
.take(15))

Words from lines

我们这里要把行转为单词。上面的RDD中,每个元素都是一行。假如我们直接用split方法,最后是生成一个list,我们还需要把list解开,所以这里用flatMap()

# TODO: Replace <FILL IN> with appropriate code
shakespeareWordsRDD = shakespeareRDD.flatMap(lambda x : x.split(' '))
shakespeareWordCount = shakespeareWordsRDD.count()
print shakespeareWordsRDD.top(5)
print shakespeareWordCount

Remove empty elements

在上面操作的基础上,去掉空的单词。

# TODO: Replace <FILL IN> with appropriate code
shakeWordsRDD = shakespeareWordsRDD.filter(lambda x: x !='')
shakeWordCount = shakeWordsRDD.count()
print shakeWordCount

Count the words

这个就和之前的工作一样了。不多说了。

# TODO: Replace <FILL IN> with appropriate code
top15WordsAndCounts = wordCount(shakeWordsRDD).takeOrdered(15,key=lambda(w,c):-c)
print '\n'.join(map(lambda (w, c): '{0}: {1}'.format(w, c), top15WordsAndCounts))

CS100.1x-lab1_word_count_student的更多相关文章

  1. CS100.1x Introduction to Big Data with Apache Spark

    CS100.1x简介 这门课主要讲数据科学,也就是data science以及怎么用Apache Spark去分析大数据. Course Software Setup 这门课主要介绍如何编写和调试Py ...

  2. CS190.1x Scalable Machine Learning

    这门课是CS100.1x的后续课,看课程名字就知道这门课主要讲机器学习.难度也会比上一门课大一点.如果你对这门课感兴趣,可以看看我这篇博客,如果对PySpark感兴趣,可以看我分析作业的博客. Cou ...

  3. CS100.1x-lab0_student

    这是CS100.1x第一个提交的作业,是给我们测试用的.相关ipynb文件见我github.本来没什么好说的.我在这里简单讲一下,后面会更详细的讲解.主要分成5个部分. Part 1: Test Sp ...

  4. Introduction to Big Data with PySpark

    起因 大数据时代 大数据最近太热了,其主要有数据量大(Volume),数据类别复杂(Variety),数据处理速度快(Velocity)和数据真实性高(Veracity)4个特点,合起来被称为4V. ...

  5. Ubuntu16.04 802.1x 有线连接 输入账号密码,为什么连接不上?

    ubuntu16.04,在网络配置下找到802.1x安全性,输入账号密码,为什么连接不上?   这是系统的一个bug解决办法:假设你有一定的ubuntu基础,首先你先建立好一个不能用的协议,就是按照之 ...

  6. 解压版MySQL5.7.1x的安装与配置

    解压版MySQL5.7.1x的安装与配置 MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大家的英文可以看懂英 ...

  7. RTImageAssets 自动生成 AppIcon 和 @2x @1x 比例图片

    下载地址:https://github.com/rickytan/RTImageAssets 此插件用来生成 @3x 的图片资源对应的 @2x 和 @1x 版本,只要拖拽高清图到 @3x 的位置上,然 ...

  8. 802.1x协议&eap类型

    EAP: 0,扩展认证协议 1,一个灵活的传输协议,用来承载任意的认证信息(不包括认证方式) 2,直接运行在数据链路层,如ppp或以太网 3,支持多种类型认证 注:EAP 客户端---服务器之间一个协 ...

  9. 脱壳脚本_手脱壳ASProtect 2.1x SKE -&gt; Alexey Solodovnikov

    脱壳ASProtect 2.1x SKE -> Alexey Solodovnikov 用脚本.截图 1:查壳 2:od载入 3:用脚本然后打开脚本文件Aspr2.XX_unpacker_v1. ...

随机推荐

  1. HTTP 头 Connection:close 作用

    有的网站会在服务器运行一段时间后down掉,有很多原因可能造成这种现象:比如tomcat堆和非堆内存设置不足,程序没能释放内存空间造成内存溢出,或者某些进程一直运行没能释放,造成cup资源大量消耗. ...

  2. HBase HA的分布式集群部署(适合3、5节点)

    本博文的主要内容有: .HBase的分布模式(3.5节点)安装    .HBase的分布模式(3.5节点)的启动 .HBase HA的分布式集群的安装 .HBase HA的分布式集群的启动    .H ...

  3. 以太坊系列之一: 以太坊RLP用法-以太坊源码学习

    RLP (递归长度前缀)提供了一种适用于任意二进制数据数组的编码,RLP已经成为以太坊中对对象进行序列化的主要编码方式.RLP的唯一目标就是解决结构体的编码问题:对原子数据类型(比如,字符串,整数型, ...

  4. 【转载】MySQl 数据库插入加锁分析

    http://yeshaoting.cn/article/database/mysql%20insert%E9%94%81%E6%9C%BA%E5%88%B6/

  5. mybatis中使用in查询时的注意事项

    1. 当查询的参数只有一个时 findByIds(List<Long> ids)  1.a 如果参数的类型是List, 则在使用时,collection属性要必须指定为 list < ...

  6. 【bzoj4543】[POI2014]Hotel加强版

    题目 抄题解.jpg 发现原来的\(O(n^2)\)的换根\(dp\)好像行不通了呀 我们考虑非常牛逼的长链剖分 我们设\(f[x][j]\)表示在\(x\)的子树中距离\(x\)为\(j\)的点有多 ...

  7. Eclipse的PHP插件PHPEclipse安装和使用

    PHP有很多相当不错的开发工具,如Zend Studio.NetBeans.phpdesigner等,但对于习惯Java编程的程序猿们来说,最常用的还要属Eclipse.那么Eclipse能用于PHP ...

  8. 20175209 实验三《敏捷开发与XP实践》实验报告

    20175209 实验三<敏捷开发与XP实践>实验报告 一.实验内容 编码标准:在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Cod ...

  9. h5py

    解决办法: sudo apt-get install libhdf5-dev sudo apt-get install python-h5py

  10. VC++程序运行时间测试函数

    0:介绍 我们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执行一个特定的操作,比如在多媒体中,比如在游戏中等,都会用到时间函数.还比如我们通过记录函数或者算 ...