在Pig中压扁内袋

时间:2014-08-26 20:27:47

标签: apache-pig flatten

我有这个:

(1,{(1,2,3)})

(4,{(4,2,1),(4,3,3)})

(8,{(8,3,4)})

我想要这个:

(1,1,2,3)

(4,4,2,1)

(4,4,3,3)

(8,8,3,4)

我觉得这篇文章很有帮助,但无法让它发挥作用: How to flatten a group into a single tuple in Pig? 这似乎很容易,但我是猪新手。 提前谢谢!

编辑回答reo的问题(谢谢reo):

在发布SO之前,我试图在我的问题上压扁这个包,只是为了得到以下错误(在我的例子中d是C):

    ERROR 2117: Unexpected error when launching map reduce job.

org.apache.pig.impl.logicalLayer.FrontendException: ERROR 1066: Unable to open iterator for alias d
        at org.apache.pig.PigServer.openIterator(PigServer.java:857)
        at org.apache.pig.tools.grunt.GruntParser.processDump(GruntParser.java:682)
        at org.apache.pig.tools.pigscript.parser.PigScriptParser.parse(PigScriptParser.java:303)
        at org.apache.pig.tools.grunt.GruntParser.parseStopOnError(GruntParser.java:189)
        at org.apache.pig.tools.grunt.GruntParser.parseStopOnError(GruntParser.java:165)
        at org.apache.pig.tools.grunt.Grunt.run(Grunt.java:69)
        at org.apache.pig.Main.run(Main.java:490)
        at org.apache.pig.Main.main(Main.java:111)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.hadoop.util.RunJar.main(RunJar.java:197)
Caused by: org.apache.pig.PigException: ERROR 1002: Unable to store alias d
        at org.apache.pig.PigServer.storeEx(PigServer.java:956)
        at org.apache.pig.PigServer.store(PigServer.java:919)
        at org.apache.pig.PigServer.openIterator(PigServer.java:832)
        ... 12 more
Caused by: org.apache.pig.backend.executionengine.ExecException: ERROR 2117: Unexpected error when launching map reduce job.
        at org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.MapReduceLauncher.launchPig(MapReduceLauncher.java:322)
        at org.apache.pig.PigServer.launchPlan(PigServer.java:1270)
        at org.apache.pig.PigServer.executeCompiledLogicalPlan(PigServer.java:1255)
        at org.apache.pig.PigServer.storeEx(PigServer.java:952)
        ... 14 more
Caused by: java.lang.RuntimeException: Could not resolve error that occured when launching map reduce job: java.lang.NullPointerException
        at org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.PigOutputFormat.checkOutputSpecsHelper(PigOutputFormat.java:193)
        at org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.PigOutputFormat.checkOutputSpecs(PigOutputFormat.java:187)
        at org.apache.hadoop.mapred.JobClient$2.run(JobClient.java:937)
        at org.apache.hadoop.mapred.JobClient$2.run(JobClient.java:896)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:396)
        at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1117)
        at org.apache.hadoop.mapred.JobClient.submitJobInternal(JobClient.java:896)
        at org.apache.hadoop.mapred.JobClient.submitJob(JobClient.java:870)
        at org.apache.hadoop.mapred.jobcontrol.Job.submit(Job.java:378)
        at org.apache.hadoop.mapred.jobcontrol.JobControl.startReadyJobs(JobControl.java:247)
        at org.apache.hadoop.mapred.jobcontrol.JobControl.run(JobControl.java:279)
        at java.lang.Thread.run(Thread.java:662)
        at org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.MapReduceLauncher$1.run(MapReduceLauncher.java:260)

        at org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.MapReduceLauncher$JobControlThreadExceptionHandler.uncaughtException(MapReduceLauncher.java:631)
        at java.lang.Thread.dispatchUncaughtException(Thread.java:1874)

这是我原来的问题。但是现在我在这里使用的可重复的例子中尝试了同样的事情,它可以工作。

grunt> A = load 'my_location_on_HDFS' using PigStorage('|') as (a,b,c);
grunt> B = group A by a;
grunt> describe B;
B: {group: bytearray,A: {(a: bytearray,b: bytearray,c: bytearray)}}
grunt> dump B;
(1,{(1,2,3)})
(4,{(4,2,1),(4,3,3)})
(8,{(8,3,4)})
grunt> C = foreach B generate group, flatten(A);
grunt> dump C;
(1,1,2,3)
(4,4,2,1)
(4,4,3,3)
(8,8,3,4)

我想我必须仔细看看我的原始数据/情况。

1 个答案:

答案 0 :(得分:3)

如果你弄平了包,那么你将获得所需的输出,让我们假设你上面的输入是B,你通过分组输入A得到了这个

只做一个描述B;

Describe B;    
B: {group: bytearray,A: {()}}

如果您可以尝试类似

的话,现在A是一个包
 C= foreach B generate group, FLATTEN(A);

C将具有您想要展平行李的值将导致单独的行