概述
pip install pyhs2
--------------------代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# hive util with hiveserver2
import pyhs2
from pyhs2.error import Pyhs2Exception
import sys
from file_util import fileUtil
from config_util import config
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)
class hiveUtil:
def __init__(self):
self.conn = pyhs2.connect(host=config.hive_host,
port=config.hive_port,
authMechanism=config.hive_auth,
user=config.hive_user,
password=config.hive_passwd,
database=config.hive_db,
)
def query(self, sql):
with self.conn.cursor() as cursor:
cursor.execute(sql)
return cursor.fetch()
def queryNoResult(self, sql):
with self.conn.cursor() as cursor:
result=cursor.execute(sql)
return result
def close(self):
self.conn.close()
if __name__ == '__main__':
hiveClient=HiveClient()
hql_path = "/home/airflow/ebus/hql/SP_ODS_EBUS_CB_ACC_INF_DAY_INSERT.hql"
text=fileUtil.get_file_content(hql_path)
f=open(hql_path)
txt=f.read()
sql=txt.replace('${day_id}','20170818').replace('${past_day_id}','20170817')
sql_list=sql.split(";")
result=0
for hql in sql_list:
if hql.strip()!='':
print("hql=="+hql)
try:
hiveClient.queryNoResult(hql)
except Pyhs2Exception :
result=1
print(result)
hiveClient.close()
-----------/home/airflow/ebus/hql/SP_ODS_EBUS_CB_ACC_INF_DAY_INSERT.hql 文件如下:
INSERT INTO TABLE bank.ODS_CBBNDIRT PARTITION(PARTITION_DATE = '${day_id}')
SELECT
TRIM(REGEXP_REPLACE(CORPORATION,'"','')) as CORPORATION,
TRIM(REGEXP_REPLACE(DAY_ID,'"','')) as DAY_ID,
TRIM(REGEXP_REPLACE(FK_CBBND_KEY,'"','')) as FK_CBBND_KEY,
TRIM(REGEXP_REPLACE(CB_IRT_ACT_DATE,'"','')) as CB_IRT_ACT_DATE,
CBIRT_DB_TIMESTAMP ,
CB_BASE_IRT ,
TRIM(REGEXP_REPLACE(FILLER,'"','')) as FILLER
FROM out.ODS_CBBNDIRT;
最后
以上就是可靠小懒虫为你收集整理的pyhs2连接hive的全部内容,希望文章能够帮你解决pyhs2连接hive所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复