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

时间:2016-02-17 10:29:33

标签: python hive thrift

我正在尝试使用thrift api连接安全的Hive服务器。此代码适用于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)

1 个答案:

答案 0 :(得分:0)

您需要在代码中添加身份验证机制,例如“NOSASL”,“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