执行MapReduce时使用InverseMapper和IdentityReducer时出错

时间:2017-02-21 20:53:52

标签: hadoop mapreduce

所以我有一个巨大的访问日志文件,我试图在服务器上找到最受欢迎的路径。找到路径被击中的次数是传统的字数统计问题。

但是,因为输出值没有在MR作业中排序(只有键被排序)我正在执行另一个MR作业,其中mapper将Output of Previous作为输入,我使用InverseMapper.java来反转键和值并使用Identity Reducer(Reducer.java),因为不需要聚合,我只需要对键进行排序(即第一个Job的值)。这是我的代码:

package edu.pitt.cloud.CloudProject;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable.DecreasingComparator;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.map.InverseMapper;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class AccessLogMostHitPath {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        String configPath = "/usr/local/hadoop-2.7.3/etc/hadoop/";

        Path inputPath = new Path(args[0]);
        Path outputPath = new Path(args[1]);
        Path finalOutputPath = new Path(args[2]);

        Configuration config = new Configuration(true);

        config.addResource(new Path(configPath+"hdfs-site.xml"));
        config.addResource(new Path(configPath+"core-site.xml"));
        config.addResource(new Path(configPath+"yarn-site.xml"));
        config.addResource(new Path(configPath+"mapred-site.xml"));



        Job job = Job.getInstance(config, "AccessLogMostHitPath");
        job.setJarByClass(AccessLogMostHitPath.class);

        job.setMapperClass(AccessLogMostHitPathMapper.class);
        job.setReducerClass(AccessLogMostHitPathReducer.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        config.set("mapreduce.job.running.map.limit", "2");

        FileInputFormat.addInputPath(job, inputPath);
        job.setInputFormatClass(TextInputFormat.class);

        FileOutputFormat.setOutputPath(job, outputPath);
        job.setOutputFormatClass(TextOutputFormat.class);

        job.setNumReduceTasks(1);   

        System.out.println("Starting Job Execution ::: AccessLogMostHitPath");
        int code = job.waitForCompletion(true) ? 0 : 1;
        System.out.println("Job Execution Finished ::: AccessLogMostHitPath");


        if(code != 0){
            System.out.println("First Job Failed");
            System.exit(code);
        }

            FileSystem hdfs = FileSystem.get(config);
            Path successPath = new Path(outputPath+"/_SUCCESS");
            if (hdfs.exists(successPath))
              hdfs.delete(successPath, true);

        Job job2 = Job.getInstance(config, "AccessLogMostHitPathSort");
        job2.setJarByClass(AccessLogMostHitPath.class);

        job2.setMapperClass(InverseMapper.class);
        job2.setReducerClass(Reducer.class);
        //config.set("mapreduce.input.keyvaluelinerecordreader.key.value.separator", "\\t");


        KeyValueTextInputFormat.addInputPath(job2, outputPath);
        job2.setInputFormatClass(KeyValueTextInputFormat.class);

        FileOutputFormat.setOutputPath(job2, finalOutputPath);
        job2.setOutputFormatClass(TextOutputFormat.class);
        job2.setNumReduceTasks(1);

        job2.setMapOutputKeyClass(IntWritable.class);
        job2.setMapOutputValueClass(Text.class);
        job2.setSortComparatorClass(DecreasingComparator.class);

        job2.setOutputKeyClass(IntWritable.class);
        job2.setOutputValueClass(Text.class);
        config.set("mapreduce.job.running.map.limit", "2");

        System.out.println("Starting Job Execution ::: AccessLogMostHitPathSort");
        int code2 = job2.waitForCompletion(true) ? 0 : 1;
        System.out.println("Job Execution Finished ::: AccessLogMostHitPathSort");
        System.exit(code2);

    }
}

我执行以下异常时执行此操作:

Error: java.io.IOException: Type mismatch in key from map: expected org.apache.hadoop.io.IntWritable, received org.apache.hadoop.io.Text
        at org.apache.hadoop.mapred.MapTask$MapOutputBuffer.collect(MapTask.java:1072)
        at org.apache.hadoop.mapred.MapTask$NewOutputCollector.write(MapTask.java:715)
        at org.apache.hadoop.mapreduce.task.TaskInputOutputContextImpl.write(TaskInputOutputContextImpl.java:89)
        at org.apache.hadoop.mapreduce.lib.map.WrappedMapper$Context.write(WrappedMapper.java:112)
        at org.apache.hadoop.mapreduce.lib.map.InverseMapper.map(InverseMapper.java:36)
        at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:146)
        at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:787)
        at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
        at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:164)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:422)
        at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1698)
        at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)

哪里出错了?我可以看到某个地方存在Key或Value Type的不匹配但我已经交叉检查了Everything。请帮助。

1 个答案:

答案 0 :(得分:1)

问题是KeyValueTextInputFormat。这是文本输入格式,它将键读取为Text,值为Text。但是您声明job2输出映射器类型是IntWritableText

job2.setMapOutputKeyClass(IntWritable.class);
job2.setMapOutputValueClass(Text.class);

因此,您必须提供自己的输入格式,以便正确读取输入。