0

我是hadoop的新手,并试图从书中运行一个示例程序。我面临错误

java.io.IOException:映射中的键类型不匹配:预期 org.apache.hadoop.io.LongWritable,收到 org.apache.hadoop.io.Text

请帮我解决错误。下面是代码

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.KeyValueTextInputFormat;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class HadoopJob extends Configured implements Tool {



public static class MapperClass extends MapReduceBase implements Mapper<Text, Text, Text, Text> {

        @Override
        public void map(Text key, Text value, OutputCollector<Text, Text> output,
                Reporter reporter) throws IOException {
            output.collect(value, key);         
        }

    }

    public static class ReducerClass extends MapReduceBase implements Reducer<Text, Text, Text, Text> {

        @Override
        public void reduce(Text key, Iterator<Text> values,
                OutputCollector<Text, Text> output, Reporter reporter) throws IOException {

            String csv = "";
            while (values.hasNext()) {
                if (csv.length() > 0) {
                    csv += ",";
                }

                csv += values.next().toString();
            }

            output.collect(key, new Text(csv));
        }

    }

    public static void main(String[] args) throws Exception{
        int res = ToolRunner.run(new Configuration(), new HadoopJob(), args);
        System.exit(res);       
    }

    @Override
    public int run(String[] arg0) throws Exception {

        Configuration conf = getConf();
        JobConf job = new JobConf(conf, HadoopJob.class); 

        Path in = new Path("/home/sumit/Documents/samples/WordCount/input");
        Path out = new Path("/home/sumit/Documents/samples/WordCount/output");

        FileSystem fs = FileSystem.get(new Configuration());
        if (fs.exists(out)) {
            fs.delete(out, true);
        }

        FileInputFormat.setInputPaths(job, in);
        FileOutputFormat.setOutputPath(job, out);

        job.setJobName("Hadoop Job");
        job.setMapperClass(MapperClass.class);
        job.setReducerClass(ReducerClass.class);

        job.setInputFormat(KeyValueTextInputFormat.class);
        job.setOutputFormat(TextOutputFormat.class);
        job.setOutputValueClass(Text.class);
        job.set("key.value.separator.in.input.line", ",");

        JobClient.runJob(job);

        return 0;
    }

}

另请参阅下面的痕迹

13/05/18 21:04:54 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
13/05/18 21:04:54 WARN mapred.JobClient: No job jar file set.  User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
13/05/18 21:04:54 WARN snappy.LoadSnappy: Snappy native library not loaded
13/05/18 21:04:54 INFO mapred.FileInputFormat: Total input paths to process : 1
13/05/18 21:04:55 INFO mapred.JobClient: Running job: job_local_0001
13/05/18 21:04:55 INFO util.ProcessTree: setsid exited with exit code 0
13/05/18 21:04:55 INFO mapred.Task:  Using ResourceCalculatorPlugin : org.apache.hadoop.util.LinuxResourceCalculatorPlugin@5090d8ea
13/05/18 21:04:55 INFO mapred.MapTask: numReduceTasks: 1
13/05/18 21:04:55 INFO mapred.MapTask: io.sort.mb = 100
13/05/18 21:04:55 INFO mapred.MapTask: data buffer = 79691776/99614720
13/05/18 21:04:55 INFO mapred.MapTask: record buffer = 262144/327680
13/05/18 21:04:55 WARN mapred.LocalJobRunner: job_local_0001
java.io.IOException: Type mismatch in key from map: expected org.apache.hadoop.io.LongWritable, recieved org.apache.hadoop.io.Text
    at org.apache.hadoop.mapred.MapTask$MapOutputBuffer.collect(MapTask.java:1014)
    at org.apache.hadoop.mapred.MapTask$OldOutputCollector.collect(MapTask.java:592)
    at com.sis.hadoop.wordcount.HadoopJob$MapperClass.map(HadoopJob.java:32)
    at com.sis.hadoop.wordcount.HadoopJob$MapperClass.map(HadoopJob.java:1)
    at org.apache.hadoop.mapred.MapRunner.run(MapRunner.java:50)
    at org.apache.hadoop.mapred.MapTask.runOldMapper(MapTask.java:436)
    at org.apache.hadoop.mapred.MapTask.run(MapTask.java:372)
    at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:212)
13/05/18 21:04:56 INFO mapred.JobClient:  map 0% reduce 0%
13/05/18 21:04:56 INFO mapred.JobClient: Job complete: job_local_0001
13/05/18 21:04:56 INFO mapred.JobClient: Counters: 0
13/05/18 21:04:56 INFO mapred.JobClient: Job Failed: NA
Exception in thread "main" java.io.IOException: Job failed!
    at org.apache.hadoop.mapred.JobClient.runJob(JobClient.java:1265)
    at com.sis.hadoop.wordcount.HadoopJob.run(HadoopJob.java:88)
    at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:65)
    at com.sis.hadoop.wordcount.HadoopJob.main(HadoopJob.java:58)
4

1 回答 1

6

您应该在作业配置中明确定义输出键类(就像您已经为值所做的那样):

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

默认可能是 LongWritable

于 2013-05-18T15:48:21.493 回答