将科学计数法转成decimal
编写java
package cn.bdp.udf;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import java.math.BigDecimal;
@Description(name = "number_convert",
value = "_FUNC_(string) - Convert a string to bigDecimal ,return str.")
public class NumberConvertUDF extends UDF {
public String evaluate(String str) {
if (str == null) {
return "0";
}
try {
BigDecimal bd = new BigDecimal(str);
String result = bd.toPlainString();
return result;
} catch (Exception e) {
return "0";
}
}
}
打包生成my_udf_jar.jar
创建自定义函数
# 将jar包上传到HDFS
hive> add jar /tmp/my_udf_jar.jar;
# 将jar包上传到HDFS
hive> create temporary function number_convert as 'cn.bdp.udf.NumberConvertUDF ';
hive> select number_convert("4.548820080129802E-4");
0.0004548820080129802
hive> select CAST( number_convert("4.548820080129802E-4") AS decimal(9,5) ) ;
0.00045
更新jar包操作
# 列出所有jar
hive> list jars;
# 删除对应的tmp
hive> delete jar /tmp/448a59e7-5ef8-4b76-8eed-ba04c73e126a_resources/udf-1.0.jar;
hive> DROP TEMPORARY FUNCTION number_convert;