【问题标题】:IOException: Type mismatch in key from map: Text, received LongWritableIOException:映射中的键类型不匹配:文本,收到 LongWritable
【发布时间】:2017-08-29 00:24:32
【问题描述】:

我了解该主题过去曾讨论过。但不幸的是,我无法解决这个问题。我不断收到相同的 IOException 错误。我对 Java 和 Hadoop 非常陌生,这是我第一次尝试 WordCount 练习。对于任何语法错误或格式问题,我深表歉意。请让我知道我哪里出错了。

错误:java.lang.Exception:java.io.IOException:键中的类型不匹配 来自地图:预期 org.apache.hadoop.io.Text,已收到 org.apache.hadoop.io.LongWritable

这是我的代码:

MyDriver

package p1;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class MyDriver {
    public static void main(String[] args) throws IOException,
            ClassNotFoundException, InterruptedException {
        // Create a configuration class pointing to default configuration
        Configuration conf = new Configuration();

        // Create an object of Job by specifying conf object
        Job job = Job.getInstance(conf, "MyWordCountJob");

        // Link your Driver Class with the Job
        job.setJarByClass(MyDriver.class);

        // Link your Mapper Class with the Job
        job.setJarByClass(MyMapper.class);

        // Link your Reducer Class with the Job
        job.setJarByClass(MyReducer.class);

        // set final output Key
        job.setOutputKeyClass(Text.class);

        // set final output Value
        job.setOutputValueClass(IntWritable.class);

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

        // Defining input Paths
        Path input_dir = new Path("hdfs://localhost:9000/input_data/");
        FileInputFormat.addInputPath(job, input_dir);

        // Defining Output Paths
        Path output_dir = new Path("hdfs://localhost:9000/output_data/");
        FileOutputFormat.setOutputPath(job, output_dir);

        // This piece of code will actually initiate the Job run
        // boolean flag = job.waitForCompletion(true);
        System.out.println("is this running");

        // system.exit means it will kill the jvm and terminate the program.
        System.exit(job.waitForCompletion(true) ? 0 : 1);
        System.out.println("job.waitForCompletion(true)"
                + job.waitForCompletion(true));

    }
}

package p1;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

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

        String currentline = line.toString();
        System.out.println("MyMapper.map():Offset" + offset
                + " :: CurrentLine=" + currentline);

        // apple apple ball--this is read when the mapper reads the data for the
        // first time/ mapper reads line by line.
        System.out.println(currentline);
        String words[] = currentline.split(" ");

        for (String word : words) {
            System.out.println(" words " + word);
            context.write(new Text(word), new IntWritable(1));
        }
    }
}

MyReducer

package p1;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

    //apple [1,1,1]

    @Override
    protected void reduce(Text word, Iterable<IntWritable> value, Context ctx)
            throws IOException, InterruptedException {

        System.out.println("Key=" + word);

        Iterator<IntWritable> it = value.iterator();

        int count = 0;
        while (it.hasNext()) {
            IntWritable i = (IntWritable) it.next();
            count = count + i.get();
        }

        ctx.write(word, new IntWritable(count));
    }

}

【问题讨论】:

  • 是在 map 阶段还是 reduce 阶段中断?
  • 它正在地图阶段中断。谢谢
  • 您能否提供映射器正在读取的输入文件中的示例行?
  • 这里是输入文件“apple apple apple”的第一行

标签: eclipse hadoop mapreduce word-count


【解决方案1】:

使用

job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);

而不是

job.setJarByClass(MyMapper.class);
job.setJarByClass(MyReducer.class);

【讨论】:

  • @zoheb 如果此解决方案解决了您的问题,请您接受它
  • 谢谢沙拉吉。问题得到解决。我能够获得所需的输出。你能告诉我什么时候应该使用 setJarByClass。
  • setJarByClass 仅用于设置您的驱动程序类,在您的情况下 job.setJarByClass(MyDriver.class);请点击勾选图标接受此答案
  • 谢谢沙拉吉。
【解决方案2】:

问题是您的 Map 键的类型检查问题(在几乎所有实际情况下,您都不需要在 map 阶段修改键类型)。试试这个:

public class MyMapper extends Mapper<Object, Text, Text, IntWritable> {

    @Override
    protected void map(Object offset, Text line, Context context)
        throws IOException, InterruptedException {

【讨论】:

  • 谢谢保罗。我做了建议的更改。但我继续得到同样的错误。 :(
  • 能否将您进入问题部分的堆栈跟踪粘贴到问题部分?
  • Paul,问题通过替换 setJarByClass 得到解决。你还有兴趣查看堆栈跟踪吗??
猜你喜欢
  • 2012-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多