2014-06-10 54 views
2

如何重命名袋子中的字段。我已經found投射領域的方式,但我不能重命名他們。如何重命名袋子中的字段

例如:

renamed = FOREACH named GENERATE 
    field0, 
    elements.(
     field1, 
     field2 
    ); 

如果我嘗試以下方法,我得到了一個錯誤:

renamed = FOREACH named GENERATE 
    field0, 
    elements.(
     field1 AS renamed1, 
     field2 
    ); 

錯誤消息是以下

2014-06-10 08:34:14,701 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1200: <line 233, column 16> mismatched input 'AS' expecting RIGHT_PAREN 
2014-06-10 08:34:14,702 [main] WARN org.apache.pig.tools.grunt.Grunt - There is no log file to write to. 
2014-06-10 08:34:14,702 [main] ERROR org.apache.pig.tools.grunt.Grunt - Failed to parse: <line 233, column 16> mismatched input 'AS' expecting RIGHT_PAREN 
    at org.apache.pig.parser.QueryParserDriver.parse(QueryParserDriver.java:241) 
    at org.apache.pig.parser.QueryParserDriver.parse(QueryParserDriver.java:179) 
    at org.apache.pig.PigServer$Graph.validateQuery(PigServer.java:1648) 
    at org.apache.pig.PigServer$Graph.registerQuery(PigServer.java:1621) 
    at org.apache.pig.PigServer.registerQuery(PigServer.java:575) 
    at org.apache.pig.tools.grunt.GruntParser.processPig(GruntParser.java:1093) 
    at org.apache.pig.tools.pigscript.parser.PigScriptParser.parse(PigScriptParser.java:501) 
    at org.apache.pig.tools.grunt.GruntParser.parseStopOnError(GruntParser.java:198) 
    at org.apache.pig.tools.grunt.GruntParser.parseStopOnError(GruntParser.java:173) 
    at org.apache.pig.tools.grunt.Grunt.run(Grunt.java:69) 
    at org.apache.pig.Main.run(Main.java:541) 
    at org.apache.pig.Main.main(Main.java:156) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.apache.hadoop.util.RunJar.main(RunJar.java:212) 

回答

2

你需要把AS條款在您定義袋子後。試試這個:

renamed = FOREACH named GENERATE 
    field0, 
    elements.(
     field1 
     field2 
    ) AS bagName:{(renamed1, renamed2)};