python + hadoop (案例)
python如何链接hadoop,并且使用hadoop的资源,这篇文章介绍了一个简单的案例!
一、python的map/reduce代码
首先认为大家已经对haoop已经有了很多的了解,那么需要建立mapper和reducer,分别代码如下:
1、mapper.py
#!/usr/bin/env python
import sys
for line in sys.stdin:
line = line.strip()
words = line.split()
for word in words:
print '%s\t%s' %(word, 1)
2、reducer.py
#!/usr/bin/env python
from operator import itemgetter
import sys current_word = None
current_count = 0
word = None for line in sys.stdin:
words = line.strip()
word, count = words.split('\t') try:
count = int(count)
except ValueError:
continue if current_word == word:
current_count += count
else:
if current_word:
print '%s\t%s' %(current_word, current_count)
current_count = count
current_word = word if current_word == word:
print '%s\t%s' %(current_word, current_count)
建立了两个代码之后,测试一下:
[qiu.li@l-tdata5.tkt.cn6 /export/python]$ echo "I like python hadoop , hadoop very good" | ./mapper.py | sort -k , | ./reducer.py
,
good
hadoop
I
like
python
very
二、上传文件
发现没啥问题,那么成功一半了,下面上传几个文件到hadoop做进一步测试。我在线上找了几个文件,命令如下:
wget http://www.gutenberg.org/ebooks/20417.txt.utf-8
wget http://www.gutenberg.org/files/5000/5000-8.txt
wget http://www.gutenberg.org/ebooks/4300.txt.utf-8
查看下载的文件:
[qiu.li@l-tdata5.tkt.cn6 /export/python]$ ls
.txt.utf- .txt.utf- -.txt mapper.py reducer.py run.sh
上传文件到hadoop上面,命令如下:hadoop dfs -put ./*.txt /user/ticketdev/tmp (hadoop是配置好的,目录也是建立好的)
建立run.sh
hadoop jar $STREAM \
-files ./mapper.py,./reducer.py \
-mapper ./mapper.py \
-reducer ./reducer.py \
-input /user/ticketdev/tmp/*.txt \
-output /user/ticketdev/tmp/output
查看结果:
[qiu.li@l-tdata5.tkt.cn6 /export/python]$ hadoop dfs -cat /user/ticketdev/tmp/output/part- | sort -nk | tail
DEPRECATED: Use of this script to execute hdfs command is deprecated.
Instead use the hdfs command for it. it
which
that
a
is
to
in
and
of
the
三、参考文献:
http://www.cnblogs.com/wing1995/p/hadoop.html?utm_source=tuicool&utm_medium=referral
python + hadoop (案例)的更多相关文章
- ArcGIS Python编程案例-电子资料链接
ArcGIS Python编程案例(1)-Python语言基础 https://www.jianshu.com/p/dd90816d019b ArcGIS Python编程案例(2)-使用ArcPy编 ...
- 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍,样例程序与本地调试
相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(二) -- 在集群上运行与监控 用python + hadoop streami ...
- 用python + hadoop streaming 编写分布式程序(二) -- 在集群上运行与监控
写在前面 相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍,样例程序与本地调试 用python + hado ...
- 用python + hadoop streaming 编写分布式程序(三) -- 自定义功能
又是期末又是实训TA的事耽搁了好久……先把写好的放上博客吧 相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍 ...
- Python入门(案例)
Python入门(案例) #一.上课案例: #输出hello wordprint('hello word') #python注释有两种#1.单行注释#这是单行注释#2.多行注释'''这是多行注释''' ...
- Python 小案例实战 —— 简易银行存取款查询系统
Python 小案例实战 -- 简易银行存取款查询系统 涉及知识点 包的调用 字典.列表的混合运用 列表元素索引.追加 基本的循环与分支结构 源码 import sys import time ban ...
- python项目案例
python项目案例1:----此学习案例用python3编写,摘自明日科技,感谢! 学生管理系统: 功能描述:具有增删改查,排序,保存并显示学生的全部信息. 1.主界面---函数menu(),显示功 ...
- python - hadoop,mapreduce demo
Hadoop,mapreduce 介绍 59888745@qq.com 大数据工程师是在Linux系统下搭建Hadoop生态系统(cloudera是最大的输出者类似于Linux的红帽), 把用户的交易 ...
- Python学习案例之Web版语音合成播报
前言 语音合成技术能将用户输入的文字,转换成流畅自然的语音输出,并且可以支持语速.音调.音量设置,打破传统文字式人机交互的方式,让人机沟通更自然. 应用场景 将游戏场景中的公告.任务或派单信息通过语音 ...
随机推荐
- Orleans中的Timer和Reminder
Timers and Reminder 定时器和提醒器 Orleans runtime 允许开发人员通过一种叫做timer和另一种叫做reminder的机制为grain添加周期性行为.接下来我分别为大 ...
- 跟我一起学WCF(13)——WCF系列总结
引言 WCF是微软为了实现SOA的框架,它是对微乳之前多种分布式技术的继承和扩展,这些技术包括Enterprise Service..NET Remoting.XML Web Service.MSMQ ...
- [ACM_图论] Fire Net (ZOJ 1002 带障碍棋盘布炮,互不攻击最大数量)
Suppose that we have a square city with straight streets. A map of a city is a square board with n ...
- [jQuery学习系列三 ]3-JQuery学习二-字典操作
前言:如果看过了第一篇和第二篇, 相信大家会对jQuery有个初步的认识了, 对于jQuery的选择器和数组的操作都已经很熟悉了, 这一篇就单独罗列jQuery中字典的操作相关的内容. 1. 数组中添 ...
- Atitit.常用的gc算法
Atitit.常用的gc算法 1.1. 记-清除算法1 1.2. 复制算法1 1.3. 标记-整理算法2 1.4. 分代收集算法2 1.1. 记-清除算法 最基础的收集算法,算法分为标记和清除两个阶段 ...
- iOS应用性能调优的25个建议和技巧
本文来自iOS Tutorial Team 的 Marcelo Fabri,他是Movile的一名 iOS 程序员.这是他的个人网站:http://www.marcelofabri.com/,你还可以 ...
- Python核心编程笔记(类)
Python并不强求你以面向对象的方式编程(与Java不同) # coding=utf8 class FooClass(object): version = 0.1 def __init__(self ...
- 轻松了解Spring中的控制反转和依赖注入(二)
紧接上一篇文章<轻松了解Spring中的控制反转和依赖注入>讲解了SpringIOC和DI的基本概念,这篇文章我们模拟一下SpringIOC的工作机制,使我们更加深刻的理解其中的工作. 类 ...
- javaweb回顾第四篇Servlet异常处理
前言:很多网站为了给用户很好的用户体验性,都会提供比较友好的异常界面,现在我们在来回顾一下Servlet中如何进行异常处理的. 1:声明式异常处理 什么是声明式:就是在web.xml中声明对各种异常的 ...
- 代码大全 MSIL语言程序设计
.NET平台的编译器会将高级语言(C#,VB.NET,F#)编译成MSIL(微软中间语言)格式.熟悉MSIL语言,可以读懂一些加密程序混淆过的算法,这些算法几乎不能还原成高级语言,但是可以还原成MSI ...