• 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       ' '   
    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 1 ",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       " "   
    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 = '';  
    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] =='')  
    25.         {  
    26.             strcpy(str_last_key,str_cur_key);  
    27.         }  
    28.         if(strcmp(str_cur_key, str_last_key))// 前后不相等,输出   
    29.         {  
    30.             printf("%s %d ",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 %d ",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 educer:

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

  • 相关阅读:
    wpf 获取datagrid中模板中控件
    WPF DataGrid DataGridTemplateColumn 控制模板中控件
    ztree实现拖拽移动和复制
    layui的select监听
    layui父页面获取子页面数据
    win10安装网络适配器
    bat启动OpenOffice4
    java注解简单使用
    win7安装IIS
    java的Array和List相互转换
  • 原文地址:https://www.cnblogs.com/sky-heaven/p/9083203.html
Copyright © 2020-2023  润新知