在C中使用MapReduce代码有什么问题?

时间:2014-01-15 08:43:06

标签: c hadoop mapreduce hadoop-streaming

每行的格式为: date \ ttime \ tstore name \ titem description \ tcost \ tmeththod of payment 我们想要元素2(商店名称)和4(成本) 我们需要将它们写出标准输出,用标签分隔

我希望获得每家商店的总销售额。我通过手动输入数据进行检查,但它似乎不适用于MapReduce。 我希望找出问题及其解决方案,建议如果可能,再次避免此类问题,因为我自己从Udacity教程中学习这一切。

MAPPER

#include<stdio.h>
main()
{

size_t p;
int i,j,k;
char *A,*a;
char *store,*cost;
while(getline(&A,&p,stdin) != -1)
{
    i=0,j=0,k=0;
     for(a=A;*a!='\n'&& i<=5;a++)
    {
        if(*a=='\t')
        {
            i++;
            if(i==2)
            store=a+1;
            if(i==4)
            cost=a+1;
            *a='\0';

        }       


    }
    if(i==5)
    printf("%s\t%s",store,cost);




}   

}

减速器

#include<stdio.h>
#include<string.h>
main()
{
size_t p;
int i,j,k,flag=0;
char *A,*a;
char storenow[100]={0},*sval,storepre[100]={0};
double val,valpre;
while(getline(&A,&p,stdin) != -1)
{
    strcpy(storepre,storenow);
    valpre=val;
    a=A;
    while(*A!='\t')
    A++;
    *A='\0';
    A++;
    sval=A;
    sscanf(sval,"%lf",&val);
    strcpy(storenow,a);

    if(!(strcmp(storenow,storepre))||!flag)
    val=val+valpre;
    else
    printf("%s\t%lf\n",storepre,valpre);
    flag=1;
}

    printf("%s\t%lf\n",storenow,val);

}

1 个答案:

答案 0 :(得分:0)

无论算法有任何问题,程序都会使用getline()错误。

输出指针应该在调用之前初始化为NULL,否则它被解释为已经分配。你应该free()在某一点上行。

我还认为,至少在第一个程序中,通过直接在输入流上使用sscanf()(甚至scanf())而不是自己的标记化循环,您可以大大简化代码。如果你确实需要支持任意长的输入行,这可能会有问题。