Python鏈接到HiveServer2

環境配置

首先安裝Python包:python

  • sasl
  • pyhs2

pyhs2項目做者2014年以後就沒有更新了。git

代碼樣例

import pyhs2

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

    	#Execute query
        cur.execute("select * from table")

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

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

使用KERBEROS

import pyhs2

# KERBEROS
conn = pyhs2.connect(host='n5.cluster',
                     port=10000,
                     authMechanism="KERBEROS")

conn = pyhs2.connect(host='n5.cluster',
                     port=10000,
                     authMechanism="KERBEROS",
                     user='xwtech/n5.cluster@EXAMPLE.COM',
                     configuration={'krb_host': 'n5.cluster', 'krb_service': 'hive'})

pyhs2地址

https://github.com/BradRuderman/pyhs2/github

相關文章
相關標籤/搜索