1.C语言实现WordCount
编写Mapper程序
//mapper #include <stdio.h> #include <string.h> #include <stdlib.h> #define BUF_SIZE 2048 #define DELIM " " #define SEPARATOR " " int main(int argc, char *argv[]){ char buffer[BUF_SIZE]; while(fgets(buffer, BUF_SIZE - 1, stdin)){ int len = strlen(buffer); if(buffer[len-1] == ' ') { buffer[len-1] = 0; } char *query = NULL; query = strtok(buffer,SEPARATOR); while(query){ printf("%s 1 ", query); query = strtok(NULL, " "); } } return 0; }
执行编译
gcc mapper.c -o wordcountmapper
编写reducer程序
#include <stdio.h> #include <string.h> #include <stdlib.h> #define BUFFER_SIZE 1024 #define DELIM " " int main(int argc, char *argv[]){ char strLastKey[BUFFER_SIZE]; char strLine[BUFFER_SIZE]; int count = 0; *strLastKey = '