功能实现

功能:统计文本文件中所有单词出现的频率功能。

下面是要统计的文本文件

【/root/hadooptest/input.txt】

foo foo quux labs foo bar quux abc bar see you by test welcome test
abc labs foo me python hadoop ab ac bc bec python

编写Map代码

Map代码,它会从标准输入(stdin)读取数据,默认以空格分割单词,然后按行输出单词机器出现频率到标准输出(stdout),不过整个Map处理过程并不会统计每个单词出现的总次数,而是直接输出“word,1”,以便作为Reduce的输入进行统计,要求mapper.py具备执行权限。

【/root/hadooptest/mapper.py】

#!/usr/bin/env python
# -*- coding:utf-8 -*- import sys #输入为标准输入stdin
for line in sys.stdin:
#删除开头和结尾的空行
line = line.strip()
#以默认空格分隔单词到words列表
words = line.split()
for word in words:
#输出所有单词,格式为“单词,1”以便作为Reduce的输入
print '%s\t%s' % (word,1)0

编写Reduce代码

Reduce代码,它会从标准输入(stdin)读取mapper.py的结果,然后统计每个单词出现的总次数并输出到标准输出(stdout),要求reducer.py同样具备可执行 权限。

【/root/hadooptest/reducer.py】

#!/usr/bin/env python
# -*- coding:utf-8 -*- from operator import itemgetter
import sys current_word = None
current_count = 0
word = None #获取标准输入,即mapper.py的标准输出
for line in sys.stdin:
#删除开头和结尾的空行
line = line.strip() #解析mapper.py输出作为程序的输入,以tab作为分隔符
word,count = line.split('\t',1) #转换count从字符型到整型
try:
count = int(count)
except ValueError:
#count非数字时,忽略此行
continue #要求mapper.py的输出做排序(sort)操作,以便对连续的word做判断
if current_word == word:
current_count += count
else:
if current_word:
#输出当前word统计结果到标准输出
print '%s\t%s' % (current_word,current_count)
current_count = count
current_word = word #输出最后一个word统计
if current_word == word:
print '%s\t%s' % (current_word,current_count)

测试代码

在Hadoop平台运行前进行本地测试

[root@wx ~]# cd /root/hadooptest/
[root@wx hadooptest]# cat input.txt | ./mapper.py
foo 1
foo 1
quux 1
labs 1
foo 1
bar 1
quux 1
abc 1
bar 1
see 1
you 1
by 1
test 1
welcome 1
test 1
abc 1
labs 1
foo 1
me 1
python 1
hadoop 1
ab 1
ac 1
bc 1
bec 1
python 1 [root@wx hadooptest]# cat input.txt | ./mapper.py | sort -k1,1 | ./reducer.py
ab 1
abc 2
ac 1
bar 2
bc 1
bec 1
by 1
foo 4
hadoop 1
labs 2
me 1
python 2
quux 2
see 1
test 2
welcome 1
you 1

Hadoop平台运行

在HDFS上创建文本文件存储目录,本示例中为/user/root/word

/usr/local/hadoop-2.6.4/bin/hadoop fs -mkdir -p /user/root/word

将输入文件上传到HDFS,本例中是/root/hadooptest/input.txt

/usr/local/hadoop-2.6.4/bin/hadoop fs -put /root/hadooptest/input.txt /user/root/word

查看/user/root/word目录下的文件

/usr/local/hadoop-2.6.4/bin/hadoop fs -ls /user/root/word
#结果:
Found 1 items
-rw-r--r-- 2 root supergroup 118 2016-03-22 13:36 /user/root/word/input.txt

执行MapReduce任务,输出结果文件制定为/output/word

/usr/local/hadoop-2.6.4/bin/hadoop jar /usr/local/hadoop-2.6.4/share/hadoop/tools/lib/hadoop-streaming-2.6.4.jar -files 'mapper.py,reducer.py' -input /user/root/word -output /output/word -mapper ./mapper.py -reducer ./reducer.py

参数说明:

/usr/local/hadoop-2.6.4/bin/hadoop jar /usr/local/hadoop-2.6.4/share/hadoop/tools/lib/hadoop-streaming-2.6.4.jar \
-input <输入目录> \ # 可以指定多个输入路径,例如:-input '/user/foo/dir1' -input '/user/foo/dir2'
-inputformat <输入格式 JavaClassName> \
-output <输出目录> \
-outputformat <输出格式 JavaClassName> \
-mapper <mapper executable or JavaClassName> \
-reducer <reducer executable or JavaClassName> \
-combiner <combiner executable or JavaClassName> \
-partitioner <JavaClassName> \
-cmdenv <name=value> \ # 可以传递环境变量,可以当作参数传入到任务中,可以配置多个
-file <依赖的文件> \ # 配置文件,字典等依赖
-D <name=value> \ # 作业的属性配置

查看生成的分析结果文件清单,其中/output/word/part-00000为分析结果文件

[root@wx hadooptest]# /usr/local/hadoop-2.6.4/bin/hadoop fs -ls /output/word
Found 2 items
-rw-r--r-- 2 root supergroup 0 2016-03-22 13:47 /output/word/_SUCCESS
-rw-r--r-- 2 root supergroup 110 2016-03-22 13:47 /output/word/part-00000

查看结果数据

[root@wx hadooptest]# /usr/local/hadoop-2.6.4/bin/hadoop fs -cat /output/word/part-00000
ab 1
abc 2
ac 1
bar 2
bc 1
bec 1
by 1
foo 4
hadoop 1
labs 2
me 1
python 2
quux 2
see 1
test 2
welcome 1
you 1

参考资料:

根据刘天斯《Python自动化运维技术与最佳实践》整理

Hadoop:使用原生python编写MapReduce的更多相关文章

  1. Python实现MapReduce,wordcount实例,MapReduce实现两表的Join

    Python实现MapReduce 下面使用mapreduce模式实现了一个简单的统计日志中单词出现次数的程序: from functools import reduce from multiproc ...

  2. Hadoop学习基础之三:MapReduce

    现在是讨论这个问题的不错的时机,因为最近媒体上到处充斥着新的革命所谓“云计算”的信息.这种模式需要利用大量的(低端)处理器并行工作来解决计算问题.实际上,这建议利用大量的低端处理器来构建数据中心,而不 ...

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

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

  4. hive--构建于hadoop之上、让你像写SQL一样编写MapReduce程序

    hive介绍 什么是hive? hive:由Facebook开源用于解决海量结构化日志的数据统计 hive是基于hadoop的一个数据仓库工具,可以将结构化的数据映射为数据库的一张表,并提供类SQL查 ...

  5. [Hadoop in Action] 第4章 编写MapReduce基础程序

    基于hadoop的专利数据处理示例 MapReduce程序框架 用于计数统计的MapReduce基础程序 支持用脚本语言编写MapReduce程序的hadoop流式API 用于提升性能的Combine ...

  6. 在Hadoop上用Python实现WordCount

    一.简单说明 本例中我们用Python写一个简单的运行在Hadoop上的MapReduce程序,即WordCount(读取文本文件并统计单词的词频).这里我们将要输入的单词文本input.txt和Py ...

  7. 在Hadoop平台跑python脚本

    1.开发IDE,我使用的是PyCharm. 2.运行原理       使用python写MapReduce的“诀窍”是利用Hadoop流的API,通过STDIN(标准输入).STDOUT(标准输出)在 ...

  8. Hadoop学习总结之三:Map-Reduce入门

    1.Map-Reduce的逻辑过程 假设我们需要处理一批有关天气的数据,其格式如下: 按照ASCII码存储,每行一条记录 每一行字符从0开始计数,第15个到第18个字符为年 第25个到第29个字符为温 ...

  9. Hadoop学习笔记(5) ——编写HelloWorld(2)

    Hadoop学习笔记(5) ——编写HelloWorld(2) 前面我们写了一个Hadoop程序,并让它跑起来了.但想想不对啊,Hadoop不是有两块功能么,DFS和MapReduce.没错,上一节我 ...

随机推荐

  1. ZOJ 1056 The Worm Turns

    原题链接 题目大意:贪吃蛇的简化版,给出一串操作命令,求蛇的最终状态是死是活. 解法:这条蛇一共20格的长度,所以用一个20个元素的队列表示,队列的每个元素是平面的坐标.每读入一条指令,判断其是否越界 ...

  2. html部分---格式与布局;

    一:position:fixed(相对于浏览器窗口来对元素进行定位) <style type="text/css"> .aa { position:fixed; lef ...

  3. JavaWeb学习记录(六)——用户登录功能之Cookie

    private Cookie nameCookie=null;    private Cookie passCookie=null;    private Cookie cookieUser;     ...

  4. 课堂所讲整理:输入输出流(I/O)2(修改版)

    package org.hanqi.ex; import java.io.*; public class TestFile2 { public static void main(String[] ar ...

  5. min-height for IE6

    1. className{     min-height:500px;     height:auto !important;     height:500px; }   2. 在做页面布局时遇到了i ...

  6. Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 [ ^

    Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character clas ...

  7. Questions?

    http://www.datastax.com/wp-content/themes/datastax-2014-08/files/NoSQL_Benchmarks_EndPoint.pdf http: ...

  8. Apache安全配置方案

    Apache安全配置方案 from:http://drops.wooyun.org/%e8%bf%90%e7%bb%b4%e5%ae%89%e5%85%a8/2727 apache的一些配置主要是通过 ...

  9. 动态从数据库中获取数据填充Select

    JavaScript代码: $(document).ready(function () { getIntype(); });function getIntype(){ $.ajax({ type:&q ...

  10. swagger-editor 快速REST-API 测试文档编写

    1. 在线使用 http://editor.swagger.io/#/ 2. 离线工具 https://github.com/swagger-api/swagger-editor 3. 跨域访问问题: ...