Hadoop Streaming: c++编写uniq程序

1. 描述
使用Hadoop streaming对输入数据的第一列进行uniq去重计算,采用c++编写map-reduce程序。

2. mapper程序
mapper.cpp

#include <iostream>
#include <string>

using namespace std;

#define BUFFER_SIZE 102400
#define DELIM "\t"

int main(int argc, char** argv)
{
        char buf[BUFFER_SIZE];
        char *strCur = NULL;

        //读取第一个字段并输出
        while(fgets(buf, BUFFER_SIZE-1, stdin))
        {
                strCur = strtok(buf, DELIM);
                cout << buf << endl;
        }


        return 0;
}

3. reducer
reducer.cpp
#include <string>
#include <set>
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
        string key;

        set<string> myset;

        while(cin >> key)
        {
                myset.insert(key);
        }

        cout << myset.size() << endl;

        return 0;
}

4. 测试数据
test.txt

a       15      1
a       15      1
a       15      1
a       15      1
b       20      1
c       15      1
c       15      1
d       16      1
a       16      1

5. 本地测试
$ g++ -o mapper mapper.cpp 
$ g++ -o reducer reducer.cpp 
$ cat test.txt | ./mapper | ./reducer 
4

6. Hadoop streaming 测试

$ hadoop fs -put test.txt /user/test.txt
$ hadoop streaming 
    -input /user/test.txt /
    -output /user/tmp_1324 /
    -mapper ./mapper -reducer ./reducer /
    -file mapper -file reducer /
    -jobconf mapred.reduce.tasks=1 /
    -jobconf mapre.job.name="uniq_test" 

7. 查看结果

$  hadoop fs -cat /user/tmp_1324/part-00000
4

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值