Java 驱动程序中的job.setOutputKeyClass和setOutputValueClass与减速器不匹配';s context.write方法,但程序运行正常。如何? 驱动程序代码: 减速机代码 公共类WcReducer扩展了Reducer { @凌驾 公共void reduce(文本键、Iterable值、上下文上下文)引发IOException、InterruptedException{ 字符串key1=null; int-total=0; for(可长写值:值){ total+=value.get(); key1=key.toString(); } 编写(新文本(键1),“ABC”); } }

Java 驱动程序中的job.setOutputKeyClass和setOutputValueClass与减速器不匹配';s context.write方法,但程序运行正常。如何? 驱动程序代码: 减速机代码 公共类WcReducer扩展了Reducer { @凌驾 公共void reduce(文本键、Iterable值、上下文上下文)引发IOException、InterruptedException{ 字符串key1=null; int-total=0; for(可长写值:值){ total+=value.get(); key1=key.toString(); } 编写(新文本(键1),“ABC”); } },java,hadoop,mapreduce,hadoop2,Java,Hadoop,Mapreduce,Hadoop2,这里,在驱动程序类中,我设置了job.setOutputKeyClass(Text.class)和job.setOutputValueClass(LongWritable.class),但在reducer类中,我编写了一个字符串context.write(新文本(key1),“ABC”)。我认为在运行程序时应该有一个错误,因为输出类型不匹配,而且reducer的键应该实现writeablecomparable,value应该实现writeable接口。奇怪的是,这个程序运行得很好。我不明白为什么

这里,在驱动程序类中,我设置了
job.setOutputKeyClass(Text.class)
job.setOutputValueClass(LongWritable.class)
,但在reducer类中,我编写了一个字符串
context.write(新文本(key1),“ABC”)。我认为在运行程序时应该有一个错误,因为输出类型不匹配,而且reducer的键应该实现
writeablecomparable
,value应该实现
writeable
接口。奇怪的是,这个程序运行得很好。我不明白为什么没有例外。

尝试这样做:

 //  job.setOutputFormatClass(TextOutputFormat.class); 
// comment this line, and you'll sure get exception of casting.
这是因为,TextOutputFormat假定LongWritable为键,Text为值,如果您不定义outPutFormat类,它将获得默认的writable行为,这是默认情况,但如果您提到它,它将隐式地将其转换为给定类型

试试这个

//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

没有人,请查看我的回答,这是因为您没有为reducer提供outputkeyclass,它是为mapperclass提供的,reducer将您的映射器输出存储在临时内存中…Hi Ashish,感谢您的关注。我尝试过这个,但它也工作正常,没有给出任何错误。我认为无论我们是否定义它都不重要,因为默认情况下它只接受TextOutputformat。你能详细解释一下你的答案吗?我的系统出现了异常是的,在这样做之后,程序出现了异常。但我不清楚原因。你能详细说明一下吗。
 //  job.setOutputFormatClass(TextOutputFormat.class); 
// comment this line, and you'll sure get exception of casting.
//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