MR编程模式因为云计算的兴趣而火了起来,但是分布的云计算环境个人很难搭建,为了便于大家学习MR编程模式,这里介绍一个给力的python工具octo.py。
它只是一个小小的python文件,但是确可以较为方便的用来进行MR模式编程,这样不用搭建云计算环境就能学习如何以MR方式编程了。
这里以本人刚完成的一个实例做示范,也算是给自己留的一个笔记。
下载程序。需要安装python环境,然后把下载好的octo.py放到Python27下面(如果安装的是python 2.6.X版本的,应该是Python26)PS:本人是windows 7环境
下载数据。数据放在我的微盘 我的微盘 我的微盘 上了。里面的数据格式如下
books/bc/tanselCGSS93/Tuzhilin93:::Alexander Tuzhilin:::Applications of temporal Databases to Knowledge-based Simulations.在两个:::之间的是作者,如果一本书有多个作者,这些作者会以::隔开。第一个:::之前的是book信息存储网站,第二个:::之后的信息是书本的标题即title。
熟悉简单的MR模式,阅读原论文是最好的熟悉。
数据解压出来后是hw3data,在该文件夹下面有很多文件,里面全部是书本及相应作者的信息。
任务是:给定一个作者,马上要知晓其写作书本题目中出现平率最高、次高或者说top-k terms的词汇。这里要避免一些垃圾词汇,称为allStopWords,包括
allStopWords={'about':1, 'above':1, 'after':1, 'again':1, 'against':1, 'all':1, 'am':1, 'an':1, 'and':1, 'any':1, 'are':1, 'arent':1, 'as':1, 'at':1, 'be':1, 'because':1, 'been':1, 'before':1, 'being':1, 'below':1, 'between':1, 'both':1, 'but':1, 'by':1, 'cant':1, 'cannot':1, 'could':1, 'couldnt':1, 'did':1, 'didnt':1, 'do':1, 'does':1, 'doesnt':1, 'doing':1, 'dont':1, 'down':1, 'during':1, 'each':1, 'few':1, 'for':1, 'from':1, 'further':1, 'had':1, 'hadnt':1, 'has':1, 'hasnt':1, 'have':1, 'havent':1, 'having':1, 'he':1, 'hed':1, 'hell':1, 'hes':1, 'her':1, 'here':1, 'heres':1, 'hers':1, 'herself':1, 'him':1, 'himself':1, 'his':1, 'how':1, 'hows':1, 'i':1, 'id':1, 'ill':1, 'im':1, 'ive':1, 'if':1, 'in':1, 'into':1, 'is':1, 'isnt':1, 'it':1, 'its':1, 'its':1, 'itself':1, 'lets':1, 'me':1, 'more':1, 'most':1, 'mustnt':1, 'my':1, 'myself':1, 'no':1, 'nor':1, 'not':1, 'of':1, 'off':1, 'on':1, 'once':1, 'only':1, 'or':1, 'other':1, 'ought':1, 'our':1, 'ours ':1, 'ourselves':1, 'out':1, 'over':1, 'own':1, 'same':1, 'shant':1, 'she':1, 'shed':1, 'shell':1, 'shes':1, 'should':1, 'shouldnt':1, 'so':1, 'some':1, 'such':1, 'than':1, 'that':1, 'thats':1, 'the':1, 'their':1, 'theirs':1, 'them':1, 'themselves':1, 'then':1, 'there':1, 'theres':1, 'these':1, 'they':1, 'theyd':1, 'theyll':1, 'theyre':1, 'theyve':1, 'this':1, 'those':1, 'through':1, 'to':1, 'too':1, 'under':1, 'until':1, 'up':1, 'very':1, 'was':1, 'wasnt':1, 'we':1, 'wed':1, 'well':1, 'were':1, 'weve':1, 'were':1, 'werent':1, 'what':1, 'whats':1, 'when':1, 'whens':1, 'where':1, 'wheres':1, 'which':1, 'while':1, 'who':1, 'whos':1, 'whom':1, 'why':1, 'whys':1, 'with':1, 'wont':1, 'would':1, 'wouldnt':1, 'you':1, 'youd':1, 'youll':1, 'youre':1, 'youve':1, 'your':1, 'yours':1, 'yourself':1, 'yourselves':1}
如果用关系数据库去做,当然可以,但会非常慢。而且,当查询的任务需求改变,需要再次重写程序和数据库设计。
代码直接上来(保存该文件在Python27目录下,例如名字为test.py):
import glob read_files=glob.glob(r"D:/hw3data/*") '''python 中的glob可以阅读特定文件夹下的指定类型文件'''' def file_contents(file_name): f = open(file_name,'r') try: return f.read() finally: f.close() source=dict((file_name,file_contents(file_name)) for file_name in read_files) f=open('outfile','w') allStopWords={'about':1, 'above':1, 'after':1, 'again':1, 'against':1, 'all':1, 'am':1, 'an':1, 'and':1, 'any':1, 'are':1, 'arent':1, 'as':1, 'at':1, 'be':1, 'because':1, 'been':1, 'before':1, 'being':1, 'below':1, 'between':1, 'both':1, 'but':1, 'by':1, 'cant':1, 'cannot':1, 'could':1, 'couldnt':1, 'did':1, 'didnt':1, 'do':1, 'does':1, 'doesnt':1, 'doing':1, 'dont':1, 'down':1, 'during':1, 'each':1, 'few':1, 'for':1, 'from':1, 'further':1, 'had':1, 'hadnt':1, 'has':1, 'hasnt':1, 'have':1, 'havent':1, 'having':1, 'he':1, 'hed':1, 'hell':1, 'hes':1, 'her':1, 'here':1, 'heres':1, 'hers':1, 'herself':1, 'him':1, 'himself':1, 'his':1, 'how':1, 'hows':1, 'i':1, 'id':1, 'ill':1, 'im':1, 'ive':1, 'if':1, 'in':1, 'into':1, 'is':1, 'isnt':1, 'it':1, 'its':1, 'its':1, 'itself':1, 'lets':1, 'me':1, 'more':1, 'most':1, 'mustnt':1, 'my':1, 'myself':1, 'no':1, 'nor':1, 'not':1, 'of':1, 'off':1, 'on':1, 'once':1, 'only':1, 'or':1, 'other':1, 'ought':1, 'our':1, 'ours ':1, 'ourselves':1, 'out':1, 'over':1, 'own':1, 'same':1, 'shant':1, 'she':1, 'shed':1, 'shell':1, 'shes':1, 'should':1, 'shouldnt':1, 'so':1, 'some':1, 'such':1, 'than':1, 'that':1, 'thats':1, 'the':1, 'their':1, 'theirs':1, 'them':1, 'themselves':1, 'then':1, 'there':1, 'theres':1, 'these':1, 'they':1, 'theyd':1, 'theyll':1, 'theyre':1, 'theyve':1, 'this':1, 'those':1, 'through':1, 'to':1, 'too':1, 'under':1, 'until':1, 'up':1, 'very':1, 'was':1, 'wasnt':1, 'we':1, 'wed':1, 'well':1, 'were':1, 'weve':1, 'were':1, 'werent':1, 'what':1, 'whats':1, 'when':1, 'whens':1, 'where':1, 'wheres':1, 'which':1, 'while':1, 'who':1, 'whos':1, 'whom':1, 'why':1, 'whys':1, 'with':1, 'wont':1, 'would':1, 'wouldnt':1, 'you':1, 'youd':1, 'youll':1, 'youre':1, 'youve':1, 'your':1, 'yours':1, 'yourself':1, 'yourselves':1} def final(key, value): print key, value f.write(str((key, value))) f.write(str('\n')) '''为了便于查看结果,最后的outfile中是分行的''' def mapfn(key, value): for line in value.splitlines(): line_split=line.split(':::') titles=line_split[2] titles=titles.replace('.',' ') titles=titles[:-1] authors_list=line_split[1] authors=authors_list.split('::') for author in authors: yield author, titles.lower() def reducefn(key, value): w={} for titles in value: title=titles.split(' ') for word in title: if title not in allStopWords.keys(): if word in w: w[word]=w[word]+1 else: w[word]=1 w_sorted=sorted(w.iteritems(),key=lambda d:d[1],reverse=False) return key, w_sorted
然后打开一个cmd窗口运行server,需要在Python27目录下,然后输入命令 python octo.py server test.py,就会看到服务器起来了
然后可以打开若干个cmd窗口,同样cd到Python27目录下,运行多个client。只需要输入命令python octo.py client localhost
可以通过localhost指定主机,这个还没试过,不知道是只需要指定ip,还是必须指定ip+端口,有机会试试
测试例子:Sudhakar M. Reddy所作书中,出现频率最高的词汇应该是circuits(15次)
reduce阶段的value,原来是key相同的一系列value,组成了一个value list,这些value list就是map阶段产生的。
在python中string操作的split和replace时,原字符串是不变的,所以要赋值
在产生的outfile中会发现有乱码,这是因为有一些作者的名字是latin字母,而outfile很可能是asc编码,这个问题没搞定
相关论文推荐阅读:
mapreduce: simplified data processing on large clusters--by Jeffrey Dean and Sanjay Ghemawat
sorting, searching and simulation in the MapReduce Framework--Michael T.Goodrich
Investigation of data locality and fairness in MapReduce--Zhenhua Gao
PS:还有一个同样轻量,但是更加高效的python实现,mincemeat,网上有一个还可以的中文教程