Search code examples
javaapache-pigsql-execution-plan

run pig explain command over the entire script in java


I am trying to find the way to run the explain command over the entire pig script in java. I was using PigServer but it offers only to do explain over the single query (alias) not the entire script. Is there a way to do something like:

$ pig -x local -e 'explain -script Temp1/TPC_test.pig -out explain-out9.txt'

but from my Java code?


Solution

  • You may use PigRunner for this purpose.
    E.g:

    import org.apache.pig.PigRunner;
    import org.apache.pig.tools.pigstats.PigStats;
    
    public class PigTest {
    
        public static void main(String[] args) throws Exception {
    
            args = new String [] {
                    "-x", "local",
                    "-e", "explain -script Temp1/TPC_test.pig -out explain-out9.txt"
            };
    
            PigStats stats = PigRunner.run(args, null);
            //print plan:
            //stats.getJobGraph().explain(System.out, "text", true);
        }
    
    }
    

    I found that the following runtime dependencies are needed to avoid NoClassDefFoundError: