One solution. You can use some kind of message system, queue or broker of some kind to serialize / deserialize or transfer messages between python and java. Then create sorters / producers / consumers to work on the queues that will be processed in python or java.
py4j is mainly used for / for pyspark and hadoop type.
To answer your question more immediately.
An example of using json-simple .:
import org.apache.commons.io.FileUtils; import org.json.simple.JSONObject; //import org.json.simple.JSONObject; public class TestObject { private double[] values; private int length; private int anotherVariable; private boolean someBool; private String someString; //getters, setters public String toJSON() { JSONObject obj=new JSONObject(); obj.put("values",new Double(this.values)); obj.put("length",new Integer(this.length)); obj.put("bool_val",new Boolean(this.SomeBool)); obj.put("string_key",this.someString); StringWriter out = new StringWriter(); obj.writeJSONString(out); return out.toString(); } public void writeObject(){ Writer writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream("anObject.json"), "utf-8") ) ) writer.write(this.toJSON()); } public static void setObject(){ values = 100.134; length = 12; anotherVariable = 15; someString = "spam"; } }
And in python:
class DoStuffWithObject(object): def __init__(self,obj): self.obj = obj self.changeObj() self.writeObj() def changeObj(self): self.obj['values'] = 100.134; self.obj['length'] = 12; self.obj['anotherVariable'] = 15; self.obj['someString'] = "spam"; def writeObj(self): ''' write back to file ''' with open('anObject.json', 'w') as f: json.dump(self.obj, f) def someOtherMethod(self, s): ''' do something else ''' print('hello {}'.format(s)) import json with open('anObject.json','r') as f: obj = json.loads(f.read())
And then in java read the object. There are solutions that implement this idea (JSON-RPC, XML-RPC and options). Depending on this, you might also want to consider something like ( http://docs.mongodb.org/ecosystem/drivers/java/ ) that mongo does json.
Cm:
A more complete list of queues:
Related Resources:
source share