Hive python

python怎么读hive表生成txt文件

小亿
86
2024-03-18 09:51:53
栏目: 编程语言

你可以使用Python的PyHive库连接Hive数据库并执行查询,然后将查询结果写入到txt文件中。以下是一个简单的示例代码:

from pyhive import hive

# 连接Hive数据库
conn = hive.Connection(host="hostname", port=10000, username="username")
cursor = conn.cursor()

# 执行查询
cursor.execute("SELECT * FROM your_table")

# 将结果写入txt文件
with open("output.txt", "w") as f:
    for row in cursor.fetchall():
        f.write('\t'.join(str(field) for field in row) + '\n')

# 关闭连接
cursor.close()
conn.close()

在上面的代码中,你需要将hostnameusernameyour_table替换为你的Hive数据库的主机名、用户名和表名。然后运行代码即可将查询结果写入到名为output.txt的txt文件中。

0
看了该问题的人还看了