使用Python实现Hadoop MapReduce程序

2023-05-16

为什么80%的码农都做不了架构师?>>>   hot3.png

笔者的机器运行效果如下(输入数据是find的帮助手册,和笔者预期一样,the是最多的):175835_KWmh_123914.png


--------------------------------------以下是原帖---------------------------------

  • 在这个实例中,我将会向大家介绍如何使用Python 为 Hadoop编写一个简单的MapReduce

    程序。
    尽管Hadoop 框架是使用Java编写的但是我们仍然需要使用像C++、Python等语言来实现 Hadoop程序。尽管Hadoop官方网站给的示例程序是使用Jython编写并打包成Jar文件,这样显然造成了不便,其实,不一定非要这样来实现,我们可以使用Python与Hadoop 关联进行编程,看看位于/src/examples/python/WordCount.py  的例子,你将了解到我在说什么。

    我们想要做什么?

    我们将编写一个简单的 MapReduce 程序,使用的是C-Python,而不是Jython编写后打包成jar包的程序。
    我们的这个例子将模仿 WordCount 并使用Python来实现,例子通过读取文本文件来统计出单词的出现次数。结果也以文本形式输出,每一行包含一个单词和单词出现的次数,两者中间使用制表符来想间隔。

    先决条件

    编写这个程序之前,你学要架设好Hadoop 集群,这样才能不会在后期工作抓瞎。如果你没有架设好,那么在后面有个简明教程来教你在Ubuntu Linux 上搭建(同样适用于其他发行版linux、unix)

    如何使用Hadoop Distributed File System (HDFS)在Ubuntu Linux 建立单节点的 Hadoop 集群

    如何使用Hadoop Distributed File System (HDFS)在Ubuntu Linux 建立多节点的 Hadoop 集群


    Python的MapReduce代码


    使用Python编写MapReduce代码的技巧就在于我们使用了 HadoopStreaming 来帮助我们在Map 和 Reduce间传递数据通过STDIN (标准输入)和STDOUT (标准输出).我们仅仅使用Python的sys.stdin来输入数据,使用sys.stdout输出数据,这样做是因为HadoopStreaming会帮我们办好其他事。这是真的,别不相信!

    Map: mapper.py


    将下列的代码保存在/home/hadoop/mapper.py中,他将从STDIN读取数据并将单词成行分隔开,生成一个列表映射单词与发生次数的关系:
    注意:要确保这个脚本有足够权限(chmod +x /home/hadoop/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)

    在这个脚本中,并不计算出单词出现的总数,它将输出 "<word> 1" 迅速地,尽管<word>可能会在输入中出现多次,计算是留给后来的Reduce步骤(或叫做程序)来实现。当然你可以改变下编码风格,完全尊重你的习惯。

    Reduce: reducer.py


    将代码存储在/home/hadoop/reducer.py 中,这个脚本的作用是从mapper.py 的STDIN中读取结果,然后计算每个单词出现次数的总和,并输出结果到STDOUT。
    同样,要注意脚本权限:chmod +x /home/hadoop/reducer.py

    #!/usr/bin/env python
     
    from operator import itemgetter
    import sys
     
    # maps words to their counts
    word2count = {}
     
    # 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)
            word2count[word] = word2count.get(word, 0) + count
        except ValueError:
            # count was not a number, so silently
            # ignore/discard this line
            pass
     
    # sort the words lexigraphically;
    #
    # this step is NOT required, we just do it so that our
    # final output will look more like the official Hadoop
    # word count examples
    sorted_word2count = sorted(word2count.items(), key=itemgetter(0))
     
    # write the results to STDOUT (standard output)
    for word, count in sorted_word2count:
        print '%s\\t%s'% (word, count)


    测试你的代码(cat data | map | sort | reduce)


    我建议你在运行MapReduce job测试前尝试手工测试你的mapper.py 和 reducer.py脚本,以免得不到任何返回结果
    这里有一些建议,关于如何测试你的Map和Reduce的功能:

    ——————————————————————————————————————————————

    \r\n

     # very basic test
     hadoop@ubuntu:~$ echo "foo foo quux labs foo bar quux" | /home/hadoop/mapper.py
     foo     1
     foo     1
     quux    1
     labs    1
     foo     1
     bar     1
    ——————————————————————————————————————————————
     hadoop@ubuntu:~$ echo "foo foo quux labs foo bar quux" | /home/hadoop/mapper.py | sort | /home/hadoop/reducer.py
     bar     1
     foo     3
     labs    1
    ——————————————————————————————————————————————
    
     # using on[object Object]e of the ebooks as example input
     # (see below on where to get the ebooks)
     hadoop@ubuntu:~$ cat /tmp/gutenberg/20417-8.txt | /home/hadoop/mapper.py
     The     1
     Project 1
     Gutenberg       1
     EBook   1
     of      1
     [...] 
     (you get the idea)
    
     quux    2
    
     quux    1


    ——————————————————————————————————————————————
    
    
    
    为了这个例子,我们将需要三种电子书:

    下载他们,并使用us-ascii编码存储 解压后的文件,保存在临时目录,比如/tmp/gutenberg.

     hadoop@ubuntu:~$ ls -l /tmp/gutenberg/
     total 3592
     -rw-r--r-- 1 hadoop hadoop  674425 2007-01-22 12:56 20417-8.txt
     -rw-r--r-- 1 hadoop hadoop 1423808 2006-08-03 16:36 7ldvc10.txt
     -rw-r--r-- 1 hadoop hadoop 1561677 2004-11-26 09:48 ulyss12.txt
     hadoop@ubuntu:~$
    在我们运行MapReduce job 前,我们需要将本地的文件复制到HDFS中:
    
     hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop dfs -copyFromLocal /tmp/gutenberg gutenberg
     hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop dfs -ls
     Found 1 items
     /user/hadoop/gutenberg  <dir>
     hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop dfs -ls gutenberg
     Found 3 items
     /user/hadoop/gutenberg/20417-8.txt      <r 1>   674425
     /user/hadoop/gutenberg/7ldvc10.txt      <r 1>   1423808
     /user/hadoop/gutenberg/ulyss12.txt      <r 1>   1561677
    
    
    
    现在,一切准备就绪,我们将在运行Python MapReduce job 在Hadoop集群上。像我上面所说的,我们使用的是
     帮助我们传递数据在Map和Reduce间并通过STDIN和STDOUT,进行标准化输入输出。
    
     
     hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop jar contrib/streaming/hadoop-0.19.1-streaming.jar
     -mapper /home/hadoop/mapper.py -reducer /home/hadoop/reducer.py -input gutenberg/* 
    -output gutenberg-output
    在运行中,如果你想更改Hadoop的一些设置,如增加Reduce任务的数量,你可以使用“-hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop jar contrib/streaming/hadoop-0.19.1-streaming.jar 
     -mapper ...
    
    一个重要的备忘是关于 
    这个任务将会读取HDFS目录下的HDFS目录下的
    目录。
    之前执行的结果如下:
    
    hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop jar contrib/streaming/hadoop-0.19.1-streaming.jar 
    -mapper /home/hadoop/mapper.py -reducer /home/hadoop/reducer.py -input gutenberg/* 
    -output gutenberg-output
     
    additionalConfSpec_:null
     null=@@@userJobConfProps_.get(stream.shipped.hadoopstreaming
     packageJobJar: [/usr/local/hadoop-datastore/hadoop-hadoop/hadoop-unjar54543/]
     [] /tmp/streamjob54544.jar tmpDir=null
     [...] INFO mapred.FileInputFormat: Total input paths to process : 7
     [...] INFO streaming.StreamJob: getLocalDirs(): [/usr/local/hadoop-datastore/hadoop-hadoop/mapred/local]
     [...] INFO streaming.StreamJob: Running job: job_200803031615_0021
     [...]
     [...] INFO streaming.StreamJob:  map 0%  reduce 0%
     [...] INFO streaming.StreamJob:  map 43%  reduce 0%
     [...] INFO streaming.StreamJob:  map 86%  reduce 0%
     [...] INFO streaming.StreamJob:  map 100%  reduce 0%
     [...] INFO streaming.StreamJob:  map 100%  reduce 33%
     [...] INFO streaming.StreamJob:  map 100%  reduce 70%
     [...] INFO streaming.StreamJob:  map 100%  reduce 77%
     [...] INFO streaming.StreamJob:  map 100%  reduce 100%
     [...] INFO streaming.StreamJob: Job complete: job_200803031615_0021
    
    
     [...] INFO streaming.StreamJob: Output: gutenberg-output  hadoop@ubuntu:/usr/local/hadoop$ 
    
    
    正如你所见到的上面的输出结果,Hadoop 同时还提供了一个基本的WEB接口显示统计结果和信息。
    当Hadoop集群在执行时,你可以使用浏览器访问   ,如图:
    
    
    
    
    检查结果是否输出并存储在HDFS目录下的中:
    
     hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop dfs -ls gutenberg-output
     Found 1 items
     /user/hadoop/gutenberg-output/part-00000     <r 1>   903193  2007-09-21 13:00
     hadoop@ubuntu:/usr/local/hadoop$ 
    
    可以使用 命令检查文件目录
    
     hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop dfs -cat gutenberg-output/part-00000
     "(Lo)cra"       1
     "1490   1
     "1498," 1
     "35"    1
     "40,"   1
     "A      2
     "AS-IS".        2
     "A_     1
     "Absoluti       1
     [...]
     hadoop@ubuntu:/usr/local/hadoop$
    
    注意比输出,上面结果的(")符号不是Hadoop插入的。
    
    
     
    
     请参考:
    
    
    http://www.michael-noll.com/wiki/Writing_An_Hadoop_MapReduce_Program_In_Python#What_we_want_to_do



转载于:https://my.oschina.net/sanpeterguo/blog/215922

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用Python实现Hadoop MapReduce程序 的相关文章

随机推荐

  • CIDR概述及其地址块计算

    CIDR概述 英文 xff1a Classless Inter Domain Routing xff0c 中文是 xff1a 无分类域间路由选择 一般叫做无分类编址 设计目的 xff1a 解决路由表项目过多过大的问题 表示法 xff1a l
  • Python基础之元组

    元组初识 元组的认识 首先 xff0c 我们来认识一下元组 xff1a 定义一个元组 uesr tuple 61 34 zhangsan 34 34 lisi 34 34 wangwu 34 定义一个空元组 empty tuple 61 元
  • 函数声明后面的const用法

    void function const 通常我们会看到一些函数声明后面会跟着一个const xff0c 这个const是做什么的呢 xff1f 看一下下面的例子 xff0c 就知道了 直接在编译前 xff0c 就会提示下面的两个错误 tes
  • 修复移动硬盘"文件或目录损坏且无法读取"

    今天使用移动硬盘的时候强制拔掉了数据线 xff0c 再此连上之后发现原来的F G H三个盘的盘符都在 xff0c 但是F盘只有盘符 xff0c 双击之后提示 34 文件或目录损坏且无法读取 34 而这个盘有我70G的数据 于是上网查资料 x
  • 维基百科的网址(没被墙)

    https en wikipedia org wiki Main Page
  • 初学者计算机_初学者极客:更改笔记本计算机盒盖时Windows的功能

    初学者计算机 Mihai Simonia Shutterstock com Mihai Simonia Shutterstock com Are you tired of your laptop automatically going to
  • 想要恢复回收站误删文件,就用EasyRecovery!

    不知道大家在日常工作中遇到回收站误删文件的情况吗 xff1f 遇到这样的情况 xff0c 不要慌张 xff0c 可以借助专业的数据恢复软件来处理 EasyRecovery软件是由全球著名数据厂商Kroll Ontrack出品的数据恢复软件
  • ubuntu 设置root用户密码并实现root用户登录

    一 xff1a 设置root用户密码 在ubuntu中root用户的密码是随机的 xff0c 所以需要我们自己起设置root用户的密码 在终端命令行中执行 sudo passwd 这时候会提示你输入当前用户密码 xff0c 输入成功之后 x
  • electron制作聊天界面(仿制qq)

    效果图 样式使用scss和flex布局 这也是制作IM系统的最后一个界面了 在制作之前参考了qq和千牛 需要注意的点 qq将滚动条美化了 而且在无操作的情况下是不会显示的 滚动条美化 webkit scrollbar 滚动条整体样式 wid
  • element-ui中的el-table滚动加载事件

    问题描述 xff1a 当表格数据量过多 xff0c 一次请求回来会很卡 xff0c 同时又不想分页的情况下 xff0c 我们想让鼠标滚动到表格底部时再去请求数据 解决思路 xff1a 项目用的是element ui的框架 xff0c 给el
  • OpenWRT配置IPV6

    准备材料 智博通 WG3526 路由器 MT7621A 16M ROM 512M RAM 中国移动光纤入户 Prefix Delegation前缀委托模式 刷机 OpenWRT 18 06 for ZBT WG3526 配置 etc con
  • chrome浏览器去掉打开新标签的常用地址缩略图

    chrome浏览器是我们最常用的浏览器 xff0c 但是打开标签后会显示历史的缩略图 有时别人借用我们的电脑 xff0c 或者开着电脑演示时 xff0c 这些浏览记录就会被展示出来 xff0c 总是感觉怪怪的 谷歌一番 xff0c 发现了关
  • cisco交换机如何查看CPU和内存使用情况,以及如何查看接口数据量

    switch4006 show processes cpu CPU utilization for five seconds 4 0 one minute 4 five minutes 4 PID Runtime ms Invoked uS
  • Remix OS PC硬盘版的安装方法。

    前言 大家好 xff0c 今天由我 xff0c 功能讨论区版主来给大家介绍一下Remix OS PC硬盘版的安装方法 开始之前大家需要明确几点 xff1a 0 你的电脑需要满足如下要求 xff08 仔细看清楚这个列表 xff0c 缺一不可
  • inline-block在360浏览器中的显示问题

    360浏览器不支持inline block效果 xff0c 在样式表中加入 display inline block zoom 1 display inline 就能达到display inline block的效果了 转载于 https
  • 个人团队贡献分+转会人员

    经过我们的商议 xff0c 个人团队贡献分如下分配 xff1a 黄杨 xff1a 33 王安然 xff1a 32 韩佳胤 xff1a 31 刘俊伟 xff1a 28 林璐 xff1a 29 谢伯炎 xff1a 30 谭传奇 xff1a 27
  • 如何在bash shell命令行中非常有效地搜索历史命令?

    How to search history commands very effectively in bash shell command line 如何在bash shell 命令行中非常有效地搜索历史命令 xff1f Just ente
  • GreenPlum 锁表以及解除锁定

    最近遇到truncate表 xff0c 无法清理的情况 xff0c 在master节点查看加锁情况 xff0c 并未加锁 这种情况极有可能是segment节点相关表加了锁 xff0c 所以遇到这种情况除了排查master节点的锁 xff0c
  • 使用 FreeRTOS 时注意事项总结(基础篇教程完结)

    以下转载自安富莱电子 xff1a http forum armfly com forum php FreeRTOS 的初始化流程 推荐的初始化流程如下 xff0c 本教程配套的所有例子都是采用的这种形式 xff0c 当然 xff0c 不限制
  • 使用Python实现Hadoop MapReduce程序

    为什么80 的码农都做不了架构师 xff1f gt gt gt 笔者的机器运行效果如下 xff08 输入数据是find的帮助手册 xff0c 和笔者预期一样 xff0c the是最多的 xff09 xff1a 以下是原帖 在这个实例中 xf