运行mapreduce程序时出现问题

时间:2016-04-09 19:41:04

标签: mapreduce

我正在尝试写一个客户单词计数程序。但是我收到错误说"错误:java.io.IOException:键入地图中的键不匹配:期望org.apache.hadoop.io.IntWritable ,收到org.apache.hadoop.io.LongWritable"你在哪里可以看到我没有在程序中的任何地方使用过LongWritable。请指导解决此问题并纠正我出错的地方。

程序: public class customeWordCount {

public static class Map extends Mapper<Text, IntWritable, Text,IntWritable>{
    private static IntWritable count;
    private static Text wordCropped;
    public void map(IntWritable key, Text value,
            Context context)
            throws IOException,InterruptedException {
    TreeMap map = new TreeMap(String.CASE_INSENSITIVE_ORDER);
        String line = value.toString();
        StringTokenizer tokenizer = new StringTokenizer(line);


        String word = null;
        while (tokenizer.hasMoreTokens()) {
            word =tokenizer.nextToken();

        if(word.length()>=3){
             wordCropped.set(word.substring(0, 3));

            if(map.containsKey( wordCropped.toString().toLowerCase())){
                count = (IntWritable) map.get(wordCropped);


                context.write(wordCropped, (IntWritable)count);

            }
            else {
                context.write(wordCropped, (IntWritable)count);
            }
        }
            }


    }

}
public static class Reduce extends Reducer<Text,IntWritable,Text,IntWritable>{

    public void reduce(Text key, Iterable<IntWritable> values,
            Context context)
            throws IOException,InterruptedException {
        int sum=0;
        // TODO Auto-generated method stub
        for(IntWritable x: values)
        {
            sum++;

        }
        context.write(key, new IntWritable(sum));

    }

}

public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub

    //JobConf conf = new JobConf(Al.class);
    Configuration conf= new Configuration();


    //conf.setJobName("mywc");
    Job job = new Job(conf,"Custome_count");

    job.setJarByClass(customeWordCount.class);
    job.setMapperClass(Map.class);
    job.setReducerClass(Reduce.class);

    //conf.setMapperClass(Map.class);
    //conf.setReducerClass(Reduce.class);
    job.setMapOutputKeyClass(IntWritable.class);

    //Defining the output value class for the mapper

    job.setMapOutputValueClass(Text.class);

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

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



    Path outputPath = new Path(args[1]);

        //Configuring the input/output path from the filesystem into the job

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

        //deleting the output path automatically from hdfs so that we don't have delete it explicitly

    outputPath.getFileSystem(conf).delete(outputPath);

        //exiting the job only if the flag value becomes false

    System.exit(job.waitForCompletion(true) ? 0 : 1);
}

}

1 个答案:

答案 0 :(得分:0)

由于您将文本文件作为输入,因此将使用的InputFormat为TextInputFormat。此格式将LongWritable作为键,将Text作为值。

话虽如此,您的映射器应声明为:

public static class Map extends Mapper<LongWritable, Text, Text,IntWritable>

并且您的地图方法签名必须更改为:

public void map(LongWritable key, Text value, Context context)
        throws IOException,InterruptedException