3

ドライバーコード:

public class WcDriver {

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

        Job job = new Job(conf, "WcDriver");
        job.setJarByClass(WcDriver.class);

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

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

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        job.setMapperClass(WcMapper.class);
        job.setReducerClass(WcReducer.class);

        job.waitForCompletion(true);
    }
}

レデューサーコード

public class WcReducer extends Reducer<Text, LongWritable, Text,String>
{   
    @Override
    public void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
        String key1 = null;
        int total = 0;
        for (LongWritable value : values) {
            total += value.get();
            key1= key.toString();
        }
        context.write(new Text(key1), "ABC");
    }
}

ここでは、ドライバ クラスでは と を設定job.setOutputKeyClass(Text.class)job.setOutputValueClass(LongWritable.class)ましたが、リデューサ クラスでは string を記述していますcontext.write(new Text(key1), "ABC");。出力タイプが一致しないため、プログラムの実行中にエラーが発生するはずであり、レデューサーのキーも実装する必要がWritableComparableあり、値はWritableインターフェイスを実装する必要があると思います。不思議なことに、このプログラムは順調に進んでいます。例外がない理由がわかりません。

4

2 に答える 2

3

これを試してみてください:

 //  job.setOutputFormatClass(TextOutputFormat.class); 
// comment this line, and you'll sure get exception of casting.

これは、TextOutputFormat が LongWritable をキーとして、Text を値として想定しているためです。outPutFormat クラスを定義しない場合、書き込み可能なデフォルトの動作が期待されますが、これについて言及すると、指定された型に暗黙的にキャストします。;

于 2014-02-23T13:05:05.567 に答える
1

これを試して

//job.setOutputValueClass(LongWritable.class); if you comment this line you get an error
this will for only define the key value pair by defaul it depent on the output format and
it will be text so this is not giving any error
于 2014-02-23T13:05:05.597 に答える