Scala class inheriting from a common Java class

I am trying to write a Hadoop mapping class in Scala. As a starting point, I gave a Java example from the book “Hadoop: The Ultimate Guide” and tried to port it to Scala.

The original Java class extends org.apache.hadoop.mapreduce.Mapper :

 public class MaxTemperatureMapper extends Mapper<LongWritable, Text, Text, IntWritable> 

and overrides the method

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

These methods are called and work correctly (I tested using the unit test, and then ran it using yarn).

My attempt at a Scala port:

 class MaxTemperatureMapperS extends Mapper[LongWritable, Text, Text, IntWritable] 

and then the method

 @throws(classOf[IOException]) @throws(classOf[InterruptedException]) override def map(key: LongWritable, value: Text, context: Context): Unit = { ... } 

but the Scala compiler produces an error message:

 error: method map overrides nothing. 

So, I thought that the two methods have the same signature in Scala and Java, but apparently I'm missing something. Can you give me a hint?

+5
source share
1 answer

Sometimes the best way to do this is to let you work with the IDE:

 class Test extends Mapper[LongWritable, Text, Text, IntWritable] { override def map(key: LongWritable, value: Text, context: Mapper[LongWritable, Text, Text, IntWritable]#Context): Unit = ??? } 

In this case, the problem is that the definition of the Context class "lives" inside the Mapper class, so you need to use syntax C #

+5
source

Source: https://habr.com/ru/post/1275244/


All Articles