将hive的hql执行结果保存到变量中

这里分别针对shell脚本和python脚本举例:

shell脚本如下:

注意:在hive语句左右两边使用的是ESC键下面的点号,不是单引号。

#!/usr/bin/env bash

test1=`hive -S -e "select max(period_value) from dw_dm.dm_guba_loginlog_activity_stat where dim = 'all' and period = 'day' and year = '2017';"`
test1=`echo ${test1}`

echo '--------------------'
echo ${test1}

python中直接有函数os.popen(xxx).read()可以引用:

# -*-coding:utf-8-*-

import os

hqlCommand = '''
hive -S -e "select max(period_value) from dw_dm.dm_guba_loginlog_activity_stat 
where dim = 'all' 
and period = 'day' 
and year = '2017';" 
'''

maxValue = os.popen(hqlCommand).read()
print(maxValue)
print(len(hqlCommand.strip()))

最后要注意的是变量的值中含有空格,需要做去空格处理。

原文地址:https://www.cnblogs.com/30go/p/7641417.html