數據庫開發實戰教程:使用Python鏈接Kerberos的Presto

【摘要】本文將爲你們帶來Python鏈接presto開源的兩個實踐案例。

Python鏈接presto開源提供瞭如下兩個庫能夠使用html

presto-python-client:github.com/prestodb/pr…python

pyhive:github.com/dropbox/PyH…git

接下來就來分別實踐一下,這兩種客戶端。github

環境準備

  1. 建立一臺Ubuntu的ECS,VPC選擇和MRS集羣一致
  2. 下載安裝apt-get,pip3,python3
  3. 安裝MRS客戶端

參考support.huaweicloud.com/usermanual-…api

遇到的報錯:Error: Network time protocol(NTP) not running. Please start NTP first.bash

解決方案:sudo /etc/init.d/ntp restartsession

4. 在MRS Manager頁面中系統設置>用戶管理中建立的用戶,並下載認證憑據,解壓後,將krb5.conf和user.keytab放到/root目錄下ide

5. 獲取SSL認證憑據工具

將集羣中的/opt/Bigdata/MRS_1.8.10/install/FusionInsight-ldapclient-2.5.0/ldapclient/local/cert/cacert.pem文件拷貝到Ubuntu的ECS的/root目錄下fetch

鏈接方式一:presto-python-client

(1)安裝依賴

  • 安裝pandas:

pip3 install pandas

  • 安裝requests-kerberos:

sudo apt-get install python3-dev

sudo apt install krb5-multidev

pip3 install requests_kerberos

  • 安裝presto-python-client

pip3 install presto-python-client

(2)編寫代碼

import prestodb
import os
from pandas import DataFrame
os.system('kinit -kt {}/user.keytab {}'.format('/root','sxy'))
 
conn=prestodb.dbapi.connect(
    host='192.168.0.194', #Presto Coordinator的ip
    port=7521,
    user='sxy',
    catalog='tpcds',
    schema='sf1',
    http_scheme='https',
    auth=prestodb.auth.KerberosAuthentication(config='/root/krb5.conf',service_name='presto',principal='sxy',mutual_authentication=False,ca_bundle='/root/cacert.pem')
)
 
cur = conn.cursor()
cur.execute('select c_first_name, c_last_name from customer limit 5')
df = DataFrame(cur.fetchall())
df.columns = ['First_Name', 'Last_Name']
print(df)複製代碼

(3)運行結果

鏈接方式二:PyHive

(1)安裝依賴

  • 安裝requests-kerberos:

sudo apt-get install python3-dev

sudo apt install krb5-multidev

pip3 install requests_kerberos

  • 安裝pyhive

pip3 install pyhive2. 編寫代碼

(2)編寫代碼

from pyhive import presto
 
presto_cli = presto.connect(
    host='192.168.0.194',
    port=7521,
    username='sxy',
    password='XXXX', # 建立用戶時設置的密碼
    catalog='tpcds',
    schema='sf1',
    poll_interval=1,
    source='pyhive',
    session_props=None,
    protocol='https',
    requests_session=None,
    requests_kwargs={
        'verify':'/root/cacert.pem'
    },
    KerberosRemoteServiceName='presto',
    KerberosPrincipal='sxy',
    KerberosConfigPath='/root/krb5.conf',
    KerberosKeytabPath='/root/user.keytab',
    KerberosCredentialCachePath=None, KerberosUseCanonicalHostname=None
)
c = presto_cli.cursor()
c.execute('show tables')
for i in c.fetchall():
    print(i)複製代碼
  1. 運行結果

那些踩的坑

  1. 用EulerOS鏡像裝requests-kerberos遇到一些XXX.h文件沒法找到的問題

沒有找到EulerOS下替代sudo apt-get install python3-dev,修復該問題的方法。轉而使用Ubuntu。

2. SSL認證失敗

出現如下報錯的緣由pem文件配置不正確

3. 401 Unauthorized報錯

緣由:Kerberos的四個相關參數(user, principal,krb5,keytab)配置錯誤。

校驗參數的方法:用presto_cli工具登陸客戶端後執行show tables,若成功則,參數正確。

點擊關注,第一時間瞭解華爲雲新鮮技術~

相關文章
相關標籤/搜索