java项目中,执行python命令或者文件


import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

public class PyCaller {


    public static void main(String[] args) throws IOException , InterruptedException {
        String exe = "python";
        String command = "D:\\calculator_simple.py";
        String num1 = "1";
        String num2 = "2";
        String[] cmdArr = new String[] {exe, command, num1, num2};
        Process process = Runtime.getRuntime().exec(cmdArr);
        InputStream is = process.getInputStream();
        DataInputStream dis = new DataInputStream(is);
        String str = dis.readLine();
        process.waitFor();
        System.out.println(str);
    }
}

 

在 Flink Java 项目中运行 Python 代码可以使用 PyFlink。PyFlink 是 Flink 的 Python API,它提供了一种简单的方式来编写 Flink Job,而无需编写 Java 或 Scala 代码。 以下是一些使用 PyFlink 运行 Python 代码的步骤: 1. 安装 PyFlink:可以使用 pip 命令安装 PyFlink。 ``` pip install apache-flink ``` 2. 编写 Python 代码:编写你的 PyFlink 代码。例如: ```python from pyflink.table import * from pyflink.table.descriptors import FileSystem, OldCsv, Schema t_env = BatchTableEnvironment.create() t_env.connect(FileSystem().path('/path/to/your/file')) \ .with_format(OldCsv() .field('name', DataTypes.STRING()) .field('age', DataTypes.INT())) \ .with_schema(Schema() .field('name', DataTypes.STRING()) .field('age', DataTypes.INT())) \ .create_temporary_table('mySource') t_env.connect(FileSystem().path('/path/to/your/output')) \ .with_format(OldCsv() .field('name', DataTypes.STRING()) .field('age', DataTypes.INT())) \ .with_schema(Schema() .field('name', DataTypes.STRING()) .field('age', DataTypes.INT())) \ .create_temporary_table('mySink') t_env.from_path('mySource') \ .filter("age > 18") \ .insert_into('mySink') t_env.execute("MyPyFlinkJob") ``` 3. 运行 Python 代码:在 Flink 集群上运行 Python 代码。可以使用以下命令提交 PyFlink 作业: ``` bin/flink run -p <parallelism> -py <path/to/your/python/file> <path/to/your/flink/job/config/file> ``` 其中,`<parallelism>` 是作业的并行度,`<path/to/your/python/file>` 是你的 Python 代码的路径,`<path/to/your/flink/job/config/file>` 是你的 Flink 配置文件的路径。 这些步骤可以帮助你在 Flink Java 项目中运行 Python 代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值