执行时Hadoop程序错误 - 来自map的键中不匹配:期望org.apache.hadoop.io.Text,收到org.apache.hadoop.io.LongWritable

时间:2014-04-14 18:23:25

标签: java mapreduce

执行字母计数程序时出现以下错误。

java.io.IOException: Type mismatch in key from map: expected org.apache.hadoop.io.Text, recieved org.apache.hadoop.io.LongWritable
    at org.apache.hadoop.mapred.MapTask$MapOutputBuffer.collect(MapTask.java:1014)
    at org.apache.hadoop.mapred.MapTask$NewOutputCollector.write(MapTask.java:691)
    at org.apache.hadoop.mapreduce.TaskInputOutputContext.write(TaskInputOutputContext.java:80)
    at com.example.AlphabetCount$Map.map(AlphabetCount.java:40)

用于运行的命令:./ bin / hadoop jar /home/ubuntu/Documents/AlphabetCount.jar输入输出

当我使用错误消息谷歌时,我浏览并检查了前八个链接。我已经实现了他们的建议,但出现了错误消息。你能帮帮我吗?

代码:

package com.example;

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

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.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 AlphabetCount {

    public static class Map1 extends
            Mapper<LongWritable, Text, Text, IntWritable> {

        private Text alphabet = new Text();

        public void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            String line = value.toString();
            byte[] byteArray = line.getBytes();
            int sum = 0;
            alphabet.set("a");
            for (int i = 0; i < byteArray.length; i++) {
                if ((byteArray[i] == 'a') || (byteArray[i] == 'A')) {
                    sum += 1;
                }
            }
            context.write(alphabet, new IntWritable(sum));
        }
    }

    public static class Reduce1 extends
            Reducer<Text, IntWritable, Text, IntWritable> {

        public void reduce(Text key, Iterator<IntWritable> value,
                Context context) throws IOException, InterruptedException {
            final Text alphabet = new Text();
            alphabet.set(key);
            int sum = 0;
            while (value.hasNext()) {
                sum = sum + value.next().get();
            }
            context.write(alphabet, new IntWritable(sum));
        }
    }

    public static void main(String[] args) throws IOException,
            InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        Job job = new Job(conf);
        job.setJarByClass(AlphabetCount.class);

        job.setMapperClass(Map1.class);
        job.setCombinerClass(Reduce1.class);
        job.setReducerClass(Reduce1.class);

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

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.waitForCompletion(true);
    }
}

更新:(解决方案)以上代码有效!我收到错误是因为我正在执行的jar与我用上面的代码更新的jar不同!我最初将jar(带有错误代码)从eclipse导出到位置x,然后我在位置y更新代码,但仍然从位置x执行jar!该死!

1 个答案:

答案 0 :(得分:0)

尝试在main方法中指定输入和输出格式类,并尝试指定Mapper的输入键格式。你应该有类似的东西:

public class AlphabetCount {

public static class Map1 extends
        Mapper<Text, Text, Text, IntWritable> {

    ...

    public void map(Text key, Text value, Context context)
            throws IOException, InterruptedException {
        ...
    }
}

public static class Reduce1 extends
        Reducer<Text, IntWritable, Text, IntWritable> {

    public void reduce(Text key, Iterator<IntWritable> value,
            Context context) throws IOException, InterruptedException {

        ...
    }
}

public static void main(String[] args) throws IOException,
        InterruptedException, ClassNotFoundException {
    Configuration conf = new Configuration();
    Job job = new Job(conf);

    ...

    job.setInputFormatClass(KeyValueTextInputFormat.class);
    job.setOutputFormatClass(TextOutputFormat.class);

    ...
}
}