zoukankan      html  css  js  c++  java
  • A Python example for HiveServer2

    要做一个通过调用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.

  • 相关阅读:
    shell 学习笔记 LinuxShell脚本攻略(第2版)
    [六省联考2017]分手是祝愿
    [SDOI2010]粟粟的书架
    [CQOI2018]解锁屏幕
    [SCOI2007]最大土地面积
    CF369E Valera and Queries
    CF817F MEX Queries
    [HEOI2016/TJOI2016]求和
    [CQOI2018]九连环
    [HNOI2015]亚瑟王
  • 原文地址:https://www.cnblogs.com/520sojustdoit/p/4506916.html
Copyright © 2011-2022 走看看