Hadoop Streaming:c++uniqプログラムの作成
1.入力データの第1列に対してHadoop streamingを用いてuniqの再計算を行い、c++を用いてmap-reduceプログラムを記述する.
2.mapperプログラムmapper.cpp
3. reducer
reducer.cpp
4.テストデータtxt
5.ローカルテスト
6.Hadoop streamingテスト
7.結果の表示
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.テストデータ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