博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python实现Mapreduce的wordcount
阅读量:3915 次
发布时间:2019-05-23

本文共 3526 字,大约阅读时间需要 11 分钟。

文章目录

介绍

  • Hadoop作为Apache的基金项目,解决的大数据处理时间长的问题,其中MapReduce并行处理框架作为Hadoop中重要的成员。由于Hadoop的架构实现是由JAVA实现的,所以在进行大数据处理时,JAVA程序用的较多,但是,想要把深度学习算法用到MapReduce中,Python是深度学习和数据挖掘处理数据较为容易的语言,所以基于以上考虑,本文介绍了使用python实现MapReduce中的WordCount实验,文章内容(代码部分)来自于某一博主CSDN博客,参考链接在最后。

Hadoop Stream

主要使用的Hadoop提供的Hadoop Streaming,首先,介绍一下Hadoop Stream

Streaming 的作用

  • Hadoop Streaming框架,最大的好处是,让任何语言编写的map, reduce程序能够在hadoop集群上运行;map/reduce程序只要遵循从标准输入stdin读,写出到标准输出stdout即可;
  • 其次,容易进行单机调试,通过管道前后相接的方式就可以模拟streaming, 在本地完成map/reduce程序的调试
    #cat inputfile | mapper | sort | reducer > output
  • 最后,streaming框架还提供了作业提交时的丰富参数控制,直接通过streaming参数,而不需要使用java语言修改;很多mapreduce的高阶功能,都可以通过steaming参数的调整来完成。

Streaming 的局限

Streaming默认只能处理文本数据Textfile,对于二进制数据,比较好的方法是将二进制的key, value进行base64编码,转化为文本;

Mapper和reducer的前后都要进行标准输入和标准输出的转化,涉及数据拷贝和解析,带来了一定的开销。

Streaming 命令的相关参数

# hadoop jar hadoop-streaming-2.6.5.jar \ [普通选项] [Streaming选项]

普通选项和Stream选项可以参考如下网址:

Python实现MapReduce的WordCount

  1. 首先,编写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)

在这个脚本中,并不计算出单词出现的总数,它将输出 " 1" 迅速地,尽管可能会在输入中出现多次,计算是留给后来的Reduce步骤(或叫做程序)来实现。记住为mapper.py赋予可执行权限:chmod 777

  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)

将代码存储在/usr/local/hadoop/reducer.py 中, 的STDIN中读取结果,然后计算每个单词出现次数的总和,并输出结果到STDOUT。

同样,要注意脚本权限:chmod 777

  1. 建议在运行MapReduce任务的时候测试一下脚本运行效果正确:
root@localhost:/root/pythonHadoop$ echo "foo foo quux labs foo bar quux" | ./mapper.py  foo      1  foo      1  quux     1  labs     1  foo      1  bar      1  quux     1  root@localhost:/root/pythonHadoop$ echo "foo foo quux labs foo bar quux" |./mapper.py | sort |./reducer.py  bar     1  foo     3  labs    1  quux    2

如果执行效果如上,则证明可行。可以运行MapReduce了。

  1. 在Hadoop平台运行python脚本:
[root@node01 pythonHadoop]         hadoop jar contrib/hadoop-streaming-2.6.5.jar    -mapper mapper.py    -file mapper.py    -reducer reducer.py    -file reducer.py    -input /ooxx/*   -output /ooxx/output/
  1. 最后执行 hdfs dfs -cat /ooxx/output/part-00000进行输出结果的查看。
    结果就不展示了,对于hello.txt文件可以自己用echo 制作,也可以从网上自行下载测试文件,对于测试结果,不同数据集结果不尽相同。

参考文章:

转载地址:http://qscrn.baihongyu.com/

你可能感兴趣的文章
从业务需求抽象成模型解决方案
查看>>
Kafka
查看>>
Magicodes.IE 2.2发布
查看>>
应用交付老兵眼中的Envoy, 云原生时代下的思考
查看>>
.NET 开源项目 StreamJsonRpc 介绍[上篇]
查看>>
.NET Core微服务开发选项
查看>>
探讨NET Core数据进行3DES加密或解密弱密钥问题
查看>>
Vue 3拖更,尤雨溪介绍最新进展
查看>>
如何利用.NETCore向Azure EventHubs准实时批量发送数据?
查看>>
WPF 框架全构建环境虚拟机硬盘分享
查看>>
ABP框架 v3.0 已发布!
查看>>
使用.Net Core实现的一个图形验证码
查看>>
.NET 开源项目 StreamJsonRpc 介绍[中篇]
查看>>
Blazor带我重玩前端(三)
查看>>
基于.NetCore3.1系列 —— 认证授权方案之授权揭秘 (下篇)
查看>>
实现业务数据的同步迁移 · 思路一
查看>>
龙芯开源社区上线.NET主页
查看>>
eShopOnContainers 知多少[11]:服务间通信之gRPC
查看>>
闲谈设计模式
查看>>
平台or职位,你怎么选?
查看>>