Reputation: 23901
I have a working understanding of Java. I understand reserved words. I also understand the basics of anonymous classes. I am reading this Spark example and see a "call" statement. What is the meaning of call
and @Override
? I see that call is not a reserved word -- but I also don't see it in the Spark docs or in the import statement. Could someone break down what is happening in this code? I get that it passes an anonymous class as a parameter (right?) -- and then that abstract class has an anonymous method called "call" (right?). But what is getting overwritten? Why the @Override
? What does call
refer to?
JavaPairRDD<String, Integer> ones = words.mapToPair(new PairFunction<String, String, Integer>() {
@Override
public Tuple2<String, Integer> call(String s) {
return new Tuple2<String, Integer>(s, 1);
}
});
Upvotes: 2
Views: 2345
Reputation: 187499
PairFunction
is an interface that has a single method named call
. What's happening here is that an implementation of this interface is being created and passed as a parameter to words.mapToPair
.
It might be a bit simpler to understand if you see an equivalent (but more verbose) way to do the same thing:
class PairFunctionImpl implements PairFunction<String, String, Integer> {
@Override
public Tuple2<String, Integer> call(String s) {
return new Tuple2<String, Integer>(s, 1);
}
}
JavaPairRDD<String, Integer> ones = words.mapToPair(new PairFunctionImpl());
The code you've shown is functionally identical to this, except implementing the interface, creating an instance, and passing this instance as a parameter to words.mapToPair
is done in a single step via an anonymous class.
The @Override
annotation tells the compiler to check that the call
method signature matches the definition in the PairFunction
interface, which has the following benefits:
In this particular case, you don't get much benefit from @Override
because you would get a compile-time error even without it (use of @Override
is always optional), but personally I always use it.
Upvotes: 2
Reputation: 311163
What you see in this snippet is the instantiation of an anonymous class that implements spark.api.java.function.PairFunction
. call
is a method in that interface that should be implemented, and @Override
signifies that this method is defined in the interface (as opposed to just adding another method to the one you're implementing).
Note: that same syntax holds for extending an abstract class, although this is not the case here.
Upvotes: 2
Reputation: 39750
You have a few questions
PairFunction
interface. You later refer to this object as an abstract class, it is not abstract either. You can't construct (call new
on) an abstract class. The class you just instantiated is concrete and not abstract. Just anonymous.call
: This is the name of a method which any class implementing the PairFunction
interface has to implement. What it actually does is a Spark question, but I gather from the java docs that it triggers the pairing function.@Override
: This is an annotation which is used to indicate that the method you are now defining overrides the implementation in a parent class. In this case you aren't actually overriding any existing implementation as only an interface exists but that's the convention. The annotation has no impact on the run-time execution. I'll refer you to this question for more details on when to use it.Upvotes: 1