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/*

posted @   Sky&Zhang  阅读(338)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
历史上的今天:
2017-05-24 OpenCV实践之路——人脸检测(C++/Python) 【转】
2017-05-24 40行代码的人脸识别实践【转】
2016-05-24 Linux下的Backlight子系统(二)【转】
2016-05-24 Linux内核设计与实现读书笔记(8)-内核同步方法【转】
2016-05-24 request threaded-only IRQs with IRQF_ONESHOT【转】
2016-05-24 Linux kernel中断子系统之(五):驱动申请中断API【转】
点击右上角即可分享
微信分享提示