Azure对于学生账户有260刀的免费试用,火急火燎地创建Hadoop Cluster!本例子是使用Hadoop MapReduce来统计一本电子书中各个单词的出现个数.

Let's get hands dirty!

首先,我们在Azure中创建了一个Cluster,并且使用putty Ssh访问了该集群,ls一下:

在cluster上创建一个/home/hduser/文件夹

OK,接下来在本地创建一个mapper.py文件和reducer.py文件,注意权限:chmod +x reducer.py(mapper.py)

mapper.py代码

  1. #!/usr/bin/env python
  2. """mapper.py"""
  3.  
  4. import sys
  5.  
  6. # input comes from STDIN (standard input)
  7. for line in sys.stdin:
  8. # remove leading and trailing whitespace
  9. line = line.strip()
  10. # split the line into words
  11. words = line.split()
  12. # increase counters
  13. for word in words:
  14. # write the results to STDOUT (standard output);
  15. # what we output here will be the input for the
  16. # Reduce step, i.e. the input for reducer.py
  17. #
  18. # tab-delimited; the trivial word count is 1
  19. print '%s\t%s' % (word, 1)

reducer.py代码

  1. #!/usr/bin/env python
  2. """reducer.py"""
  3.  
  4. from operator import itemgetter
  5. import sys
  6.  
  7. current_word = None
  8. current_count = 0
  9. word = None
  10.  
  11. # input comes from STDIN
  12. for line in sys.stdin:
  13. # remove leading and trailing whitespace
  14. line = line.strip()
  15.  
  16. # parse the input we got from mapper.py
  17. word, count = line.split('\t', 1)
  18.  
  19. # convert count (currently a string) to int
  20. try:
  21. count = int(count)
  22. except ValueError:
  23. # count was not a number, so silently
  24. # ignore/discard this line
  25. continue
  26.  
  27. # this IF-switch only works because Hadoop sorts map output
  28. # by key (here: word) before it is passed to the reducer
  29. if current_word == word:
  30. current_count += count
  31. else:
  32. if current_word:
  33. # write result to STDOUT
  34. print '%s\t%s' % (current_word, current_count)
  35. current_count = count
  36. current_word = word
  37.  
  38. # do not forget to output the last word if needed!
  39. if current_word == word:
  40. print '%s\t%s' % (current_word, current_count)

本地mapper.py测试: echo "foo foo quux labs foo bar quux" | ./mapper.py

并复制到Cluster上:

本例中,我们使用一本电子书,地址是http://www.gutenberg.org/cache/epub/20417/pg20417.txt,直接在linux客户端下载后,上传到cluster中

OK,万事俱备,运行MapReduce

查看输出文件前20行

相关参考文章:

http://www.michael-noll.com/tutorials/writing-an-hadoop-mapreduce-program-in-python/

https://docs.microsoft.com/en-us/azure/hdinsight/hadoop/apache-hadoop-streaming-python

http://hadooptutorial.info/hdfs-file-system-commands/

Hadoop: 在Azure Cluster上使用MapReduce的更多相关文章

  1. Hadoop 系列文章(三) 配置部署启动YARN及在YARN上运行MapReduce程序

    这篇文章里我们将用配置 YARN,在 YARN 上运行 MapReduce. 1.修改 yarn-env.sh 环境变量里的 JAVA_HOME 路径 [bamboo@hadoop-senior ha ...

  2. 【Cloud Computing】Hadoop环境安装、基本命令及MapReduce字数统计程序

    [Cloud Computing]Hadoop环境安装.基本命令及MapReduce字数统计程序 1.虚拟机准备 1.1 模板机器配置 1.1.1 主机配置 IP地址:在学校校园网Wifi下连接下 V ...

  3. 如何在cluster上跑R脚本

    R 是一个比较不错但是有时候操蛋的语言,不错是因为用着爽的时候真的很爽,操蛋是因为这种爽不是什么时候都可以的,比如说在cluster上批处理跑R脚本. 当然说这话有些在上面跑过的各种不服气,你丫傻逼吧 ...

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

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

  5. Azure 网站上的 Java

     编辑人员注释:本文章由Windows Azure 网站团队的项目经理Chris Compy 撰写. Microsoft 已推出针对 Azure 网站上基于 Java 的网站的支持.此功能旨在通过 ...

  6. 在 Azure 网站上使用 Memcached 改进 WordPress

    编辑人员注释:本文章由 Windows Azure 网站团队的项目经理 Sunitha Muthukrishna 和 Windows Azure 网站开发人员体验合作伙伴共同撰写. 您是否希望改善在 ...

  7. Windows Azure 网站上的 WebSocket 简介

    编辑人员注释:本文章由 Windows Azure 网站团队的首席项目经理 Stefan Schackow 撰写. Windows Azure 网站最近新增了对 WebSocket 协议的支持..NE ...

  8. 删除 Windows Azure 网站上的标准服务器头

    编辑人员注释: 本文章由 Windows Azure 网站团队的项目经理 Erez Benari 撰写. 请求和响应中包含的 HTTP 头是Web 服务器和浏览器之间的 HTTP 通信过程的一部分.例 ...

  9. 在 Windows Azure 网站上使用 Django、Python 和 MySQL:创建博客应用程序

    编辑人员注释:本文章由 Windows Azure 网站团队的项目经理 Sunitha Muthukrishna 撰写. 根据您编写的应用程序,Windows Azure 网站上的基本Python 堆 ...

随机推荐

  1. 生日蛋糕 (poj1190) (dfs剪枝)

    [题目描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1 <= i <= M)层蛋糕是半径为Ri, 高度为 ...

  2. -webkit-overflow-scrolling:touch介绍和碰到的坑

    1.作用 可控制元素在移动设备有滚动回弹效果,可惯性滚动 2.适应场景 在ios移动端上,设置容器overflow-y:scroll;使容器内元素滚动时,滑动会很卡顿,使用-webkit-overfl ...

  3. sweetalert插件替代alert/confirm

    更多关于SweetAlert的内容请参考:https://github.com/t4t5/sweetalert.

  4. openSSH学习笔记(一)

    OpenSSH 是 SSH (Secure SHell) 协议的免费开源实现.SSH协议族可以用来进行远程控制, 或在计算机之间传送文件.而实现此功能的传统方式,如telnet(终端仿真协议). rc ...

  5. vue.js(18)--父组件向子组件传值

    子组件是不能直接使用父组件中数据的,需要进行属性绑定(v-bind:自定义属性名=“msg”),绑定后需要在子组件中使用props[‘自定义属性名’]数组来定义父组件的自定义名称. props数组中的 ...

  6. 基于 SwiftUI 创建一个可删除、可添加列表项的列表

    执行环境 macOS Mojave: 10.14.5 xcode: Version 11.0 beta 6 (11M392q) 预览效果 完整代码 import SwiftUI class Item: ...

  7. VS #include 【bits/bstdc++.h】出错

    目录 1. 本文地址 2. 按 3. 操作步骤 1. 本文地址 博客园:https://www.cnblogs.com/coco56/p/11163142.html 简书:https://www.ji ...

  8. Jupyter配置工作路径

    在修改之前,C:\Users\Administrator\ .jupyter 目录下面只有一个“migrated”文件. 打开命令窗口(运行->cmd),进入python的Script目录下输入 ...

  9. java类实现序列化的方法?collection框架中实现什么样的接口

  10. 进程间的mutex

    设两个进程共用一个临界资源的互斥信号量mutex=1,当mutex=-1时表示(). 一个进程进入了临界区,另一个进程等待 没有一个进程进入临界区 两个进程都进入临界区 两个进程都在等待 互斥信号量不 ...