在hadoop计划中输入不匹配错误

时间:2018-04-05 04:32:58

标签: java hadoop mapreduce

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.apache.hadoop.io.Text不是类org.apache.hadoop.io.IntWritable           在org.apache.hadoop.mapred.IFile $ Writer.append(IFile.java:194)           在org.apache.hadoop.mapred.Task $ CombineOutputCollector.collect(Task.java:1350)           在org.apache.hadoop.mapred.Task $ NewCombinerRunner $ OutputConverter.write(Task.java:1667)           at org.apache.hadoop.mapreduce.task.TaskInputOutputContextImpl.write(TaskInputOutputContextImpl.java:89)           在org.apache.hadoop.mapreduce.lib.reduce.WrappedReducer $ Context.write(WrappedReducer.java:105)           在CommonFriends $ IntSumReducer.reduce(CommonFriends.java:51)           在CommonFriends $ IntSumReducer.reduce(CommonFriends.java:38)           在org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:171)           在org.apache.hadoop.mapred.Task $ NewCombinerRunner.combine(Task.java:1688)           在org.apache.hadoop.mapred.MapTask $ MapOutputBuffer.sortAndSpill(MapTask.java:1637)           在org.apache.hadoop.mapred.MapTask $ MapOutputBuffer.flush(MapTask.java:1489)           at org.apache.hadoop.mapred.MapTask $ NewOutputCollector.close(MapTask.java:723)           在org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:793)           在org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)           在org.apache.hadoop.mapred.YarnChild $ 2.run(YarnChild.java:164)           at java.security.AccessController.doPrivileged(Native Method)           在javax.security.auth.Subject.doAs(Subject.java:422)           在org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1657)           在org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)

这是我的代码,错误信息如下。 任何的想法?? 我认为mapper和reducer的输出类配置问题 输入文件是文件中的数字列表。 如果需要,将提供更多细节。 该程序找到朋友之间的共同朋友

2 个答案:

答案 0 :(得分:0)

刚看了你的代码,看来你正在使用reducer代码作为汇编代码。

您需要检查一件事。

您的合并器代码将以<Text, IntWritable>的形式输入,而Combiner的输出将为<Text, Text>格式。

然后,Reducer的输入格式为< Text, Text>,但您已将Reducer的输入指定为< Text, IntWritable >,因此它会抛出错误。

可以做两件事: -

1)您可以考虑更改Reducer的输出类型。

2)您可以考虑编写单独的Combiner代码。

答案 1 :(得分:0)

删除代码中的job.setCombinerClass(IntSumReducer.class);可以解决此问题