Python连接Hive
2016-06-15 17:01
423 查看
1.在使用Python连接hive之前,需要将hive安装包下的lib/py中的文件拷贝到python的sys.path中的site-packages下,否则引入对应的包会报错,这个是使用hive提供的Python接口来调用hive客户端。
2 启动hive 的thrift
确保以下服务开启:
hive --service hiveserver
默认端口是10000
from hive_service import ThriftHive
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
def ReadHiveTest(sql):
try:
tSocket = TSocket.TSocket('172.18.1.88',10000)
tTransport = TTransport.TBufferedTransport(tSocket)
protocol = TBinaryProtocol.TBinaryProtocol(tTransport)
client = ThriftHive.Client(protocol)
tTransport.open()
client.execute(sql)
return client.fetchAll()
except Thrift.TException, tx:
print '%s' % (tx.message)
finally:
tTransport.close()
if __name__ == '__main__':
showDatabasesSql = 'show databases'
showTablesSql = 'show tables'
selectSql = 'SELECT * FROM 07_jn_mysql_2'
result = ReadHiveTest(selectSql)
print(result[1])
2 启动hive 的thrift
确保以下服务开启:
hive --service hiveserver
默认端口是10000
from hive_service import ThriftHive
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
def ReadHiveTest(sql):
try:
tSocket = TSocket.TSocket('172.18.1.88',10000)
tTransport = TTransport.TBufferedTransport(tSocket)
protocol = TBinaryProtocol.TBinaryProtocol(tTransport)
client = ThriftHive.Client(protocol)
tTransport.open()
client.execute(sql)
return client.fetchAll()
except Thrift.TException, tx:
print '%s' % (tx.message)
finally:
tTransport.close()
if __name__ == '__main__':
showDatabasesSql = 'show databases'
showTablesSql = 'show tables'
selectSql = 'SELECT * FROM 07_jn_mysql_2'
result = ReadHiveTest(selectSql)
print(result[1])
相关文章推荐
- Python基本运算符
- python学习笔记 操作文件和目录
- Python学习笔记 20160615
- Python自然语言处理第二章部分习题
- python lib error
- Python编码问题
- python学习笔记 IO 文件读写
- python中获取div的文本内容并和想定结果进行对比
- Numpy详细教程
- 第一个 Python 程序
- 第一个 Python 程序
- Python SMTP发送邮件
- pytesser,图片文本识别工具
- python编程的中文问题
- python获取当前系统时间(yyyy-mm-dd)
- Python学习笔记 使用数据库SQlite Mysql
- Python运算符与表达式
- k近邻算法
- Python3 面向对象
- python中list的拷贝与numpy的array的拷贝