共計(jì) 509 個(gè)字符,預(yù)計(jì)需要花費(fèi) 2 分鐘才能閱讀完成。
你可以使用 Python 的 PyHive 庫(kù)連接 Hive 數(shù)據(jù)庫(kù)并執(zhí)行查詢,然后將查詢結(jié)果寫入到 txt 文件中。以下是一個(gè)簡(jiǎn)單的示例代碼:
from pyhive import hive
# 連接 Hive 數(shù)據(jù)庫(kù)
conn = hive.Connection(host="hostname", port=10000, username="username")
cursor = conn.cursor()
# 執(zhí)行查詢
cursor.execute("SELECT * FROM your_table")
# 將結(jié)果寫入 txt 文件
with open("output.txt", "w") as f:
for row in cursor.fetchall():
f.write('\t'.join(str(field) for field in row) + '\n')
# 關(guān)閉連接
cursor.close()
conn.close()
在上面的代碼中,你需要將 hostname
、username
和your_table
替換為你的 Hive 數(shù)據(jù)庫(kù)的主機(jī)名、用戶名和表名。然后運(yùn)行代碼即可將查詢結(jié)果寫入到名為 output.txt
的 txt 文件中。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完