Python操作Hive准备
来源:互联网 发布:淘宝卖家如何分销 编辑:程序博客网 时间:2024/06/03 18:18
教程中使用的是Java操作hive,而我的需求是使用python操作hive,所以需要进行简单的环境配置。
使用python操作hive有两种方式:
Thrift api方式和python hive相关的包
1、Thrift api方式
根据介绍,只需要把hive/lib/py包下的文件全部拷贝到python的扩展库文件夹下即可site-packages。
拷贝完成后,启动hiveserver2服务器,使用测试代码测试:
import sysfrom hive_service import ThriftHivefrom hive_service.ttypes import HiveServerExceptionfrom thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocoldef hiveExe(sql): try: transport = TSocket.TSocket('192.168.83.135', 10000) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = ThriftHive.Client(protocol) transport.open() client.execute(sql) print "The return value is : " print client.fetchAll() print "............" transport.close() except Thrift.TException, tx: print '%s' % (tx.message)if __name__ == '__main__': hiveExe("show databases adad;")
进行调试运行,发现走到client.execute(sql)这句时无法继续执行,目前还没找到解决办法。
2、使用pyhs2包操作hive(官方已经不再持续支持,但还可以用)
安装pyhs2依赖包:
yum install cyrus-sasl-plainyum install cyrus-sasl-develyum install python-devel.x86_64yum install cyrus-sasl-devel.x86_64
使用pip安装pyhs2
pip installpyhs2
测试代码如下:
import pyhs2conn = pyhs2.connect(host='localhost', port=11111, authMechanism="PLAIN", user='root', password='test', database='default')cur = conn.cursor()print cur.getDatabases()
代码执行结果如下图:
3、使用pyHive包操作hive,需要安装pyhive包,测试代码如下:
from pyhive import hivefrom TCLIService.ttypes import TOperationState# 打开hive连接hiveConn = hive.connect(host='192.168.83.135',port=11111)cursor = hiveConn.cursor()# 执行sql语句cursor.execute('show databases', async=True)# 得到执行语句的状态status = cursor.poll().operationStateprint "status:",status# 如果执行出错,循环执行,直到执行正确,可不要while status in (TOperationState.INITIALIZED_STATE, TOperationState.RUNNING_STATE): logs = cursor.fetch_logs() for message in logs: print message # If needed, an asynchronous query can be cancelled at any time with: # cursor.cancel() status = cursor.poll().operationState# 打印结果print cursor.fetchall()# 关闭hive连接cursor.close()hiveConn.close()
程序运行结果:
在之后的学习中,将使用pyhive进行操作。
阅读全文
0 0
- Python操作Hive准备
- Hbase Python 操作准备
- python-操作hive
- Python操作MySql-pybatis的准备阶段(三)
- 用 python 操作 Excel 2007 表格 --- 准备篇
- Hive入门 准备用于ETL
- hive操作
- Hive 操作
- Hive操作
- hive操作
- Hive操作
- 使用python来操作hive(通过 pyhs2 和 HiveServer2)
- Python学习笔记-List基本操作【python 3】//为继续学习爬虫准备-01
- [Hive]Hive多分区操作
- HIVE---HIve的JavaAPI操作
- Hadoop--Hive准备(MySQL RPM安装)
- 初学Python环境准备
- python爬虫准备
- Email邮件服务的搭建(postfix on rhel7.2)
- 【Android API】Activity的四种启动详细分析
- JAVA--List接口的四种遍历方法
- 2017.08.19【NOIP提高组】模拟赛B组 经济编码
- Java Concurrency代码实例之六-ConcurrentHashMap
- Python操作Hive准备
- git切换到非master分支
- spring-AOP-苍老师
- Office 2016中修改正文默认字体
- 受欢迎的10个类
- c++ poco Connector tcpclient测试用例
- 动态规划之01背包问题
- 怎样在Transformer+文档保护中使用数字签名
- 测试