您好,登錄后才能下訂單哦!
這篇文章主要講解了Python如何通過kerberos安全認證操作kafka,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
如何通過Kerberos認證.
1.安裝Kerberos客戶端
CentOS:
yum install krb5-workstation
使用which kinit查看是否安裝成功;
2.拷貝Kerberos配置文件
conf目錄下krb5.conf和kafka.keytab和jaas.conf拷貝到客戶端機器的etc目錄, 同時,krb5.conf中的kdc集群主機名和IP配置到客戶端機器hosts配置文件中
3.Kinit客戶端通過kerberos認證
獲取Principal
klist -kt kafka.keytab
4.安裝python-gssapi
pip install gssapi
遇到的問題,如下:
a.在linux中執行wget命令提示 -bash: wget: command not found 解決方法
yum -y install wget
b.報錯:bash: pip: command not found
wget https://bootstrap.pypa.io/get-pip.py python get-pip.py pip -V #查看pip版本
python -m pip install --upgrade --force pip easy_install -U setuptools pip install --upgrade setuptools
3.pip安裝出現Command “python setup.py egg_info” failed with error code 1 的解決方法
Traceback (most recent call last): File “”, line 1, in File “/tmp/pip-install-6HfDE3/gssapi/setup.py”, line 109, in raise Exception("Could not find main GSSAPI shared library. Please " Exception: Could not find main GSSAPI shared library. Please try setting GSSAPI_MAIN_LIB yourself or setting ENABLE_SUPPORT_DETECTION to ‘false'
Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-install-6HfDE3/gssapi/
yum install -y krb5-devel.x86_64
4.關于error: command ‘gcc' failed with exit status 1錯誤的解決方法
yum install gcc python-devel
安裝kafka-python
pip install kafka-python
初始化環境變量
export KAFKA_OPTS="-Djava.security.auth.login.config=/etc/conf/jaas.conf -Djava.security.krb5.conf=/etc/krb5.conf"
Python操作kafka樣例
from kafka import KafkaProducer from kafka.errors import KafkaError import os class Kafka_Producer(): def __init__(self, kafkahost, kafkaport, kafkatopic): self.kafkaHost = kafkahost self.kafkaPort = kafkaport self.kafkatopic = kafkatopic self.producer = KafkaProducer( bootstrap_servers = '{kafka_host}:{kafka_port}'.format(kafka_host=self.kafkaHost,kafka_port=self.kafkaPort), security_protocol="SASL_PLAINTEXT", sasl_mechanism="GSSAPI", sasl_kerberos_service_name="kafka", compression_type='gzip' #壓縮方式 ) def sendFileData(self, params): try: f = open(params,'rb') parmasMessage = f.read(-1).strip() producer = self.producer producer.send(self.kafkatopic, parmasMessage) producer.flush() except KafkaError as e: print (e) def main(): filePath = "/home/public/data/" topic = "demo" producer = Kafka_Producer("xxx.xx.xx.xx","9092",topic) dirList = os.listdir(filePath) for fileName in dirList: producer.sendFileData(filePath+fileName) print('send success!!!') if __name__=='__main__': main()
看完上述內容,是不是對Python如何通過kerberos安全認證操作kafka有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。