使用thrift api连接安全配置单元

qv7cva1a  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(513)

我正在尝试将安全配置单元服务器连接到 thrift 应用程序编程接口。如果hiveserver不需要身份验证(不安全),但不适用于安全的hiveserver,则此代码可以很好地工作
参考https://cwiki.apache.org/confluence/display/hive/hiveclient

  1. # !/usr/bin/env python
  2. import sys
  3. from hive import ThriftHive
  4. from hive.ttypes import HiveServerException
  5. from thrift import Thrift
  6. from thrift.transport import TSocket
  7. from thrift.transport import TTransport
  8. from thrift.protocol import TBinaryProtocol
  9. try:
  10. transport = TSocket.TSocket('localhost', 10000)
  11. transport = TTransport.TBufferedTransport(transport)
  12. protocol = TBinaryProtocol.TBinaryProtocol(transport)
  13. client = ThriftHive.Client(protocol)
  14. transport.open()
  15. client.execute("CREATE TABLE r(a STRING, b INT, c DOUBLE)")
  16. client.execute("LOAD TABLE LOCAL INPATH '/path' INTO TABLE r")
  17. client.execute("SELECT * FROM r")
  18. while (1):
  19. row = client.fetchOne()
  20. if (row == None):
  21. break
  22. print row
  23. client.execute("SELECT * FROM r")
  24. print client.fetchAll()
  25. transport.close()
  26. except Thrift.TException, tx:
  27. print '%s' % (tx.message)
cwdobuhd

cwdobuhd1#

您需要在代码中添加身份验证机制,如“nosal”、“kerberos”、“ldap”等,并根据该机制提供适当的凭据。
例子:

  1. authMechanisms = set(['NOSASL', 'PLAIN', 'KERBEROS', 'LDAP'])
  2. if authMechanism not in authMechanisms:
  3. raise NotImplementedError('authMechanism is either not supported or not implemented')
  4. #Must set a password for thrift, even if it doesn't need one
  5. #Open issue with python-sasl
  6. if authMechanism == 'PLAIN' and (password is None or len(password) == 0):
  7. password = 'password'
  8. socket = TSocket(host, port)
  9. socket.setTimeout(timeout)
  10. if authMechanism == 'NOSASL':
  11. transport = TBufferedTransport(socket)
  12. else:
  13. sasl_mech = 'PLAIN'
  14. saslc = sasl.Client()
  15. saslc.setAttr("username", user)
  16. saslc.setAttr("password", password)
  17. if authMechanism == 'KERBEROS':
  18. krb_host,krb_service = self._get_krb_settings(host, configuration)
  19. sasl_mech = 'GSSAPI'
  20. saslc.setAttr("host", krb_host)
  21. saslc.setAttr("service", krb_service)

参考文献:https://github.com/bradruderman/pyhs2/blob/master/pyhs2/connections.py

展开查看全部

相关问题