港服(Server.HK)Python教程:python 连接hive的包是什么

python连接hive的几种方式 ·基于pyhive连接hive。 ·基于impyla连接hive。 方法一:使用Py…

python连接hive的几种方式

·基于pyhive连接hive。

·基于impyla连接hive。

方法一:使用PyHive库

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

pip install sasl
pip install thrift
pip install thrift-sasl
pip install PyHive

相关推荐:《Python基础教程》

Python脚本代码操作:

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依赖包:

pip install six
pip install bit-array
pip install thriftpy

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

pip install sasl
pip install thrift-sasl

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

Python脚本代码:

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()

为您推荐

港服(Server.HK)Python教程:如何实现对Python中列表的排序?

对List进行排序,Python提供了两个方法 方法1.用List的内建函数list.sort进行排序 list.sor...

港服(Server.HK)Python教程:python迭代器中的函数整理

1、可以连接迭代器的函数 chain:按顺序将多个迭代器连接成一个迭代器。 Cycle:重复迭代器的所有元素。 Tee:...

港服(Server.HK)Python教程:用Python举例实现逆波兰表达式

逆波兰表达式是编译原理中的一种基本表达式,利用Python语言也可以实现逆波兰表达式的输出,这里举例实践说明: 什么是逆...

Python 程序:检查给定字符串是否为回文

港服(Server.HK)Python教程: 用一个实例写一个 Python 程序来检查给定的字符串是不是回文。在 Py...

港服(Server.HK)Python教程:python3判断字典中key是否存在

今天来说一下如何判断字典中是否存在某个key,一般有两种通用做法,下面为大家来分别讲解一下: 第一种方法:使用自带函数实...
返回顶部