Python怎么连接hive
时间: 2023-10-24 15:58:37 浏览: 106
要使用Python连接Hive,你可以使用两种不同的库:pyhive和impyla。下面是使用这两个库的示例代码:
使用pyhive库连接Hive的示例代码如下:
```python
from pyhive import hive
import pandas as pd
# 创建Hive连接
conn = hive.Connection(host='10.16.15.2', port=10000, username='hive', database='user')
cur = conn.cursor()
# 执行查询语句
sql = "select * from user_huaxiang_wide_table"
cur.execute(sql)
# 获取查询结果并转换为DataFrame
df = pd.DataFrame(cur.fetchall())
# 关闭连接
cur.close()
conn.close()
# 打印查询结果
print(df)
```
使用impyla库连接Hive的示例代码如下:
```python
from impala.dbapi import connect
import pandas as pd
# 创建Hive连接
conn = connect(host='10.16.15.2', port=10000, auth_mechanism='PLAIN', user='hive', password='user@123', database='user')
cur = conn.cursor()
# 执行查询语句
sql = "select * from user_huaxiang_wide_table limit 100"
cur.execute(sql)
# 获取查询结果并转换为DataFrame
df = pd.DataFrame(cur.fetchall())
# 关闭连接
cur.close()
conn.close()
# 打印查询结果
print(df)
```
这两种方法都可以连接Hive并执行查询语句,然后将结果转换为DataFrame进行处理。你可以根据自己的需求选择其中一种方法来连接Hive。
#### 引用[.reference_title]
- *1* *2* [Python连接hive数据库小结](https://blog.csdn.net/qq_40304090/article/details/108263224)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [python 连接 hive](https://blog.csdn.net/zhenyangzhijia/article/details/48694721)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文