在java map reduce中执行python脚本

jtjikinw  于 2021-06-04  发布在  Hadoop
关注(0)|答案(1)|浏览(338)

我需要在javamapreduce程序中执行python脚本。
在mapper类中,我需要执行python命令:

python methratio.py --ref=../refernce/referncefile -r -g --out=Ouputfile ./Inputfile

这里inputfile是hdfs中的输入文件,outputfile(hdfs中)是python脚本写入输出的地方。
我可以使用process builder或其他更好的选项吗??

yacmzcpb

yacmzcpb1#

我不知道这是否对您有帮助,但您可以用这种方式在java中执行系统命令:

public static void main(String[] args) throws IOException {
        String s = null;
        String command = "python <your_command>";
        Process p = Runtime.getRuntime().exec(command);

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

    // read the output from the command
    System.out.println("Here is the standard output of the command:\n");
    while ((s = stdInput.readLine()) != null) {
        System.out.println(s);
    }
}

您可以在中看到一个详细的示例http://alvinalexander.com/java/edu/pj/pj010016
希望这对你有帮助

相关问题