Search code examples
javaapache-pignetbeans-8ubuntu-16.04eclipse-neon

Unable to run Pig Embbeded Java Program in Ubuntu


recently I do research on Apache Pig and I'm try to build Pig-Embedded Java Program. I found and copied an example from a website (From https://acadgild.com/blog/embedding-pig-java/) and I try to compile it before I run.

javac pig_java.java

The compilation is successful without prompt any error. However when I follow the instruction in https://wiki.apache.org/pig/EmbeddedPig and run the following command:

java -cp /pigjar/pig.jar pig_embbed.pig_java

it show:

Error: Could not find or load main class pig_embbed.pig_java

Do anyone face this kind of situation before? :'(

Source code:

package pig_embbed;

import java.util.Properties;
import org.apache.pig.ExecType;
import org.apache.pig.PigServer;

public class pig_java {
    public static void main(String[] args) {
        try { 
            PigServer pigServer = new PigServer(ExecType.MAPREDUCE); 
            runQuery(pigServer);
            Properties props = new Properties(); 
            props.setProperty("fs.default.name",
    "hdfs://<hdfs_url>:<hdfs_port>"); 
    }catch(Exception e) {
        e.printStackTrace();
        }
    }

public static void runQuery(PigServer pigServer) { 
    try {        
        pigServer.registerQuery("input1 = LOAD '/user/centos7/EA/test.txt' as (line:chararray);");       
        pigServer.registerQuery("words = foreach input1 generate FLATTEN(TOKENIZE(line)) as word;");         
        pigServer.registerQuery("word_groups = group words by word;");       
        pigServer.registerQuery("word_count = foreach word_groups generate group, COUNT(words);");       
        pigServer.registerQuery("ordered_word_count = order word_count by group desc;");         
        pigServer.registerQuery("store ordered_word_count into '/user/EA/test_output';");        
    } catch(Exception e) {       
        e.printStackTrace();         
        }        
    }
}

Solution

  • The classpath is wrong. In Linux, a path starting with / is absolute, i.e. from the root directory. I assume you meant -cp pigjar/pig.jar. You also forgot to include the current directory ., which contains your code; this is included automatically in the classpath, but only if you don't actually have a -cp argument or CLASSPATH environment variable. The missing . is what caused this error message, but you need both changes:

    java -cp pigjar/pig.jar:. pig_embbed.pig_java