安装phive
pip install sasl
pip install thrift
pip install thrift-sasl
pip install PyHive
工具类
class HiveClient:
def __init__(self, db_host, user, password, database, port=10000):
"""
create connection to hive server2
"""
# 配置队列
config = {"mapreduce.job.queuename": "XXX"}
self.conn = hive.Connection(host=db_host,
port=port,
auth='CUSTOM',
username=user,
password=password,
database=database,
configuration=config
)
def query(self, sql):
"""
query
"""
with self.conn.cursor() as cursor:
cursor.execute(sql)
return cursor.fetchall()
if __name__ == '__main__':
"""
main process
"""
try:
hiveCli = HiveClient(hive_info["ip"], hive_info["username"], hive_info["password"],
"default", hive_info["port"])
sql = '''select * from db_test.test where concat_ws('-', year, month, day) = '2019-07-03' and (
sql_str LIKE '%%phone%%') limit 10'''
data = hiveCli.query(sql)
print(data)
except Exception, tx:
print '%s' % (tx.message)