您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了python3.6.5如何基于kerberos認證的hive和hdfs連接調用,內容簡而易懂,希望大家可以學習一下,學習完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。
1. Kerberos是一種計算機網絡授權協議,用來在非安全網絡中,對個人通信以安全的手段進行身份認證。具體請查閱官網
2. 需要安裝的包(基于centos)
yum install libsasl2-dev yum install gcc-c++ python-devel.x86_64 cyrus-sasl-devel.x86_64 yum install python-devel yum install krb5-devel yum install python-krbV pip install krbcontext==0.9 pip install thrift==0.9.3 pip install thrift-sasl==0.2.1 pip install impyla==0.14.1 pip install hdfs[kerberos] pip install pykerberos==1.2.1
3. /etc/krb5.conf 配置, 在這個文件里配置你服務器所在的域
4./etc/hosts 配置, 配置集群機器和域所在機器
5. 通過kinit 生成 ccache_file或者keytab_file
6. 連接hive代碼如下
import os from impala.dbapi import connect from krbcontext import krbcontext keytab_path = os.path.split(os.path.realpath(__file__))[0] + '/xxx.keytab' principal = 'xxx' with krbcontext(using_keytab=True,principal=principal,keytab_file=keytab_path): conn = connect(host=ip, port=10000, auth_mechanism='GSSAPI', kerberos_service_name='hive') cursor = conn.cursor() cursor.execute('SELECT * FROM default.books') for row in cursor: print(row)
7. 連接hdfs代碼如下
from hdfs.ext.kerberos import KerberosClient from krbcontext import krbcontext hdfs_url = 'http://' + host + ':' + port data = self._get_keytab(sso_ticket) self._save_keytab(data) with krbcontext(using_keytab=True, keytab_file=self.keytab_file, principal=self.user): self.client = KerberosClient(hdfs_url) self.client._list_status(path).json()['FileStatuses']['FileStatus'] #獲取path下文件及文件夾
8. 注:krbcontext這個包官方說支持python2,但是python3也能用
這個hdfs_url 一定要帶"http://"不然會報錯
9. 我新增了一些配置文件配置,具體的操作如下
python3.6.5基于kerberos認證的hdfs,hive連接調用(含基礎環境配置)
1需要準備的環境
yum包(需要先裝yum包,再裝python包,不然會有問題)
yum install openldap-clients -y yum install krb5-workstation krb5-libs -y yum install gcc-c++ python-devel.x86_64 cyrus-sasl-devel.x86_64 yum install python-devel yum install krb5-devel yum install python-krbV yum install cyrus-sasl-plain cyrus-sasl-devel cyrus-sasl-gssapi
python包安裝(pip或pip3,請根據實際情況選擇)
pip install krbcontext==0.9 pip install thrift==0.9.3 pip install thrift-sasl==0.2.1 pip install impyla==0.14.1 pip install hdfs[kerberos] pip install pykerberos==1.2.1
配置/etc/hosts文件(需要把大數據平臺的機器和域名進行配置)
10.xxx.xxx.xxx name-1 panel.test.com
10.xxx.xxx.xxx name-1
配置/etc/krb5.conf(具體查看kerberos服務配置中心)
參考配置(僅供參考,具體更具自己實際配置修改)
[libdefaults] renew_lifetime = 9d forwardable = true default_realm = PANEL.COM ticket_lifetime = 24h dns_lookup_realm = false dns_lookup_kdc = false default_ccache_name = /tmp/krb5cc_%{uid} [logging] default = FILE:/var/log/krb5kdc.log admin_server = FILE:/var/log/kadmind1.log kdc = FILE:/var/log/krb5kdc1.log [realms] PANEL.COM = { admin_server = panel.test1.com kdc = panel.test1.com }
連接代碼:
hdfs:
import json, os from hdfs.ext.kerberos import KerberosClient from krbcontext import krbcontext def _connect(self, host, port, sso_ticket=None): try: hdfs_url = 'http://' + host + ':' + port active_str = 'kinit -kt {0} {1}'.format(self.keytab_file, self.user) # 激活當前kerberos用戶認證,因為python緩存機制,切換用戶,這個緩存不會自動切換,需要手動處理下 os.system(active_str) with krbcontext(using_keytab=True, keytab_file=self.keytab_file, principal=self.user): self.client = KerberosClient(hdfs_url) except Exception as e: raise e
hive
import os from krbcontext import krbcontext from impala.dbapi import connect from auto_model_platform.settings import config def _connect(self, host, port, sso_ticket=None): try: active_str = 'kinit -kt {0} {1}'.format(self.keytab_file, self.user) # 同hdfs os.system(active_str) with krbcontext(using_keytab=True, principal=self.user, keytab_file=self.keytab_file): self.conn = connect(host=host, port=port, auth_mechanism='GSSAPI', kerberos_service_name='hive') self.cursor = self.conn.cursor() except Exception as e: raise e
總結
我在做的時候也遇到很多坑,其實在這個需要理解其中原理,比如kerberos的機制和對應命令
如果是做基礎平臺用,用多用戶切換的情況,建議不要用python,因為一點都不友好,官方包問題很多,我都改用java的jdbc去操作hdfs和hive了
如果只是自己測試和和做算法研究,還是可以用的,因為這個代碼簡單,容易實現
補充
kinit命令
kinit -kt xxxx.keytab #激活xxxx用戶當前緩存
kinit list #查看當前緩存用戶
以上就是關于python3.6.5如何基于kerberos認證的hive和hdfs連接調用的內容,如果你們有學習到知識或者技能,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。