Streaming(C++)实现WordCount

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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值