如何使用Python连接hive

安装依赖

1
2
3
4
pip install sasl
pip install thrift
pip install thrift-sasl
pip install PyHive

python脚本示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

from pyhive import hive

HOST="127.0.0.1"
PORT=10000
USERNAME="hadoop"
DATABASE="default"

conn=hive.Connection(host=HOST, port=PORT, username=USERNAME,database=DATABASE)

cursor = conn.cursor()
#cursor.execute("INSERT INTO TABLE test_out(name,count,time) SELECT name,count(1),to_date(time) FROM test GROUP BY name,to_date(time)")
cursor.execute("SELECT * FROM test")
for result in cursor.fetchall():
print(result[2])

参考 https://segmentfault.com/a/1190000022358127

Share