代码之家  ›  专栏  ›  技术社区  ›  Shadi Alian

hadoop程序中的类型不匹配错误

  •  2
  • Shadi Alian  · 技术社区  · 7 年前
    import java.io.IOException;
    import java.util.*;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Job;
    import org.apache.hadoop.mapreduce.Mapper;
    import org.apache.hadoop.mapreduce.Reducer;
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    public class CommonFriends {
            public static class TokenizerMapper
                    extends Mapper<Object, Text, Text, IntWritable>{
                    private IntWritable friend = new IntWritable();
                    private Text friends = new Text();
                    public void map(Object key, Text value, Context context )     throws IOException, InterruptedException {
                            StringTokenizer itr = new     StringTokenizer(value.toString(),"\n");
                        while (itr.hasMoreTokens()) {
                                String[] line = itr.nextToken().split(" ");
                                if(line.length > 2 ){
                                        int person = Integer.parseInt(line[0]);
                                        for(int i=1; i<line.length;i++){
                                                int ifriend = Integer.parseInt(line[i]);
                                                friends.set((person < ifriend ? person+"-"+ifriend : ifriend+"-"+person));
                                                for(int j=1; j< line.length; j++ ){
                                                        if( i != j ){
                                                                friend.set(Integer.parseInt(line[j]));
                                                                context.write(friends, friend);
                                                        }
                                                }
                                        }
                                }
                        }
                }
        }
    
        public static class IntSumReducer extends Reducer<Text,IntWritable,Text,Text> {
                private Text result = new Text();
                public void reduce(Text key, Iterable<IntWritable> values, Context context)
                        throws IOException, InterruptedException {
                        HashSet<IntWritable> duplicates = new HashSet();
                        ArrayList<Integer> tmp = new ArrayList();
                        for (IntWritable val : values) {
                                if(duplicates.contains(val))
                                        tmp.add(val.get());
                                else
                                        duplicates.add(val);
                        }
                        result.set(tmp.toString());
                        context.write(key, result);
                }
        }
    
        public static void main(String[] args) throws Exception {
                Configuration conf = new Configuration();
                Job job = Job.getInstance(conf, "Common Friends");
                job.setJarByClass(CommonFriends.class);
                job.setMapperClass(TokenizerMapper.class);
                job.setCombinerClass(IntSumReducer.class);
                job.setReducerClass(IntSumReducer.class);
                job.setMapOutputKeyClass(Text.class);
                job.setMapOutputValueClass(IntWritable.class);
                job.setOutputKeyClass(Text.class);
                job.setOutputValueClass(Text.class);
                FileInputFormat.addInputPath(job, new Path(args[0]));
                FileOutputFormat.setOutputPath(job, new Path(args[1]));
                System.exit(job.waitForCompletion(true) ? 0 : 1);
        }
    }
    

    错误:java。io。IOException:错误的值类:class org。阿帕奇。hadoop。io。文本不是类别组织。阿帕奇。hadoop。io。可写入的 位于组织。阿帕奇。hadoop。映射。IFile$编写器。追加(IFile.java:194) 位于组织。阿帕奇。hadoop。映射。任务$CombineOutputCollector。收集(Task.java:1350) 位于组织。阿帕奇。hadoop。映射。任务$NewCombinerRunner$OutputConverter。写入(Task.java:1667) 位于组织。阿帕奇。hadoop。mapreduce。任务TaskInputOutputContextImpl。写入(TaskInputOutputContextImpl.java:89) 位于组织。阿帕奇。hadoop。mapreduce。lib。减少WrappedReducer$上下文。write(WrappedReducer.java:105) 在CommonFriends$IntSumReducer。reduce(CommonFriends.java:51) 在CommonFriends$IntSumReducer。reduce(CommonFriends.java:38) 位于组织。阿帕奇。hadoop。mapreduce。减速器。运行(Reducer.java:171) 位于组织。阿帕奇。hadoop。映射。任务$NewCombinerRunner。联合收割机(Task.java:1688) 位于组织。阿帕奇。hadoop。映射。MapTask$MapOutputBuffer。sortAndSpill(MapTask.java:1637) 位于组织。阿帕奇。hadoop。映射。MapTask$MapOutputBuffer。刷新(MapTask.java:1489) 位于组织。阿帕奇。hadoop。映射。MapTask$NewOutputCollector。关闭(MapTask.java:723) 位于组织。阿帕奇。hadoop。映射。MapTask。runNewMapper(MapTask.java:793) 位于组织。阿帕奇。hadoop。映射。MapTask。运行(MapTask.java:341) 位于组织。阿帕奇。hadoop。映射。YarnChild 2美元。run(YarnChild.java:164) 在java。安全AccessController。doPrivileged(本机方法) 在javax。安全授权。主题doAs(主题:java:422) 位于组织。阿帕奇。hadoop。安全用户组信息。doAs(UserGroupInformation.java:1657) 位于组织。阿帕奇。hadoop。映射。YarnChild。main(YarnChild.java:158)

    这是我的代码,错误消息如下。 有什么想法吗?? 我认为mapper和reducer的输出类的配置问题 输入文件是文件中的数字列表。 如果需要,将提供更多详细信息。 程序在朋友之间查找常见的朋友

    2 回复  |  直到 7 年前
        1
  •  0
  •   HbnKing    7 年前

    去除 job.setCombinerClass(IntSumReducer.class); 在你的代码中可以解决这个问题

        2
  •  0
  •   Deepan Ram    7 年前

    刚刚查看了您的代码,似乎您正在使用reducer代码作为组合器代码。

    有一件事你需要检查一下。

    您的组合器代码将以 <Text, IntWritable> 合路器的输出为 <Text, Text> 总体安排

    那么减速机的输入格式为 < Text, Text> 但您已将Reducer的输入指定为 < Text, IntWritable > ,因此它抛出了错误。

    可以做两件事:-

    1) 您可以考虑更改减速器的输出类型。

    2) 您可以考虑编写一个单独的组合器代码。