编程语言
首页 > 编程语言> > 使用py4j 实现python与java 的互调用

使用py4j 实现python与java 的互调用

作者:互联网

py4j 是基于rpc 模式的python与Java 调用的处理模式,功能还是很强大的,而且pyspark 就是基于此的

参考使用

主要提供server 能力
pom.xml

 
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.dalong</groupId>
    <artifactId>py4j-learning</artifactId>
    <version>1.0-SNAPSHOT</version>
 
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>net.sf.py4j</groupId>
            <artifactId>py4j</artifactId>
            <version>0.10.9.5</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>myinstance-app</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <!-- Run shade goal on package phase -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <!-- add Main-Class to manifest file -->
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.dalong.StackEntryPoint</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

入口

package com.dalong;
 
import py4j.GatewayServer;
 
public class StackEntryPoint {
    private Stack stack;
 
    public StackEntryPoint() {
        stack = new Stack();
        stack.push("Initial Item");
    }
 
    public Stack getStack() {
        return stack;
    }
 
    public static void main(String[] args) {
        //  StackEntryPoint 为暴露的服务
        GatewayServer gatewayServer = new GatewayServer(new StackEntryPoint(),11111);
        gatewayServer.start();
        System.out.println("Gateway Server Started");
    }
 
}

推荐基于venv 运行处理

from py4j.java_gateway import JavaGateway, GatewayParameters
// 注意访问端口,需要和暴露的一致
gateway = JavaGateway(gateway_parameters=GatewayParameters(port=11111))
 
stack = gateway.entry_point.getStack()
random = gateway.jvm.java.util.Random()
value = random.nextInt(10)
stack.push("First %s" % ('item'))
 
item = stack.pop()
 
print(item)
print(value)
mvn clean pacakge 
java  -jar target/myinstance-app.jar 
python app.py 

效果

 

 

说明

以上是一个简单的试用,实际上py4j 官方文档以及提供的能力是很多的,还是很强大的,默认模式没有安全控制,但是我们可以添加,同时也是可以支持链接python服务的
对于auth 可以使用提供的builder,如下

 
 GatewayServer server = new GatewayServer.GatewayServerBuilder().entryPoint(
                new StackEntryPoint()).authToken("HelloWorld").javaPort(11111).build();
        server.start();

python 调用

gateway = JavaGateway(gateway_parameters=GatewayParameters(port=11111,auth_token="HelloWorld"))

参考资料

https://www.py4j.org/
https://github.com/py4j/py4j
https://github.com/ninia/jep
https://github.com/rongfengliang/py4j-learning.git

标签:py4j,java,python,StackEntryPoint,stack,com,gateway
来源: https://www.cnblogs.com/rongfengliang/p/16535741.html