在Java中获取Pig脚本输出

时间:2016-02-04 06:15:28

标签: java shell apache-pig

我可以将存储输出的pig脚本的输出加载到Java 中的Pig存储中吗?

我所指的猪脚本的最后一行是 -

public void SomeMethodThatAcceptsEmployee(Employee emp)
{
}

Employee employee = new Employee();
Manager manager = new Manager();

// You can call this method passing Employee:
SomeMethodThatAcceptsEmployee(employee);
// But you can also call this method passing Manager:
SomeMethodThatAcceptsEmployee(manager);

想要在java中导入关系,以便在UI中进一步处理和渲染

如果是,我该怎么做?

1 个答案:

答案 0 :(得分:0)

是的,你可以做到。使用Embedded Pig concept,它提供PigServer,它有助于在java程序中运行pigcript。

import java.io.IOException;
import org.apache.pig.PigServer;

public class WordCount {
   public static void main(String[] args) {
      PigServer pigServer = new PigServer();
      try {
         pigServer.registerJar("/mylocation/tokenize.jar");
         runMyQuery(pigServer, "myinput.txt";
        } 
      catch (IOException e) {
         e.printStackTrace();
        }
   }

   public static void runMyQuery(PigServer pigServer, String inputFile) throws IOException {        
       pigServer.registerQuery("A = load '" + inputFile + "' using TextLoader();");
       pigServer.registerQuery("B = foreach A generate flatten(tokenize($0));");
       pigServer.registerQuery("C = group B by $1;");
       pigServer.registerQuery("D = foreach C generate flatten(group), COUNT(B.$0);");

       pigServer.store("D", "myoutput");
   }
}

以下是更多示例:http://www.programcreek.com/java-api-examples/index.php?api=org.apache.pig.PigServer