博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
理解MapReduce计算构架
阅读量:4449 次
发布时间:2019-06-07

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

1.编写map函数,reduce函数

(1)创建mapper.py文件

cd /home/hadoop/wc 

gedit mapper.p

 

(2)mapper函数

 

#!/usr/bin/env pythonimport sysfor i in stdin:    i = i.strip()    words = i.split()    for word in words:    print '%s\t%s' % (word,1) (3)reducer.py文件创建

cd /home/hadoop/wc

gedit reducer.py

(4)reducer函数
#!/usr/bin/env pythonfrom operator import itemgetterimport syscurrent_word = Nonecurrent_count = 0word = Nonefor i in stdin:    i = i.strip()    word, count = i.split('\t',1)    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 = wordif current_word == word:    print '%s\t%s' % (current_word, current_count) 2.将其权限作出相应修改
chmod a+x /home/hadoop/mapper.py
echo "foo foo quux labs foo bar quux" | /home/hadoop/wc/mapper.py
echo "foo foo quux labs foo bar quux" | /home/hadoop/wc/mapper.py | sort -k1,1 | /home/hadoop/wc/reducer.p 3.本机上测试运行代码

放到HDFS上运行

下载并上传文件到hdfs上

cd  /home/hadoop/wcwget http://www.gutenber.org/files/5000/5000-8.txtwget http://www.gutenber.org/cache/epub/20417/pg20417.txt
cd /usr/hadoop/wchdfs dfs -put /home/hadoop/hadoop/gutenberg/*.txt /user/hadoop/input

 

 

转载于:https://www.cnblogs.com/tyx123/p/9026182.html

你可能感兴趣的文章
svn的分支
查看>>
爬虫必备—性能相关(异步非阻塞)
查看>>
H5 65-清除浮动方式一
查看>>
四旋翼姿态解算——基础理论及推导
查看>>
通过用户模型,对数据库进行增删改查操作
查看>>
redis安装使用配置
查看>>
C++数据文件存储与加载(利用opencv)
查看>>
[TensorFlow 2] [Keras] fit()、fit_generator() 和 train_on_batch() 分析与应用
查看>>
Java 编译错误:缺少返回语句
查看>>
leanote使用本地账户时,去掉待同步的小红点
查看>>
整理pandas操作
查看>>
REST-assured 2发送消息代码重构
查看>>
python-面向对象
查看>>
11 go并发编程-上
查看>>
操作DOM树
查看>>
WPF——TargetNullValue(如何在绑定空值显示默认字符)
查看>>
巧用五招提升Discuz!X运行速度
查看>>
01构建之法阅读笔记
查看>>
mac svn命令 linux同样适用
查看>>
jQuery,ctrl+enter组合事件
查看>>