将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()))

 

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM