使用py4j 实现python与java 的互调用
2022/7/31 1:25:16
本文主要是介绍使用py4j 实现python与java 的互调用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
py4j 是基于rpc 模式的python与Java 调用的处理模式,功能还是很强大的,而且pyspark 就是基于此的
参考使用
- java 项目
主要提供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");
}
}
- python调用
推荐基于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 实现python与java 的互调用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?