2016-02-17 36 views
1

我試圖將安全配置單元服務器與thrift api連接起來。此代碼的工作以及在HiveServer不需要身份驗證(不安全)的情況,但不與安全HiveServer使用節儉api連接安全配置單元

參考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) 
+0

什麼是這個'C++'部分? –

+0

這不是語言基礎問題,我可以接受任何語言的答案,無論如何,我已經刪除了C++ –

回答

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