Gio*_*gio 5 java inheritance scala
我正在尝试在Scala中编写一个Hadoop映射器类.作为一个起点,我从"Hadoop:the Definitive Guide"一书中摘取了一个Java示例,并尝试将其移植到Scala.
原始Java类扩展org.apache.hadoop.mapreduce.Mapper:
public class MaxTemperatureMapper
extends Mapper<LongWritable, Text, Text, IntWritable>
Run Code Online (Sandbox Code Playgroud)
并覆盖该方法
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException
Run Code Online (Sandbox Code Playgroud)
调用此方法并正常工作(我使用单元测试进行测试,然后使用纱线运行).
我在Scala端口的尝试是:
class MaxTemperatureMapperS extends Mapper[LongWritable, Text, Text, IntWritable]
Run Code Online (Sandbox Code Playgroud)
然后是方法
@throws(classOf[IOException])
@throws(classOf[InterruptedException])
override def map(key: LongWritable, value: Text, context: Context): Unit =
{
...
}
Run Code Online (Sandbox Code Playgroud)
但Scala编译器发出错误:
error: method map overrides nothing.
Run Code Online (Sandbox Code Playgroud)
所以我认为这两种方法在Scala和Java中具有相同的签名,但显然我遗漏了一些东西.你能给我一些提示吗?
有时,最好的方法是让IDE为您服务:
class Test extends Mapper[LongWritable, Text, Text, IntWritable] {
override def map(key: LongWritable, value: Text, context: Mapper[LongWritable, Text, Text, IntWritable]#Context): Unit = ???
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,问题是类Context的定义"存在"在类中,Mapper因此您需要使用#语法