我正在尝试在 Hive 中查找数字的阶乘。目前没有 Hive 函数可以做到这一点,所以我尝试编写自己的函数。这是我的代码:
package com.guy.hive.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.LongWritable;
import org.apache.commons.math3.util.ArithmeticUtils;
public final class Factorial extends UDF {
public LongWritable evaluate(final LongWritable s){
int n = (int) s.get();
int fact = (int) ArithmeticUtils.factorial(n);
return new LongWritable(fact);
}
}
当我运行这个 Hive 查询时:
select factorial(c) from (select count(*) as c from test_table) ;
我得到异常:
Caused by: org.apache.hadoop.hive.ql.metadata.HiveException: Unable to execute method public org.apache.hadoop.io.LongWritable com.vm.hive.udf.Factorial.evaluate(long) on object com.vm.hive.udf.Factorial@37483748 of class com.vm.hive.udf.Factorial with arguments {39514210:java.lang.Long} of size 1
有人可以帮忙吗?
堆栈跟踪:
Caused by: org.apache.hadoop.hive.ql.metadata.HiveException: Unable to execute method public org.apache.hadoop.io.LongWritable com.vm.hive.udf.Factorial.evaluate(org.apache.hadoop.io.LongWritable) on object com.vm.hive.udf.Factorial@5faa5faa of class com.vm.hive.udf.Factorial with arguments {39514210:org.apache.hadoop.io.LongWritable} of size 1
at org.apache.hadoop.hive.ql.exec.FunctionRegistry.invoke(FunctionRegistry.java:1030)
at org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge.evaluate(GenericUDFBridge.java:181)
at org.apache.hadoop.hive.ql.exec.ExprNodeGenericFuncEvaluator._evaluate(ExprNodeGenericFuncEvaluator.java:166)
at org.apache.hadoop.hive.ql.exec.ExprNodeEvaluator.evaluate(ExprNodeEvaluator.java:77)
at org.apache.hadoop.hive.ql.exec.ExprNodeEvaluator.evaluate(ExprNodeEvaluator.java:65)
at org.apache.hadoop.hive.ql.exec.SelectOperator.processOp(SelectOperator.java:80)
at org.apache.hadoop.hive.ql.exec.Operator.process(Operator.java:504)
at org.apache.hadoop.hive.ql.exec.Operator.forward(Operator.java:842)
at org.apache.hadoop.hive.ql.exec.GroupByOperator.forward(GroupByOperator.java:1052)
at org.apache.hadoop.hive.ql.exec.GroupByOperator.flush(GroupByOperator.java:1077)
... 10 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:611)
at org.apache.hadoop.hive.ql.exec.FunctionRegistry.invoke(FunctionRegistry.java:1006)
... 19 more
Caused by: org.apache.commons.math3.exception.MathArithmeticException: arithmetic exception
at org.apache.commons.math3.util.ArithmeticUtils.factorial(ArithmeticUtils.java:317)
at com.vm.hive.udf.Factorial.evaluate(Factorial.java:50)
... 24 more
[编辑 1 - 添加了对 Java 代码的导入。]
[编辑 2 - 添加了 StackTrace
最佳答案
我看到你的问题了。问题不在 Hive 中,而在 ArithmeticUtils factorial
方法中。看到它抛出一个 MathArithmeticException
了吗?根据文档,这将在“结果太大而无法用 long 表示”时出现。
这一定是您的情况。尝试将较小的数字传递给该方法。
另请注意,factorial
方法已弃用。文档建议改用 CombinatoricsUtils.factorialLog(int) 方法。
关于java - Hive 阶乘 UDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27044130/