python怎么读hive表生成txt文件

47次阅读
没有评论

共计 509 个字符,预计需要花费 2 分钟才能阅读完成。

你可以使用 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 文件中。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2024-03-19发表,共计509字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)