hadoop - Hadoop按键分组错误

标签 hadoop mapreduce

这是我的代码

public class SJob {

 public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException
{ 
    Job job = new Job();
    job.setJarByClass(SJob.class);
    job.setJobName("SJob");

    FileInputFormat.addInputPath(job, new Path("/home/WORK/input/data.csv"));
    FileOutputFormat.setOutputPath(job, new Path("/home/WORK/output"));

    job.setMapperClass(SMapper.class);
    job.setReducerClass(SReducer.class);

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(Text.class);

    job.waitForCompletion(true);

 }

}

public class SMapper extends Mapper<LongWritable, Text, Text, Text>{

 @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

    String line = value.toString();
    String parts[] = line.split(";");

    context.write(new Text(parts[0]), new Text(parts[1]));
 }


}

public class SReducer extends Reducer<Text, Text, Text, Text>{

 @Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {

    String properties = "";

    int noOfElements = 0;

    for(Text value : values)
    {
        properties += value + " ";
        noOfElements++;
    }

    properties += "  " + noOfElements;

        context.write(key, new Text(properties));

 }

}

这是我的输入文件

1;a

2;a

3;a

4;a

1;b

2;b

3;b

4;b

1;c

2;c

3;c

4;c



这是我的输出文件

1 b c 2

2 a b c 3

3 a b c 3

4 a b c 3

1 a 1



如您所见,按键分组效果不佳,输出应为

1 a b c 3

2 a b c 3

3 a b c 3

4 a b c 3



看起来在处理第一行时存在某种问题,我尝试交换第一行和第二行,然后发生相同的事情,在这种情况下,

2 a b c 3

I get

2 b c 2

2 a 1



可能是什么原因?

最佳答案

我发现了问题。

由于某些限制,我使用了Hadoop 0.20.2。事实是,Hadoop中存在一个错误,该错误已在某些版本中解决,但在我使用的版本中未解决。

https://issues.apache.org/jira/browse/MAPREDUCE-5777

此版本不适用于UTF-8文件。该文件需要另存为UTF-8(不带BOM)。

纠正此问题后,一切正常。

关于hadoop - Hadoop按键分组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27313213/

相关文章:

java - Oozie Java API Kerberos 身份验证

hadoop - 如何在 oozie 作业中将文件添加到分布式缓存

hadoop - 在 Pig 中使用带 MATCHES 的双引号

node.js - 我应该使用哪个 nodejs 库来写入 HDFS?

hadoop - Mapper 和 Reduce 上的依赖注入(inject)

hadoop - Hadoop/MapReduce 的替代模型

hadoop - Hadoop 2.2.0 中的 MapReduce 无法正常工作

hadoop - hadoop list 命令是否显示状态不是 1 的作业?

java - Flink 中的 LeftOuterJoin(JAVA API)

java - 比较 MapReduce 性能