I currently am creating a python api for an apache spark framework and would like to use the underlying java or scala code I already have written to drive most of my python. The issue is that I need python lambda expressions that can be converted to java or scala so that they can be re-used within my existing framework. Is there any way to do this using py4j?
Py4J supports implementing Java interfaces from Python - see here.
So you need to create Python classes that implement the interfaces in java.util.function. For example:
class Function:
def __init__(self, gateway, lambda_function):
self.gateway = gateway
self.lambda_function = lambda_function
def apply(self, arg):
return self.lambda_function(arg)
class Java:
implements = ["java.util.function.Function"]
As an example of how to use it - suppose you have a Java method that takes a Function<Integer, String>:
public class PythonLambdasExample {
public static String callFunction(Function<Integer, String> function) {
return function.apply(42);
}
}
You can invoke it from Python like this:
>>> l = lambda i : "The number was %d" % i
>>> function = Function(gateway, l)
>>> result = gateway.jvm.PythonLambdasExample.callFunction(function)
>>> print(result)
The number was 42
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With