How to reference subclasses of Java static classes with generics in Scala

I have this Java code:

public class TestMapper extends AppEngineMapper<Key, Entity, NullWritable, NullWritable> {
  public TestMapper() {
  }
// [... other overriden methods ...]
      @Override
      public void setup(Context context) {
        log.warning("Doing per-worker setup");
      }
}

... which I converted to:

class TestMapper extends AppEngineMapper[Key, Entity, NullWritable, NullWritable] {
// [... other overriden methods ...]
      override def setup(context: Context) {
        log.warning("Doing per-worker setup")
      }
}

Now the actual problem:

Context is defined as a nested class in the class org.apache.hadoop.mapreduce.Mapper :

        public static class Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>   {
    //[... some other methods ...]
protected void setup(org.apache.hadoop.mapreduce.Mapper<KEYIN,VALUEIN,KEYOUT,VALUEOUT>.Context context) throws java.io.IOException, java.lang.InterruptedException { /* compiled code */ }
        public class Context extends org.apache.hadoop.mapreduce.MapContext<KEYIN,VALUEIN,KEYOUT,VALUEOUT> {

        public Context(org.apache.hadoop.conf.Configuration configuration, org.apache.hadoop.mapreduce.TaskAttemptID conf, org.apache.hadoop.mapreduce.RecordReader<KEYIN,VALUEIN> taskid, org.apache.hadoop.mapreduce.RecordWriter<KEYOUT,VALUEOUT> reader, org.apache.hadoop.mapreduce.OutputCommitter writer, org.apache.hadoop.mapreduce.StatusReporter committer, org.apache.hadoop.mapreduce.InputSplit reporter) throws java.io.IOException, java.lang.InterruptedException { /* compiled code */ }

        }

So, I can not describe the Scala class, where / what is the Context . If the mapper did not have any generics, I could reference the Context via

Mapper#Context

but how can I say that mapper has generics?

Mapper[_,_,_,_]#Context

... does not work.

+5
source share
2 answers

You must specify the exact base type for the projection of your type, in your case

Mapper[Key, Entity, NullWritable, NullWritable]#Context

setup

override def setup(context: Mapper[Key, Entity, NullWritable, NullWritable]#Context)

,

class TestMapper extends AppEngineMapper[Key, Entity, NullWritable, NullWritable] {

  type Context = Mapper[Key, Entity, NullWritable, NullWritable]#Context

  override def setup(context: Context) = {
      // ...
   }
}

, , :

trait SMapper[A,B,C,D] extends Mapper[A,B,C,D] {
  type Context = Mapper[A,B,C,D]#Context
}

class TestMapper extends AppEngineMapper[Key, Entity, NullWritable, NullWritable]
                    with SMapper[Key, Entity, NullWritable, NullWritable] {
  override def setup(context: Context) = {
     // ...
  }
}

:

class TestMapper extends SMapper[Key, Entity, NullWritable, NullWritable] {
  override def setup(context: Context) = {
     // ...
  }
}
+9

, , ​​ Scala ().

0

All Articles