• 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.

  • 相关阅读:
    Linux内核参数优化
    Linux:文件系统层次结构标准(Filesystem Hierarchy Standard)
    全球海底光缆及我国海底光缆分布
    CentOS单网卡绑定双IP
    Weblate 2.11安装配置文档
    Docker安装及常用命令
    好文要保存
    rsh命令配置于使用
    RHEL(或CentOS)中关于逻辑卷( Logical Volume Manager,LVM)的一些概念及使用LVM的例子
    git学习资料
  • 原文地址:https://www.cnblogs.com/520sojustdoit/p/4506916.html
Copyright © 2020-2023  润新知