码迷,mamicode.com
首页 > 编程语言 > 详细

A Python example for HiveServer2

时间:2015-05-15 22:42:03      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

要做一个通过调用python来实现对hive server2 的连接。在网上搜索了很多资料,有些说的hive sever的,但是由于认证方式发送了改变,行不通。

最后,找到了权威的说明(PS: 还是应该先看官方材料):

https://cwiki.apache.org/confluence/display/Hive/Setting+up+HiveServer2#SettingUpHiveServer2-PythonClientDriver

 

所以在这里结合自己的使用,主要还是给大家翻译一下:

A Python client driver for HiveServer2 is available at https://github.com/BradRuderman/pyhs2 (thanks, Brad). It includes all the required packages such as SASL and Thrift wrappers.

The driver has been certified for use with Python 2.6 and newer.

To use the pyhs2 driver:

pip install pyhs2

通过Python 连接HiveServer2的类可以从github上下载,下载地址:https://github.com/BradRuderman/pyhs2 。其中包含了pyhs2类中使用到的其他的类,比如SASL 和Thrift wrappers。可以手动下载后放在目录下,添加到sys.path中。

 

随后给出来一个simple example:

 1 import pyhs2 
 2 with pyhs2.connect(host=localhost,
 3                    port=10000,
 4                    authMechanism="PLAIN",
 5                    user=root,
 6                    password=test,
 7                    database=default) as conn:
 8     with conn.cursor() as cur:
 9         #Show databases
10         print cur.getDatabases()
11         #Execute query
12         cur.execute("select * from table")
13         #Return column info from query
14         print cur.getSchema()
15                                                     
16         #Fetch table results        
17         for i in cur.fetch():
18             print i

 

调试的过程中基本没有遇到什么大问题:

  1. 因以前的sys.path路径下有老的pyhs2的类库,会提示说缺少sasl的类库,将旧的pyhs2打包备份后,会自动指向新的pyhs2的类库,这个问题就解决了。

  2. 抛出异常的地方,我使用 try... except Thrift.TException, tx:的方式,能正常地抛出sql的异常。

 

如果有疑问,欢迎回复讨论。

 

最后提供了一个邮件列表,供技术讨论:

 You can discuss this driver on the user@hive.apache.org mailing list.

 

A Python example for HiveServer2

标签:

原文地址:http://www.cnblogs.com/520sojustdoit/p/4506916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!