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

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

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


# !/usr/bin/env python

import sys

from hive import ThriftHive
from hive.ttypes import HiveServerException
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

try:
   transport = TSocket.TSocket('localhost', 10000)
   transport = TTransport.TBufferedTransport(transport)
   protocol = TBinaryProtocol.TBinaryProtocol(transport)

   client = ThriftHive.Client(protocol)
   transport.open()

   client.execute("CREATE TABLE r(a STRING, b INT, c DOUBLE)")
   client.execute("LOAD TABLE LOCAL INPATH '/path' INTO TABLE r")
   client.execute("SELECT * FROM r")
   while (1):
      row = client.fetchOne()
      if (row == None):
          break
      print row
  client.execute("SELECT * FROM r")
  print client.fetchAll()

  transport.close()

  except Thrift.TException, tx:
      print '%s' % (tx.message)
cwdobuhd

cwdobuhd1#

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

authMechanisms = set(['NOSASL', 'PLAIN', 'KERBEROS', 'LDAP'])
    if authMechanism not in authMechanisms:
        raise NotImplementedError('authMechanism is either not supported or not implemented')
    #Must set a password for thrift, even if it doesn't need one
    #Open issue with python-sasl
    if authMechanism == 'PLAIN' and (password is None or len(password) == 0):
        password = 'password'
    socket = TSocket(host, port)
    socket.setTimeout(timeout)
    if authMechanism == 'NOSASL':
        transport = TBufferedTransport(socket)
    else:
        sasl_mech = 'PLAIN'
        saslc = sasl.Client()
        saslc.setAttr("username", user)
        saslc.setAttr("password", password)
        if authMechanism == 'KERBEROS':
            krb_host,krb_service = self._get_krb_settings(host, configuration)
            sasl_mech = 'GSSAPI'
            saslc.setAttr("host", krb_host)
            saslc.setAttr("service", krb_service)

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

相关问题