[API]使用Python API访问Hive 2015-05-06 14:30

介绍

pyhs2是一个使用Python访问HiveServer2的第三方API。

安装

yum -y install gcc-c++ python-devel.x86_64 cyrus-sasl-devel.x86_64
yum -y install cyrus-sasl-gssapi cyrus-sasl-devel cyrus-sasl-plain cyrus-sasl-lib cyrus-sasl-md5 cyrus-sasl cyrus-sasl-plain
pip install pyhs2

使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import pyhs2

with pyhs2.connect(host='ctrl',
                   port=10000,
                   authMechanism="PLAIN",
                   user='root',
                   password='',
                   database='default') as conn:
    with conn.cursor() as cur:
        #Show databases
        print cur.getDatabases()

        #Execute query
        cur.execute("SELECT * FROM house")

        #Return column info from query
        print cur.getSchema()

        #Fetch table results
        for i in cur.fetch():
            print i

参考文档

  1. 官方主页
  2. ApacheWiki - Python Client Driver
Tags: #Hive #Python    Post on Hadoop