【问题标题】:Hadoop Map Reduce ProgramHadoop Map Reduce 程序
【发布时间】:2011-07-21 17:41:51
【问题描述】:

当我尝试基于 Hadoop 0.20 API 的 Hadoop in Action 书中的 Map Reduce 编程示例时,我收到了错误

java.io.IOException:映射中的值类型不匹配:预期 org.apache.hadoop.io.IntWritable,收到 org.apache.hadoop.io.Text

但据我检查,我正在正确传递所有内容。如果有人可以帮助我,那将非常有帮助。

这里是代码。它的代码与书中的相同。

@SuppressWarnings("unused")
public class CountPatents extends Configured implements Tool {
    @SuppressWarnings("deprecation")

    public static class MapClass extends MapReduceBase implements Mapper<Text, Text, Text, Text> {
        public void map(Text key, Text value,OutputCollector<Text, Text> output,Reporter reporter) throws IOException {
            output.collect(value, key);
        }
    }
public static class Reduce extends MapReduceBase implements Reducer<Text, Text, Text, IntWritable> {
    public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
        int count=0;
        while(values.hasNext()){
            count=count+1;

            values.next();

        }


        output.collect(key, new IntWritable(count));
    }
}


    public int run(String[] args) throws Exception {

    Configuration conf = getConf();
    JobConf job = new JobConf(conf, CountPatents.class);
    Path in = new Path(args[0]);
    Path out = new Path(args[1]);
    FileInputFormat.setInputPaths(job, in);
    FileOutputFormat.setOutputPath(job, out);
    job.setJobName("MyJob");
    job.setMapperClass(MapClass.class);
    job.setReducerClass(Reduce.class);
    job.setInputFormat(KeyValueTextInputFormat.class);
    job.setOutputFormat(TextOutputFormat.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(Text.class);
    job.set("key.value.separator.in.input.line", ",");
    JobClient.runJob(job);
    return 0;
    }
    public static void main(String[] args) throws Exception {
        int res = ToolRunner.run(new Configuration(), new CountPatents(), args);
        System.exit(res);


    }

    }

【问题讨论】:

    标签: eclipse map hadoop reduce


    【解决方案1】:

    快速查看(不在本地运行代码),当您设置 job.setOutputValueClass(Text.class); 时,您似乎将作业的输出设置为 Text 类型,但减速器上的输出类型设置为 IntWritable。这很可能是错误。

    【讨论】:

    • 我同意。 Hadoop 强制您在三个地方重复键的类型——映射器定义、reducer 定义和作业配置。所有三个必须匹配才能运行作业。
    • 我试过这样做,但没有奏效。然后我转换了程序,使得输入和输出基本上只是文本。那行得通!
    【解决方案2】:

    未接电话:

    job.setMapOutputValueClass(IntWritable.class);

    使用新的 0.20 界面和新的“Job”对象代替 JobConf 时同样的问题。

    【讨论】:

      【解决方案3】:

      错误应该在减速器的输出中:

      你的resuce类定义如下:

      公共静态类Reduce扩展MapReduceBase实现Reducer

      所以输出值应该是 IntWritable 类型。

      但是,你提到了 job.setOutputValueClass(Text.class);

      所以根据配置,reducer 的输出应该是文本。

      解决方案: 在配置中,添加以下行 job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class);

      和修改: job.setOutputValueClass(IntWritable.class);

      然后尝试运行

      【讨论】:

        【解决方案4】:

        Map 发出

        这样设置

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        

        setMapOutputKeyClasssetMapOutputValueClass

        【讨论】:

          【解决方案5】:

          在您的减速器函数中,您使用的是 OutputCollector,这意味着输出键类将是 Text 类型,而输出值类将是 IntWritable 类型。 但是在主(运行)函数中,您设置了 job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class);.

          将 job.setOutputValueClass(Text.class) 更改为 job.setOutputValueClass(IntWritable.class) 就可以了!

          此外,最好设置 MapperOutputKeyType 和 MapperOutputValueType 以避免任何差异。 Hadoop 使用基于 Writable 接口的机制而不是原生的 Java 序列化机制。与Java序列化机制不同的是,该方法没有将类名封装在序列化实体中。因此,需要显式类名才能将这些类从 Mapper 实例化到 Reducer,因为在不知道类被反序列化为(Reducer 输入键和值实例)的情况下,无法反序列化表示 Writable 实例的字节数组。需要通过在 Job 实例上调用 setMapOutputKeyClass 和 setMapOutputValueClass 来显式提供此信息

          【讨论】:

            【解决方案6】:

            公共静态类 MapClass 扩展 MapReduceBase 实现 Mapper { public void map(Text key, Text value,OutputCollector output,Reporter 记者) 抛出 IOException { output.collect(值,键); } } 公共静态类 Reduce 扩展 MapReduceBase 实现 Reducer { 公共无效减少(文本键,迭代器值,OutputCollector 输出,Reporter 记者)抛出 IOException { 整数计数=0; 而(值。hasNext()){ 计数=计数+1;

                    values.next();
            
                }
            
            
                output.collect(key, new IntWritable(count));
            }
            

            }

            public int run(String[] args) throws Exception {
            
            Configuration conf = getConf();
            JobConf job = new JobConf(conf, CountPatents.class);
            Path in = new Path(args[0]);
            Path out = new Path(args[1]);
            FileInputFormat.setInputPaths(job, in);
            FileOutputFormat.setOutputPath(job, out);
            job.setJobName("MyJob");
            job.setMapperClass(MapClass.class);
            job.setReducerClass(Reduce.class);
            job.setInputFormat(KeyValueTextInputFormat.class);
            job.setOutputFormat(TextOutputFormat.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);
            job.set("key.value.separator.in.input.line", ",");
            JobClient.runJob(job);
            return 0;
            }
            public static void main(String[] args) throws Exception {
                int res = ToolRunner.run(new Configuration(), new CountPatents(), args);
                System.exit(res);
            
            
            }
            
            }
            

            【讨论】:

            • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation 将通过展示为什么这是解决问题的好方法,并使其对有其他类似问题的未来读者更有用,从而大大提高其长期价值。请edit您的回答添加一些解释,包括您所做的假设。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-07
            • 2015-09-03
            • 1970-01-01
            • 2014-03-22
            相关资源
            最近更新 更多