我想从猪的时间戳中获取月份名称(Jan-Dec)。我用Java编写了一个UDF(我的第一个UDF),它看起来像这样:
package pig.data.preprocess;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.pig.EvalFunc;
import org.apache.pig.data.Tuple;
public class ColNameToTypeMap extends EvalFunc<String> {
@Override
public String exec(Tuple input) throws IOException {
Map<Integer, String> monthNamesMap = new HashMap<Integer, String>();
monthNamesMap.put(1, "Jan");
monthNamesMap.put(2, "Feb");
monthNamesMap.put(3, "Mar");
monthNamesMap.put(4, "Apr");
monthNamesMap.put(5, "May");
monthNamesMap.put(6, "Jun");
monthNamesMap.put(7, "Jul");
monthNamesMap.put(8, "Aug");
monthNamesMap.put(9, "Sep");
monthNamesMap.put(10, "Oct");
monthNamesMap.put(11, "Nov");
monthNamesMap.put(12, "Dec");
int monthId = (Integer)input.get(0);
return monthNamesMap.get(monthId);
}
}
以下是使用上述UDF的猪脚本:
register '<path-to-jar>/month-name.jar';
DEFINE month_name pig.data.preprocess.ColNameToTypeMap();
month_n = FOREACH only_dates GENERATE id, month_name(GetMonth(ToDate(ts)));
DUMP month_n;
当我运行此脚本时,它会给我以下异常:
ERROR 1200: could not instantiate 'pig.data.preprocess.ColNameToTypeMap' with arguments 'null'
Failed to parse: could not instantiate 'pig.data.preprocess.ColNameToTypeMap' with arguments 'null'
at org.apache.pig.parser.QueryParserDriver.parse(QueryParserDriver.java:201)
at org.apache.pig.PigServer$Graph.validateQuery(PigServer.java:1707)
at org.apache.pig.PigServer$Graph.registerQuery(PigServer.java:1680)
at org.apache.pig.PigServer.registerQuery(PigServer.java:623)
at org.apache.pig.tools.grunt.GruntParser.processPig(GruntParser.java:1082)
at org.apache.pig.tools.pigscript.parser.PigScriptParser.parse(PigScriptParser.java:505)
at org.apache.pig.tools.grunt.GruntParser.parseStopOnError(GruntParser.java:230)
at org.apache.pig.tools.grunt.GruntParser.parseStopOnError(GruntParser.java:205)
at org.apache.pig.tools.grunt.Grunt.run(Grunt.java:66)
at org.apache.pig.Main.run(Main.java:565)
at org.apache.pig.Main.main(Main.java:177)
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.run(RunJar.java:221)
at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
Caused by: java.lang.RuntimeException: could not instantiate 'pig.data.preprocess.ColNameToTypeMap' with arguments 'null'
at org.apache.pig.impl.PigContext.instantiateFuncFromSpec(PigContext.java:772)
at org.apache.pig.newplan.logical.expression.UserFuncExpression.getFieldSchema(UserFuncExpression.java:222)
at org.apache.pig.newplan.logical.optimizer.FieldSchemaResetter.execute(SchemaResetter.java:264)
at org.apache.pig.newplan.logical.expression.AllSameExpressionVisitor.visit(AllSameExpressionVisitor.java:143)
at org.apache.pig.newplan.logical.expression.UserFuncExpression.accept(UserFuncExpression.java:113)
at org.apache.pig.newplan.ReverseDependencyOrderWalker.walk(ReverseDependencyOrderWalker.java:70)
at org.apache.pig.newplan.PlanVisitor.visit(PlanVisitor.java:52)
at org.apache.pig.newplan.logical.optimizer.SchemaResetter.visitAll(SchemaResetter.java:67)
at org.apache.pig.newplan.logical.optimizer.SchemaResetter.visit(SchemaResetter.java:122)
at org.apache.pig.newplan.logical.relational.LOGenerate.accept(LOGenerate.java:245)
at org.apache.pig.newplan.DependencyOrderWalker.walk(DependencyOrderWalker.java:75)
at org.apache.pig.newplan.logical.optimizer.SchemaResetter.visit(SchemaResetter.java:114)
at org.apache.pig.parser.LogicalPlanBuilder.buildForeachOp(LogicalPlanBuilder.java:1055)
at org.apache.pig.parser.LogicalPlanGenerator.foreach_clause(LogicalPlanGenerator.java:15896)
at org.apache.pig.parser.LogicalPlanGenerator.op_clause(LogicalPlanGenerator.java:1933)
at org.apache.pig.parser.LogicalPlanGenerator.general_statement(LogicalPlanGenerator.java:1102)
at org.apache.pig.parser.LogicalPlanGenerator.statement(LogicalPlanGenerator.java:560)
at org.apache.pig.parser.LogicalPlanGenerator.query(LogicalPlanGenerator.java:421)
at org.apache.pig.parser.QueryParserDriver.parse(QueryParserDriver.java:191)
... 16 more
Caused by: java.lang.IllegalAccessException: Class org.apache.pig.impl.PigContext can not access a member of class pig.data.preprocess.ColNameToTypeMap with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:110)
at java.lang.Class.newInstance(Class.java:377)
at org.apache.pig.impl.PigContext.instantiateFuncFromSpec(PigContext.java:742)
... 34 more
我花了很多时间,但我无法弄清楚问题。如果有人以前解决了类似的问题,请帮忙。
答案 0 :(得分:0)
使用ANT / Maven构建jar时,jar中应包含所有必需的库。看来,有些图书馆遗失了。
确保所有依赖项都存在。
确保注册jar并在脚本中声明所有变量,如'ts'。