前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python操作Hive的两种方法总结

Python操作Hive的两种方法总结

作者头像
py3study
发布2020-01-06 11:20:02
6.9K0
发布2020-01-06 11:20:02
举报
文章被收录于专栏:python3

方法一:使用PyHive库

安装依赖包:其中sasl安装可能会报错,可以去https://www.lfd.uci.edu/~gohlke/pythonlibs/#sasl下载对应版本安装。

代码语言:javascript
复制
pip install sasl
pip install thrift
pip install thrift-sasl
pip install PyHive

Python脚本代码操作:

代码语言:javascript
复制
from pyhive import hive   # or import hive
conn = hive.Connection(host='****', port=****, username='****', database='****')
cursor.execute(''SELECT * FROM my_awesome_data LIMIT 10'')
for i in range(****):
    sql = "INSERT INTO **** VALUES ({},'username{}')".format(value, str(username))
    cursor.execute(sql)


# 下面是官网代码:
from pyhive import presto  # or import hive
cursor = presto.connect('localhost').cursor()
cursor.execute('SELECT * FROM my_awesome_data LIMIT 10')
print(cursor.fetchone())
print(cursor.fetchall())

方法二:使用 impyla库

impyla依赖包:

代码语言:javascript
复制
pip install six

pip install bit_array

pip install thriftpy

为了支持Hive还需要以下两个包:

代码语言:javascript
复制
pip install sasl
pip install thrift_sasl

可在Python PyPI中下载impyla及其依赖包的源码

Python脚本代码:

代码语言:javascript
复制
from impala.dbapi import connect 
conn = connect(host ='****',port = ****)
cursor = conn.cursor()
cursor.execute('SELECT * FROM mytable LIMIT 100')
print cursor.description   # 打印结果集的schema 
results = cursor.fetchall()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档