Advanced Topics
===============
Accessing Java collections and arrays from Python
-------------------------------------------------
Java collections are automatically mapped to Python collections so that
standard Python operations such as slicing work on Java collections. Here is
the mapping of the collection:
=================== ====================== ==========================================================
Java Collection Python Collection Py4J Implementation
=================== ====================== ==========================================================
Array Sequence [#arraynote]_ :class:`JavaArray <py4j.java_collections.JavaArray>`
java.util.List MutableSequence :class:`JavaList <py4j.java_collections.JavaList>`
java.util.Set MutableSet :class:`JavaSet <py4j.java_collections.JavaSet>`
java.util.Map MutableMapping :class:`JavaMap <py4j.java_collections.JavaMap>`
java.util.Iterator *Iterator Protocol* :class:`JavaIterator <py4j.java_collections.JavaIterator>`
=================== ====================== ==========================================================
.. [#arraynote] Py4J allows elements to be modified (like a real Java array), which is not the case of true
immutable sequences like tuples.
Java methods are still accessible when using the Python version of a Java
collection. Here are some usage examples for each collection class. These
examples do not cover the entire API.
It is possible to change this default mapping by registering an output
converter with :func:`register_output_converter
<py4j.protocol.register_output_converter>`
Array
^^^^^
::
>>> gateway = JavaGateway()
>>> int_class = gateway.jvm.int
>>> int_array = gateway.new_array(int_class,2)
>>> int_array[0] = 1
>>> int_array[1] = 2
>>> int_array[0]
1
>>> int_array[2]
Traceback (most recent call last):
...
IndexError: list index out of range
>>> for i in int_array:
... print(i)
...
1
2
>>> sarray = gateway.new_array(gateway.jvm.java.lang.String,2,3)
>>> len(sarray)
2
>>> len(sarray[0])
3
>>> sarray[0][1] = 'hello'
>>> sarray[0][1]
u'hello'
>>> sarray[0][0] == None
True
List
^^^^
::
>>> l = gateway.jvm.java.util.ArrayList()
>>> l.append(1) # calling Python interface
>>> l.add('hello') # calling Java interface
>>> for elem in l:
... print elem
...
1
hello
>>> l[0] = 2
>>> l.append(3)
>>> str(l)
"[2, u'hello', 3]"
>>> l2 = l[0:-1]
>>> l2[0] = 999
>>> l
[2, u'hello', 3]
>>> l2 # l2 is a copy of l and not a view so a change in l2 does not affect l
[999, u'hello']
>>> del(l[0])
>>> l
[u'hello', 3]
Set
^^^
::
>>> s = gateway.jvm.java.util.HashSet()
>>> s.add(1)
>>> s.add('hello')
>>> s
set([1, u'hello'])
>>> 1 in s
True
>>> s.remove(u'hello')
>>> s
set([1])
Map
^^^
::
>>> m = gateway.jvm.java.util.HashMap()
>>> m["a"] = 0
>>> m.put("b",1)
>>> m
{u'a': 0, u'b': 1}
>>> u"b" in m
True
>>> del(m["a"])
>>> m
{u'b': 1}
>>> m["c"] = 2
>>> for key in m:
... print("%s:%i" % (key,m[key]))
...
b:1
c:2
Byte array (byte[])
-------------------
Since version 0.7, Py4J automatically passes Java byte array (i.e., `byte[]`)
by value and convert them to Python bytearray (2.x) or bytes (3.x) and vice
versa. The rationale is that byte array are often used for binary processing
and are often immutable: a program reads a series of byte from a data source
and interpret it (or transform it into another byte array).
Prior to 0.7, a Python program could interact with a Java byte[] but each
access to a byte required a call between the Python and the Java interpreter
(i.e., the Python program only had a reference to the array).
In summary:
* If from Java, you return a byte[], Py4J will convert the byte[] to a
bytearray (Python 2.x) or bytes (Python 3.x) variable in Python.
* If from Python, you pass a bytearray or bytes variable to the Java side,
Py4J will convert it to a byte[].
* If you pass a byte[] from Java to Python, both arrays are disconnected: a
change to the array on one side will not be reflected on the other side.
* If you want to pass an array by reference, use a `Byte[]` instead of a
`byte[]` or you can use `Bytes.asList
<http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/primitives/Bytes.html#asList(byte...)>`_
from `Guava <http://code.google.com/p/guava-libraries/>`_ to obtain a list
backed by a byte array.
Implementing Java interfaces from Python (callback)
---------------------------------------------------
Since version 0.3, Py4J allows Python classes to implement Java interfaces so
that the JVM can call back Python objects. In the following example, a Python
class implements a Java listener interface.
Here is the code of listener interface:
.. code-block:: java
// py4j/examples/ExampleListener.java
package py4j.examples;
public interface ExampleListener {
Object notify(Object source);
}
Here is the code of the main Java application. The program has a main method
starting a `GatewayServer`. The entry point, a `ListenerApplication` instance,
has two methods, one to register listeners, the other to notify all listeners.
Start this program before running the Python code below.
.. code-block:: java
// py4j/examples/ListenerApplication.java
package py4j.examples;
import py4j.GatewayServer;
import java.util.ArrayList;
import java.util.List;
public class ListenerApplication {
List<ExampleListener> listeners = new ArrayList<ExampleListener>();
public void registerListener(ExampleListener listener) {
listeners.add(listener);
}
public void notifyAllListeners() {
for (ExampleListener listener: listeners) {
Object returnValue = listener.notify(this);
System.out.println(returnValue);
}
}
@Override
public String toString() {
return "<ListenerApplication> instance";
}
public static void main(String[] args) {
ListenerApplication application = new ListenerApplication();
GatewayServer server = new GatewayServer(application);
server.start(true);
}
}
Here is the Python program that implements the Java interface and calls the
ListenerApplication to notify all listeners. Then multiple exchanges between
Java and Python occurs (e.g., Python calls System.out.println on the Java
side).
::
from py4j.java_gateway import JavaGateway, CallbackServerParameters
class PythonListener(object):
def __init__(self, gateway):
self.gateway = gateway
def notify(self, obj):
print("Notified by Java")
print(obj)
gateway.jvm.System.out.println("Hello from python!")
return "A Return Value"
class Java:
implements = ["py4j.examples.ExampleListener"]
if __name__ == "__main__":
gateway = JavaGateway(
callback_server_parameters=CallbackServerParameters())
listener = PythonListener(gateway)
gateway.entry_point.registerListener(listener)
gateway.entry_point.notifyAllListeners()
gateway.shutdown()
The `PythonListener` class is a standard Python class that has one method,
`notify`. The signature of the method contains one parameter. When interfaces
contain overloaded methods, the python method must accept all possible
combinations of parameters (with `*args` and `**kwargs` or with default
parameters).
Py4J recognizes that the `PythonListener` class implements a Java interface
because it declares an internal class called `Java`, which has a member named
`implements`. This member is a list of string representing the fully qualified
name of implemented Java interfaces.
Finally, the Python program contains a main method that starts a gateway,
initializes a PythonListener instance and registers it to the
`Listene
没有合适的资源?快使用搜索试试~ 我知道了~
zeppelin-0.8.0-bin-all.tgz
需积分: 9 1 下载量 4 浏览量
2021-10-12
09:03:39
上传
评论
收藏 938.91MB TGZ 举报
温馨提示
共2298个文件
jar:1177个
html:655个
java:104个
zeppelin-0.8.0-bin-all.tgz
资源详情
资源评论
资源推荐
收起资源包目录
zeppelin-0.8.0-bin-all.tgz (2298个子文件)
LICENSE-azure-storage-4.0.0 11KB
LICENSE-fontawesome-4.2.0 4KB
LICENSE-jtransforms-2.4.0 2KB
LICENSE-jsdiff-3.3.0 2KB
LICENSE-highlightjs-9.4.0 1KB
LICENSE-scalamacros-paradise-2.1.0 1KB
LICENSE-scalamacros-quasiquotes-2.1.0 1KB
LICENSE-minilog-1.3.0 1KB
LICENSE-sbt-test-interface-1.0 1KB
LICENSE-UI-Bootstrap-0.13.0 1KB
LICENSE-github-markdown-css-2.4.0 1KB
LICENSE-es5-shim-3.1.0 1KB
LICENSE-ngToast-2.0.0 1KB
LICENSE-AngularJS-1.5.0 1KB
LICENSE-bootstrap-3.2.0 1KB
LICENSE-jekyll-1.3.0 1KB
LICENSE-simple_line_icons-1.0.0 1KB
LICENSE-fontawesome-style-4.2.0 1KB
LICENSE-aws-java-sdk-1.10.1 10KB
LICENSE-dom4j-1.6.1 2KB
LICENSE-UI.ace-0.1.1 1KB
LICENSE-jsoup-1.6.1 1KB
LICENSE-Json3-3.3.1 1KB
LICENSE-angular-elastic-input-2.0.1 1KB
LICENSE-lunrjs-0.7.1 1KB
LICENSE-nvd3.js-1.7.1 566B
LICENSE-scala-2.10 1KB
LICENSE-slf4j-1.7.10 1KB
LICENSE-slf4j-log4j12-1.7.10 1KB
LICENSE-java-lsh-0.10 1KB
LICENSE-junit-interface-0.11 1KB
LICENSE-java-string-similarity-0.12 1KB
LICENSE-angular-websocket-1.0.13 1KB
LICENSE-jquery.scrollTo-1.4.13 1KB
LICENSE-gson-2.2 11KB
LICENSE-bootstrap-3.0.2 10KB
LICENSE-py4j-0.9.2 1KB
LICENSE-d3-2.10.2 1KB
LICENSE-sqlline-1.0.2 1KB
LICENSE-angular-elastic-2.4.2 1KB
LICENSE-slf4j-simple-1.7.21 1KB
LICENSE-kryo-shaded-3.0.3 1KB
LICENSE-lodash-3.9.3 1KB
LICENSE-jekyll-bootstrap-0.3 1KB
LICENSE-perfect-scrollbar-0.5.4 1KB
LICENSE-angular-filter-0.5.4 1KB
LICENSE-spire-0.7.4 1KB
LICENSE-spire-macros-0.7.4 1KB
LICENSE-ngInfiniteScroll-1.3.4 1KB
LICENSE-bcprov-jdk15on-1.51 1KB
LICENSE-rscala-1.0.6 1KB
LICENSE-ng-sortable-1.3.6 1KB
LICENSE-angular-dragdrop-1.0.8 1KB
LICENSE-angular-xeditable-0.1.8 1KB
LICENSE-ace-builds-1.1.9 1KB
LICENSE-ace-1.1.9 1KB
AnIndex 25KB
.buildinfo 230B
setup.cfg 88B
nose.cfg 24B
interpreter.cmd 5KB
zeppelin.cmd 3KB
common.cmd 3KB
functions.cmd 1KB
stylesheet.css 13KB
stylesheet.css 13KB
basic.css 9KB
py4jdoc.css 6KB
pygments.css 4KB
R.css 1KB
DEPENDENCIES 31KB
DEPENDENCIES 28KB
DEPENDENCIES 20KB
DEPENDENCIES 18KB
DEPENDENCIES 17KB
DEPENDENCIES 16KB
DEPENDENCIES 11KB
DEPENDENCIES 10KB
DEPENDENCIES 10KB
DEPENDENCIES 9KB
DEPENDENCIES 6KB
DEPENDENCIES 6KB
DEPENDENCIES 5KB
DEPENDENCIES 4KB
DEPENDENCIES 4KB
DEPENDENCIES 4KB
DEPENDENCIES 2KB
DEPENDENCIES 2KB
DEPENDENCIES 1KB
DEPENDENCIES 1KB
DEPENDENCIES 879B
DEPENDENCIES 872B
DESCRIPTION 2KB
ajax-loader.gif 673B
HTTP.groovy 9KB
00Index.html 82KB
Protocol.html 72KB
Protocol.html 72KB
py4j_java_gateway.html 65KB
advanced_topics.html 64KB
共 2298 条
- 1
- 2
- 3
- 4
- 5
- 6
- 23
草原上的浪
- 粉丝: 0
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0