1

I have the below code which uses spark and Java 8.

JavaRDD<AdLog> accessLogs = logLines.map(adLog::parseFromLogLine).cache();

I am trying to convert this to Java 7. I found alternative ways for most of the code but I couldn't find the alternative for this specific line of code. parseFromLogLine is a method inside the class AdLog of which adLog is an object. When a line of text is encountered it should call the parseFromLogLine method inside AdLog class. The method returns a new AdLog object. How can I achieve this using Java 7. Any help would be much appreciated. Thanks in advance.

2 Answers2

0

You could use Guava Functions to help you solve this problem. (https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/Functions.html)

You define it in a new class, you initialise with an AdLog object as Input and an AdLog as a result (Function<AdLog, AdLog>) and you implements the apply method that will take the Adlogobject as input and it'll return a Adlogobject. And in the apply method, you call the parseFromLogLine().

public class AdLogFunction implements Function<AdLog, AdLog> {

    @Override
    public AdLog apply(AdLog input) {
        return input.parseFromLogLine();
    }
}

Once this class done, you can use it easily within your code

DamCx
  • 1,047
  • 1
  • 11
  • 25
0

if logLines RDD is of Type AdLog, then

 logLines.map(new Function<AdLog, AdLog>() {
        @Override
        public AdLog call(AdLog adlog) throws Exception {

            return adLog.parseFromLogLine();
        });

Java 7 way in spark.

map method's parameter is of type Function, which can be passed like below in Java 7 (anonymous inner class).

rdd.map(new Function<InputType, ReturnType>() {
    @Override
    public ReturnType call(InputType s) throws Exception {
            .......
        return op;
    }
});
Ankit Jindal
  • 121
  • 4