pig如何示例化udf对象

tkclm6bt  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(313)

有人能告诉我pig是如何示例化udf对象的吗?我用pig构建了一个管道来处理一些数据。我在多节点中部署了管道 Hadoop cluster和我希望保存管道中每个步骤之后生成的所有中间结果。所以我用java编写了一个udf,它将在初始化时打开一个http连接,并在中传输数据 exec . 另外,我将关闭中的连接 finalize 对象的名称。
我的脚本可以简化如下:

REGISTER MyPackage.jar;
DEFINE InterStore test.InterStore('localhost', '58888');
DEFINE Clean      test.Clean();

raw = LOAD 'mydata';
cleaned = FILTER (FOREACH raw GENERATE FLATTEN(Clean(*))) BY NOT ($0 MATCHES '');
cleaned = FOREACH cleaned GENERATE FLATTEN(InterStore(*));
named = FOREACH cleaned GENERATE $1 AS LocationID, $2 AS AccessCount;
named = FOREACH named GENERATE FLATTEN(InterStore(*)) AS (LocationID, AccessCount);
grp = GROUP named BY LocationID;
grp = FOREACH grp GENERATE FLATTEN(InterStore(*)) AS (group, named:{(LocationID, AccessCount)});
sum = FOREACH grp GENERATE group AS LocationID, SUM(named.AccessCount) AS TotalAccesses;
sum = FOREACH sum GENERATE FLATTEN(InterStore(*)) AS (LocationID, TotalAccesses);
ordered = ORDER sum BY TotalAccesses DESC;
STORE ordered INTO 'result';

中间层的代码可以简化如下:

class InterStore extends EvalFunc<Tuple>{
  HttpURLConnection con;  //Avoid redundant connection establishment in exec
  public InterStore(String ip, String port) throws IOException
  {
    URL url = new URL("http://" + ip + ':' + port);
    con = (HttpURLConnection)url.openConnection();
    con.setRequestMethod("PUT");
    con.setDoOutput(true);
    con.setDoInput(true);
  }
  public Tuple exec(Tuple input) throws IOException
  {
    con.getOutputStream().write((input.toDelimitedString(",")+'\n').getBytes());
    return input;
  }
  @Override
  protected void finalize() throws Throwable
  {
    con.getOutputStream().close();
    int respcode = con.getResponseCode();
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    System.out.printf("Resp Code:%d, %s\n", respcode, in.readLine());
    in.close();
  }
}

但是,我发现http连接不能像在本地模式下那样成功地传输数据。怎么处理?

w9apscun

w9apscun1#

是否有服务监听“localhost”、“58888”?
请注意,本地主机因每个执行节点而异,您可能需要执行以下操作:

%default LHOST `localhost`

并将此变量用作参数

DEFINE InterStore test.InterStore('$LHOST', '58888');

一般来说,我会在udf中做一些打印输出,并仔细检查传递给它的参数,然后测试连接(比如ping和检查端口是否可以从hadoop节点访问)

相关问题