简体   繁体   English

在Java程序中执行Pig命令

[英]execute pig command in Java program

I used to write PHP code to execute pig command, it worked well, Now I switch to Java but seems it won't work, here is my code: 我曾经写过PHP代码来执行Pig命令,它运行良好,现在我切换到Java,但似乎不起作用,这是我的代码:

String pigCommand = "pig -x local -p ouput=/tmp my_pig_script.pig";

Runtime r = Runtime.getRuntime();
Process p;
int exitVal;

try {
    p = r.exec(pigCommand);
    exitVal = p.waitFor();
    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = null;

    while((line = br.readLine()) != null) {
        System.out.println(line);
    }
    br.close();

    System.out.println("exitVal: " + exitVal);
    System.out.println("Done");

If I run the that pig command in console directly, it works, if I replace that Pig command with other shell command say 'ping www.yahoo.com', and run the java Program, it works too. 如果我直接在控制台中运行that pig命令,则可以运行,如果我用其他shell命令(例如“ ping www.yahoo.com”)替换该Pig命令,并运行Java程序,则它也可以运行。 So what might be the problem? 那么可能是什么问题呢? thanks 谢谢

You should use PigServer to execute pig scripts from java programs. 您应该使用PigServer从Java程序执行PigServer脚本。 It is more foolproof and more portable. 它更安全,更便携。

Also see this answer: https://stackoverflow.com/a/11299259/373151 另请参阅以下答案: https : //stackoverflow.com/a/11299259/373151

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM