Streaming(C++)实现WordCount
2015-12-16 15:23
711 查看
Streaming 提供来Mapreduce的API,允许我们用非JAVA语言编写map和reduce函数,这是我第一次使用Streaming,也是第一次在linux写shell脚本,值得记录一下(2015/12/16)!
我以前习惯用C++写程序,所以这里用C++实现map和reduce。
先介绍下使用Streaming实现的步骤:
1、写map和reduce函数
2、在linux下测试map和reduce函数
3、用hadoop实现
步骤一:
//mapper function
#include<string>
#include<iostream>
using namespace std;
int main()
{
string str;
while(cin>>str)
{
cout<<str<<"\t"<<1<<endl;
}
return 0;
}
//reducer function
#include<iostream>
#include<string>
using namespace std;
int main()
{
string key1,value,key2;
int cnt = 1;
cin>>key1>>value;
while(cin>>key2>>value)
{
if(key1 == key2)
{
cnt++;
}
else
{
cout<<key1<<"\t"<<cnt<<endl;
cnt = 1;
key1 = key2;
}
}
cout<<key2<<"\t"<<cnt<<endl;
return 0;
}
步骤2和3放在wordcount.sh中
#!/bin/bash
#在linux下测试
:<<cc
g++ ./map.cpp -o map
g++ ./reduce.cpp -o reduce
cat /home/hadoop/input/wordcount/wordcount.txt | ./map | sort | ./reduce
cc
#在hadoop下执行
hadoop fs -rmr /user/hadoop/output
hadoop jar $HADOOP_HOME/contrib/streaming/hadoop-streaming-1.2.1.jar \
-input /user/hadoop/input/wordcount/ \
-output /user/hadoop/output/wordcount \
-mapper /home/hadoop/c++/streaming/map \
-reducer /home/hadoop/c++/streaming/reduce \
-file /home/hadoop/c++/streaming/map \
-file /home/hadoop/c++/streaming/reduce
我以前习惯用C++写程序,所以这里用C++实现map和reduce。
先介绍下使用Streaming实现的步骤:
1、写map和reduce函数
2、在linux下测试map和reduce函数
3、用hadoop实现
步骤一:
//mapper function
#include<string>
#include<iostream>
using namespace std;
int main()
{
string str;
while(cin>>str)
{
cout<<str<<"\t"<<1<<endl;
}
return 0;
}
//reducer function
#include<iostream>
#include<string>
using namespace std;
int main()
{
string key1,value,key2;
int cnt = 1;
cin>>key1>>value;
while(cin>>key2>>value)
{
if(key1 == key2)
{
cnt++;
}
else
{
cout<<key1<<"\t"<<cnt<<endl;
cnt = 1;
key1 = key2;
}
}
cout<<key2<<"\t"<<cnt<<endl;
return 0;
}
步骤2和3放在wordcount.sh中
#!/bin/bash
#在linux下测试
:<<cc
g++ ./map.cpp -o map
g++ ./reduce.cpp -o reduce
cat /home/hadoop/input/wordcount/wordcount.txt | ./map | sort | ./reduce
cc
#在hadoop下执行
hadoop fs -rmr /user/hadoop/output
hadoop jar $HADOOP_HOME/contrib/streaming/hadoop-streaming-1.2.1.jar \
-input /user/hadoop/input/wordcount/ \
-output /user/hadoop/output/wordcount \
-mapper /home/hadoop/c++/streaming/map \
-reducer /home/hadoop/c++/streaming/reduce \
-file /home/hadoop/c++/streaming/map \
-file /home/hadoop/c++/streaming/reduce
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性
- C++ Custom Control控件向父窗体发送对应的消息