如何在Hadoop上的Apache Pig中基于两个子包构建一个超级包

时间:2015-09-15 23:11:06

标签: hadoop apache-pig

假设我有两个包,B1和B2,并想知道如何制作一个包含两个包的超级包?我希望有一个包含两个子包的超级包的目的是因为我想调用datafu的UDF SetDifference,这似乎是在包含两个包的关系上调用的?

在我的情况下,我已经有两个包,B1和B2。我想我需要在这个样本中组装一个超级包“输入”。

http://datafu.incubator.apache.org/docs/datafu/guide/set-operations.html

differenced = FOREACH input {
  -- input bags must be sorted
  sorted_b1 = ORDER B1 by val;
  sorted_b2 = ORDER B2 by val;
  GENERATE SetDifference(sorted_b1,sorted_b2);
}

更新

这是我的代码和相关的错误消息,如果有人有任何好的想法,那就太棒了。

register datafu-1.2.0.jar;
define setDifference datafu.pig.sets.SetDifference();

-- input1.txt: {(3),(4),(1),(2),(7),(5),(6)}
-- input2.txt: {(1),(3),(5),(12)}
A = load 'input1.txt' AS (B1:bag{T:tuple(val:int)});
B = load 'input2.txt' AS (B1:bag{T:tuple(val:int)});

sorted_b1 = ORDER A by val;
sorted_b2 = ORDER B by val;
differenced = setDifference(sorted_b1,sorted_b2);

-- expected produces: ({(2),(4),(6),(7)})
DUMP differenced;

[main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1200: <file TestDataFu3.pig, line 11> Cannot expand macro 'setDifference'. Reason: Macro must be defined before expansion.
提前谢谢, 林

1 个答案:

答案 0 :(得分:3)

好的,我看到你在问什么;你的行李有不同的档案。您需要导入然后加入它们才能使它们具有相同的关系。

<强>脚本

REGISTER /path/to/jars/datafu-1.2.0.jar;
DEFINE SetDifference datafu.pig.sets.SetDifference();

data1 = LOAD 'input1' AS (B1:bag{T1:tuple(val1:int)});
data2 = LOAD 'input2' AS (B2:bag{T2:tuple(val2:int)});
A = JOIN data1 BY 1, data2 BY 1;
diff = FOREACH A {
  S1 = ORDER B1 BY val1;
  S2 = ORDER B2 BY val2;
  GENERATE SetDifference(S1, S2);
};
DUMP A;

<强>输出

({(2),(4),(6),(7)})

希望这有帮助。