利用python,从多个hive表导出多个excel文件
时间: 2024-05-05 08:18:57 浏览: 123
python 利用pyhive 导出excel
可以使用Python的pandas和pyhive库连接Hive数据库,并使用pandas的to_excel方法将数据写入Excel文件。以下是一个示例代码:
```python
import pandas as pd
from pyhive import hive
# Connect to Hive server
conn = hive.Connection(host='localhost', port=10000, username='hiveuser')
# Query multiple tables and combine results using UNION
query = """
SELECT * FROM table1
UNION
SELECT * FROM table2
UNION
SELECT * FROM table3
"""
# Load data into a pandas dataframe
df = pd.read_sql(query, conn)
# Write dataframe to Excel file
writer = pd.ExcelWriter('output.xlsx')
df.to_excel(writer, index=False)
writer.save()
# Close the connection to Hive server
conn.close()
```
这段代码会将Hive中的table1、table2和table3表导出并合并到一个名为output.xlsx的Excel文件中。
阅读全文