python連線hive的demo
阿新 • • 發佈:2019-02-02
Hive server 讓Hive提供Thrift服務的伺服器形式執行,允許不同的語言編寫客戶端進行訪問。使用Thrift、JDBC、ODBC聯結器的客戶需要執行hive伺服器來和Hive進行通訊。通過設定HIVE_PORT環境變數來指明伺服器監聽的埠(預設的是10000)
pyhive為python的thrift檔案目錄,需要載入到pythonpath中
這樣有了資料倉庫基本執行指令碼的模板了,對於執行的環境變數可以再做封裝部分,指令碼只突出邏輯處理。
alias python='python2.6'
PYTHONPATH=$PYTHONPATH:~/pyhive
export PATH HADOOP_HOME HIVE_HOME PYTHONPATH
pyhive為python的thrift檔案目錄,需要載入到pythonpath中
import sys from hive_service import ThriftHive from hive_service.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('192.168.30.201', 10000) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = ThriftHive.Client(protocol) transport.open() hql = '''CREATE TABLE people(a STRING, b INT, c DOUBLE) row format delimited fields terminated by ',' ''' print hql client.execute(hql) client.execute("LOAD DATA LOCAL INPATH '/home/diver/data.txt' INTO TABLE people") #client.execute("SELECT * FROM people") #while (1): # row = client.fetchOne() # if (row == None): # break # print row client.execute("SELECT count(*) FROM people") print client.fetchAll() transport.close() except Thrift.TException, tx: print '%s' % (tx.message)
這樣有了資料倉庫基本執行指令碼的模板了,對於執行的環境變數可以再做封裝部分,指令碼只突出邏輯處理。