(1)方案1,使用pyhive执行
安装需求包
pip install sasl
pip install thrift
pip install thrift-sasl
pip install PyHive
#hql文件里使用${etl_date}作为日期参数
#hql文件使用;;作为sql段的分割
import sys
from pyhive import hive
#连接hive 注意端口默认为10000,若使用了高可用请用高可用对应ip和端口
conn = hive.Connection(host='xx.xx.xx.xx', port=10000, username='hive', database='default')
cursor = conn.cursor()
filepath = sys.argv[1] #传参文件路径
etl_date = sys.argv[1] #传参跑批日期yyyymmdd
sqls = []
with open(filepath,'r') as fr:
content = fr.read()
#sql文件内容不同代码段用;;分割,不使用;分割是为了避免切割错误
sqls = content.replace('${etl_date}',etl_date).split(';;')
for sql in sqls:
cursor.execute(sql)
(2)方案2,调用beeline执行
#hql文件里使用${etl_date}作为日期参数
#beeline部分参数因人而异
import sys
import os
from datetime import datetime
filepath = sys.argv[1] #传参文件路径
etl_date = sys.argv[1] #传参跑批日期yyyymmdd
with open(filepath,'r') as fr:
content = fr.read()
now = datetime.now().strftime('%Y%m%d,%H%M%S')
#修改日期参数并落成临时文件
tmpfile = filepath+'.tmp'+now
with open(tmpfile,'w') as fw:
fw.write(content.replace('${etl_date}',etl_date))
beeline = "beeline -u jdbc:hive2://127.0.0.1:10000 -f "+tmpfile
os.system(beeline)
os.remove(tmpfile)
——————————————
注意点:
(1)请注意hql文件的格式、编码与Python文件的格式、编码关系
(2)若涉及时间参数的更改,需自行调整代码实现,本文仅提供简单的例子