Hadoop 上使用C 语言编程【转】

转自:https://www.linuxidc.com/Linux/2012-04/58991.htm

今天尝试用C语言在Hadoop上编写统计单词的程序,具体过程如下:

一、编写map和reduce程序

mapper.c

  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <string.h>   
  4.   
  5. #define BUF_SIZE    2048   
  6. #define DELIM       '\n'   
  7.   
  8. int main(int argc, char * argv[])  
  9. {  
  10.     char buffer[BUF_SIZE];  
  11.     while(fgets(buffer,BUF_SIZE-1,stdin))  
  12.     {  
  13.         int len = strlen(buffer);  
  14.         if(buffer[len-1] == DELIM) // 将换行符去掉   
  15.             buffer[len-1] = 0;  
  16.   
  17.         char *query = NULL;  
  18.         query = strtok(buffer, " ");  
  19.         while(query)  
  20.         {  
  21.             printf("%s\t1\n",query);  
  22.             query = strtok(NULL," ");  
  23.         }  
  24.     }  
  25.     return 0;  
  26. }  
reducer.c
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <string.h>   
  4.   
  5. #define BUFFER_SIZE     1024   
  6. #define DELIM       "\t"   
  7.   
  8. int main(int argc, char * argv[])  
  9. {  
  10.     char str_last_key[BUFFER_SIZE];  
  11.     char str_line[BUFFER_SIZE];  
  12.     int count = 0;  
  13.   
  14.     *str_last_key = '\0';  
  15.   
  16.     while( fgets(str_line,BUFFER_SIZE-1,stdin) )  
  17.     {  
  18.         char * str_cur_key = NULL;  
  19.         char * str_cur_num = NULL;  
  20.   
  21.         str_cur_key = strtok(str_line,DELIM);  
  22.         str_cur_num = strtok(NULL,DELIM);  
  23.   
  24.         if(str_last_key[0] =='\0')  
  25.         {  
  26.             strcpy(str_last_key,str_cur_key);  
  27.         }  
  28.         if(strcmp(str_cur_key, str_last_key))// 前后不相等,输出   
  29.         {  
  30.             printf("%s\t%d\n",str_last_key,count);  
  31.             count = atoi(str_cur_num);  
  32.         }else{// 相等,则加当前的key的value   
  33.             count += atoi(str_cur_num);  
  34.         }  
  35.         strcpy(str_last_key,str_cur_key);  
  36.     }  
  37.     printf("%s\t%d\n",str_last_key,count);  
  38.     return 0;  
  39. }  
二、编译

gcc mapper.c -o mapper

gcc reducer.c -o reducer

三、运行

(一)启动hadoop后将待统计单词的输入文件放到 input文件夹中:bin/hadoop fs -put 待统计文件 input

(二)使用contrib/streaming/下的jar工具调用上面的mapper\reducer:

bin/hadoop jar /home/huangkq/Desktop/hadoop/contrib/streaming/hadoop-streaming-0.20.203.0.jar -mapper /home/huangkq/Desktop/hadoop2/mapper -reducer /home/huangkq/Desktop/hadoop2/reducer -input input -output c_output -jobconf mapred.reduce.tasks=2

说明:hadoop-streaming-0.20.203.0.jar是一个管道工具

(三)查看结果:bin/hadoop fs -cat c_output/*

转载于:https://www.cnblogs.com/sky-heaven/p/9083203.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值