想请问C++在使用 Hadoop Streaming 的部分,本身的"mapper"和"reduce"中,可否加进开文件读文件的功能?
我修改C++ wordcount的程序,希望mapper或reduce中可以fopen一个新文字文件,并且把<Key, Value>记录在这个文字文件中,实际直接测试C++的程序是OK的,但丢上Hadoop去执行,只有wordcount部分的结果可以成功执行,我在hdfs中的input/output都没有看到任何有关我开新的那一个文字文件的踪迹。想请问在hadoop streaming中,我用C++开档读档的这个动作,是否不被接受?
//Mapper.cpp
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main() {
string key;
FILE *pFile;
//char* libvar;
pFile = fopen ("hadoop_mapper_cpp.txt", "wa+");
//libvar = (char*) "echo 1234567";
fprintf(pFile, "mapper\n");
if (pFile == NULL) {
printf ("FAILED\n");
return 0;
}
while(cin >> key) {
cout << key << "\t" << "1" << endl;
fprintf(pFile,"%s\n", key.c_str());
}
fclose (pFile);
return 0;
}
//
//Reducer.cpp
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main() {
string cur_key, last_key, value;
FILE *pFile;
cin >> cur_key >> value;
last_key = cur_key;
int n = 1;
pFile = fopen ("hadoop_mapper_cpp.txt", "a+");
fprintf(pFile, "\n\nreducer\n\n");
while(cin >> cur_key) {
cin >> value;
if(last_key != cur_key) {
cout << last_key << "\t" << n << endl;
last_key = cur_key;
n = 1;
} else {
n++;
}
fprintf(pFile,"5.Last Key: %s\t%d\n\n", last_key.c_str(), n);
}
谢谢!
该贴被蜀山战纪编辑于2015-12-4 10:07:18