You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pig.apache.org by Dan Yi <dy...@medio.com> on 2013/01/24 07:07:09 UTC

execute pig command in Java program

Hi, 

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:

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. So what might be the problem?

Thanks.


Re: execute pig command in Java program

Posted by Cheolsoo Park <ch...@cloudera.com>.
Hi Dan,

1. Can't you print out the error messages by calling getErrorStream() on
the sub-process?
2. Is there any reason why you spawn a sub-process process rather than use
PigServer API?

Thanks,
Cheolsoo


On Wed, Jan 23, 2013 at 10:07 PM, Dan Yi <dy...@medio.com> wrote:

> Hi,
>
> 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:
>
> 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. So what might be the problem?
>
> Thanks.
>
>