简体   繁体   English

从 Java 泛型类继承的 Scala 类

[英]Scala class inheriting from a Java generic class

I am trying to write a Hadoop mapper class in Scala.我正在尝试在 Scala 中编写一个 Hadoop 映射器类。 As a starting point, I have taken a Java example from the book "Hadoop: the Definitive Guide" and tried to port it to Scala.作为起点,我从“Hadoop:权威指南”一书中选取了一个 Java 示例,并尝试将其移植到 Scala。

The original Java class extends org.apache.hadoop.mapreduce.Mapper :原始 Java 类扩展了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

This methods gets called and works properly (I tested using a unit test and then run it with yarn).此方法被调用并正常工作(我使用单元测试进行测试,然后使用纱线运行它)。

My attempt at a Scala port is:我在 Scala 端口上的尝试是:

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 issues an error:但是 Scala 编译器发出错误:

error: method map overrides nothing.

So I thought the two methods had the same signature in Scala and Java, but apparently I am missing something.所以我认为这两种方法在 Scala 和 Java 中具有相同的签名,但显然我遗漏了一些东西。 Can you give me some hint?你能给我一些提示吗?

Sometimes the best way to do this is by letting your IDE work for you:有时,最好的方法是让您的 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 class Context "lives" inside the class Mapper so you need to use the # syntax在这种情况下,问题在于类 Context 的定义“存在于”类Mapper因此您需要使用 # 语法

For reference providing code for overriding both map and reduce method in Mapper and Reducer class respectively in scala作为参考,提供分别在 Scala 中覆盖 Mapper 和 Reducer 类中的 map 和 reduce 方法的代码

Mapper Example :映射器示例:

class MaxTemperatureMapper extends Mapper[LongWritable, Text, AvroKey[Integer], AvroValue[GenericRecord]] {

  val parser = new NcdcRecordParser()
  val record = new GenericData.Record(AvroSchema.SCHEMA)

  @throws(classOf[IOException])
  @throws(classOf[InterruptedException])
  override def map(key: LongWritable, value: Text, context:Mapper[LongWritable, Text, AvroKey[Integer], AvroValue[GenericRecord]]#Context) = {

Reducer Example :减速机示例:

class MaxTemperatureReducer extends  Reducer[AvroKey[Integer],AvroValue[GenericRecord],AvroKey[GenericRecord],NullWritable]{

  @throws(classOf[IOException])
  @throws(classOf[InterruptedException])
  override def reduce(key:AvroKey[Integer],values:java.lang.Iterable[AvroValue[GenericRecord]],
      context:Reducer[AvroKey[Integer],AvroValue[GenericRecord],AvroKey[GenericRecord],NullWritable]#Context) = {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM