Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a Python object to Java using Py4j

Tags:

java

python

py4j

I'm trying to extend the example from this tutorial by sending Python objects to Java. While the example code which exchanges String objects between Python and Java works fine, when I try to replace it with my own Python object (Event), an error regarding object_id is displayed.

Python Code:

class Event(object):
   #some content here

stack = gateway.entry_point.getStack()
event = Event()

stack.push(event)

Error:

Traceback (most recent call last):
  File "/home/******/src/py4jSample.py", line 19, in <module>
   stack.push(event)
  File "/usr/local/lib/python2.7/dist-packages/py4j-0.7-py2.7.egg/py4j/java_gateway.py", line 423, in __call__
    [get_command_part(arg, self.pool) for arg in new_args])
  File "/usr/local/lib/python2.7/dist-packages/py4j-0.7-py2.7.egg/py4j/protocol.py", line 241, in get_command_part
    command_part = REFERENCE_TYPE + parameter._get_object_id()
AttributeError: 'Event' object has no attribute '_get_object_id'

Any idea how this can be solved?

like image 363
Sudhi Pulla Avatar asked Jan 22 '13 01:01

Sudhi Pulla


People also ask

What is py4j Java_gateway?

The py4j. java_gateway module defines most of the classes that are needed to use Py4J. Py4J users are expected to only use explicitly JavaGateway and optionally, GatewayParameters , CallbackServerParameters , java_import , get_field , get_method , launch_gateway , and is_instance_of .

What is the use of py4j?

Py4J enables Python programs running in a Python interpreter to dynamically access Java objects in a Java Virtual Machine. Methods are called as if the Java objects resided in the Python interpreter and Java collections can be accessed through standard Python collection methods.

What is py4j PySpark?

Py4J is a popular library which is integrated within PySpark and allows python to dynamically interface with JVM objects. PySpark features quite a few libraries for writing efficient programs.


1 Answers

the problem is that you cannot send pure Python objects to the Java side (in this case, calling push actually calls the Java method "Stack.push"). You can only send (1) objects that can be automatically converted to Java objects (primitives such as int, byte array, strings), (2) objects received from Java such as "stack", or (3) Python objects implementing a Java interface:

class Event(object):
    def myMethod(param1, param2):
        return "foo"

    class Java:
        implements = ['yourpackage.IEvent']

If you want to send Python objects implementing a Java interface, you need to accept incoming connections from the Python interpreter (the JVM will call back the Python interpreter if a Python method is called):

gateway = JavaGateway(start_callback_server=True)
stack = gateway.entry_point.getStack()
event = Event()
stack.push(event)
like image 57
Barthelemy Avatar answered Oct 17 '22 20:10

Barthelemy