从hivesql结果中读取数值到shell变量的方法

为了检查hive表中的数据,并统计展现,需要将查出的结果传入到shell变量,然后统一输出到文本。

最后使用了以下两个方法:

  • 方法一
QUAN=$(hive -S -e "select count(1) from test" | grep quantity | cut -f 2)
  • 方法二
hive -S -e "select 'quantity', count(1) from test" | grep quantity | { read a1 a2; echo $a2; }

目前,我们建议使用方法一的脚本,方法二脚本若不能保证查询结果为一行,则会产生报错。

原文地址:https://www.cnblogs.com/shenfeng/p/read_value_from_hivesql.html