使用Python实现Hadoop MapReduce程序

原文请参考:

http://blog.csdn.net/zhaoyl03/article/details/8657031/

下面只是将mapper.py和reducer.py在windows上运行了一遍,没有用Hadoop的环境去测试。

环境准备:

  1. Window 7 – 32
  2. 安装GunWin32,使得Linux命令可以在cmd上执行
  3. 安装IDLE (Python GUI),使得Python脚本可以执行
  4. 将Python的安装路径添加到windows的环境变量中,使得在cmd窗口中切换到Python脚本所在目录时,通过输入脚本名,可以直接执行Python脚本

我的Python安装在: C:\Python27\python.exe下

测试脚本放在: E:\PythonTest下

windows环境变量中增加:C:\Python27

mapper.py :

 

#!/usr/bin/env python  

import sys  

# input comes from STDIN (standard input)
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip()
# split the line into words
words = line.split()
# increase counters
for word in words:
# write the results to STDOUT (standard output);
# what we output here will be the input for the
# Reduce step, i.e. the input for reducer.py
#
# tab-delimited; the trivial word count is 1
print '%s\t%s' % (word, 1)

 

reducer.py :

 

#!/usr/bin/env python  

from operator import itemgetter
import sys current_word = None
current_count = 0
word = None # input comes from STDIN
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip() # parse the input we got from mapper.py
word, count = line.split('\t', 1) # convert count (currently a string) to int
try:
count = int(count)
except ValueError:
# count was not a number, so silently
# ignore/discard this line
continue # this IF-switch only works because Hadoop sorts map output
# by key (here: word) before it is passed to the reducer
if current_word == word:
current_count += count
else:
if current_word:
# write result to STDOUT
print '%s\t%s' % (current_word, current_count)
current_count = count
current_word = word # do not forget to output the last word if needed!
if current_word == word:
print '%s\t%s' % (current_word, current_count)

输出结果:

Python & MapReduce的更多相关文章

  1. Python+MapReduce实现矩阵相乘

    算法原理 map阶段 在map阶段,需要做的是进行数据准备.把来自矩阵A的元素aij,标识成p条<key, value>的形式,key="i,k",(其中k=1,2,. ...

  2. Writing an Hadoop MapReduce Program in Python

    In this tutorial I will describe how to write a simpleMapReduce program for Hadoop in thePython prog ...

  3. 使用Python实现Hadoop MapReduce程序

    转自:使用Python实现Hadoop MapReduce程序 英文原文:Writing an Hadoop MapReduce Program in Python 根据上面两篇文章,下面是我在自己的 ...

  4. 用python写MapReduce函数——以WordCount为例

    尽管Hadoop框架是用java写的,但是Hadoop程序不限于java,可以用python.C++.ruby等.本例子中直接用python写一个MapReduce实例,而不是用Jython把pyth ...

  5. 用Python语言写Hadoop MapReduce程序Writing an Hadoop MapReduce Program in Python

    In this tutorial I will describe how to write a simple MapReduce program for Hadoop in the Python pr ...

  6. Hadoop:使用Mrjob框架编写MapReduce

    Mrjob简介 Mrjob是一个编写MapReduce任务的开源Python框架,它实际上对Hadoop Streaming的命令行进行了封装,因此接粗不到Hadoop的数据流命令行,使我们可以更轻松 ...

  7. MapReduce原理及其主要实现平台分析

    原文:http://www.infotech.ac.cn/article/2012/1003-3513-28-2-60.html MapReduce原理及其主要实现平台分析 亢丽芸, 王效岳, 白如江 ...

  8. MapReduce实现PageRank算法(邻接矩阵法)

    前言 之前写过稀疏图的实现方法,这次写用矩阵存储数据的算法实现,只要会矩阵相乘的话,实现这个就很简单了.如果有不懂的可以先看一下下面两篇随笔. MapReduce实现PageRank算法(稀疏图法) ...

  9. 资源list:Github上关于大数据的开源项目、论文等合集

    Awesome Big Data A curated list of awesome big data frameworks, resources and other awesomeness. Ins ...

随机推荐

  1. php(验证网址是否存在)错误

      $ra=get_headers('http://hi.baidu.com'); if($ra[0]==='HTTP/1.1 200 OK'){ echo 'ok'; } 这是错误的,因为有时会返回 ...

  2. 重力感应操控(unity iphone)

    方案一:speed 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public var simulateAcceleromet ...

  3. NLTK中的词性

    NOUN n,VERB v ,ADJ a, ADV r, ADJ_SAT s     NOUN: [('s', ''), ('ses', 's'), ('ves', 'f'), ('xes', 'x' ...

  4. linq中AsEnumerable和AsQueryable的区别

    本文导读:用Linq来操作集合的时候会用到AsQueryable()和AsEnumerable(),何时该用AsQueryable()和何时该用AsEnumerable(),或许存在些疑惑.AsQue ...

  5. ABBYY FineReader 12扫描界面介绍

    ABBYY FineReader 12OCR图文识别软件自身拥有着自己的扫描界面,一般在默认情况下,ABBYY FineReader 使用其自身的扫描界面.本文就解析了ABBYY FineReader ...

  6. linux安全

    http://drops.wooyun.org/ 最专业的安全知识分享平台 http://www.freebuf.com/ 关注黑客与极客 http://book.beifabook.com/prod ...

  7. OpenJudge计算概论-扩号匹配问题【这个用到了栈的思想】

    /*====================================================================== 扩号匹配问题 总时间限制: 1000ms 内存限制: ...

  8. SDWebImage使用详解

    这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下载.同一个URL下载次数控制和优化等特征.使用示范的代码:UITableView使用UIImageView+ ...

  9. Java的大数操作分为BigInteger和BigDecimal

    Java的大数操作分为BigInteger和BigDecimal,但这两给类是分开使用的,有时候在编程的时候显得略微繁琐,现在编写了一个将二者合二为一的大数操作类. 大数操作类代码如下: 1 pack ...

  10. 车载凯立德导航地图更新以及DSA数据更新方法

    每年升级每次都去重新摸索,1年时间忘完了,遂决定把他写下来,我这就去实验,实验好了来补 第一步: 找最新的凯立德软件和地图版本号 方法:淘宝搜索, 例如:凯立德 2016,搜索结果会出现比如2016 ...