0

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();         
        }        
    }
}
4
  • The manpage for java says the classpath defaults to .. But it doesn't say that . is added if you do specify a classpath. You want to include the current directory, because that's where the main class is: java -cp /pigjar/pig.jar:. pig_embbed.pig_java. Commented Jan 21, 2017 at 10:15
  • After I enter java -cp /pigjar/pig.jar:. pig_embbed.pig_java it show this: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pig/PigServer Commented Jan 21, 2017 at 13:00
  • Looks like you did need the ., but now that it can find pig_embbed another error has come to light: it can't find the class org.apache.pig.PigServer. I'd guess the other part of the classpath is wrong. It looks like a Linux path, but because of the leading / it's an absolute path which probably isn't what you wanted. E.g. if you're in the directory pigjar which contains pig.jar, use -cp pig.jar:.; if you're in the parent directory of pigjar it's -cp pigjar/pig.jar:. Commented Jan 21, 2017 at 18:42
  • Can you post your latest reply as answer? I want to accept it :) this is worked! Commented Jan 21, 2017 at 22:59

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.