生产环境ELK记录的报错信息:
ERROR c.x.c.d.b.s.i.TableWarnServiceImpl - executeTableWarnJob error:
java.lang.NumberFormatException: For input string: "57.97888386123681"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)
at com.xy.cloudiview.datasetsubscript.business.service.impl.TableWarnServiceImpl.checkItem(TableWarnServiceImpl.java:180)
对应的报错代码片段:
private boolean checkItem(TableWarnParams params, Object result) {
boolean bl = false;
// 设置的阈值
Long threshold = params.getCount();
Long count = Long.parseLong(result.toString());
switch (params.getWhere()) {
case ">":
if (count > threshold) {
bl = true;
}
default:
break;
}
return bl;
}
报错的SQL语句:
select round(afpi_loanlevel_total_cnt_his24mth_miss * 100, 0) as afpi_miss_58
from view_monitor_miss_rate
order by list_day desc
limit 1
将这个SQL拿到类似于ad hoc这种即时查询可视化平台查询得到的结果:
考虑到是线上应用,故而考虑将SQL取整,即使用impala提供的round(num, 0)
函数,这个函数在可视化查询平台上面的执行结果如下:
但是,ELK仍然记录如下日志:
ERROR c.x.c.d.b.s.i.TableWarnServiceImpl - executeTableWarnJob error:
java.lang.NumberFormatException: For input string: "58.0"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)
at com.xy.cloudiview.datasetsubscript.business.service.impl.TableWarnServiceImpl.checkItem(TableWarnServiceImpl.java:180)
于是只好在本地测试环境尝试重现这个问题。
将报错SQL简化一下,替换到postman的requestBody里(省略其他非相关的json字段):
{
"sqlTmp": "select 57.97888386123681 as a, 11 as b;"
}
可见查询得到的是一个BigDecimal类型的数据。使用round(num, 0)
函数
本地无法复现问题。
没有办法,最后的解决方法:
Long count;
// 兼容round(num, 0)之后依然存在小数点的问题
if (result.toString().contains(".")) {
count = Math.round(Double.parseDouble(result.toString()));
} else {
count = Long.parseLong(result.toString());
}
附使用的hive-jdbc版本号:
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.1.0-cdh5.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0-cdh5.7.1</version>
</dependency>