這里分別針對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()))
最后要注意的是變量的值中含有空格,需要做去空格處理。