標籤:
1.在使用Python串連hive之前需要將hive中的檔案拷貝到自己建立python項目中
cp -r apache-hive-0.14.0-bin/lib/py /home/jia/Desktop
2.把hive上的py目錄下載到案頭之後,進入py目錄,複製裡面所有的檔案到你建立的python項目下
3.建立一個myHiveLink.py檔案,訪問hive的代碼如下
import sysfrom hive_service import ThriftHivefrom hive_service.ttypes import HiveServerExceptionfrom thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocoldef hiveExe(sql): try: transport = TSocket.TSocket(‘121.8.xxx.xx‘, 10000) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = ThriftHive.Client(protocol) transport.open() client.execute(sql) print "The return value is : " resultSets=client.fetchAll() for j in range(len(resultSets)): print resultSets[j] print "............" transport.close() except Thrift.TException, tx: print ‘%s‘ % (tx.message) return resultSetsif __name__ == ‘__main__‘: hiveExe("select * from tableName limit 10")
4.訪問結果如下:
/usr/bin/python2.7 /home/sendi/IdeaProjects/hive_python/.idea/HiveLink/myHiveLink.pyThe return value is : 460030087488272 1333280xxxx 2433 16881 20150608005859 20150608005859 NULL 113.4092361 23.1117361 20150608460037560630758 1812682xxxx 2817 5920 20150608005850 20150608005858 16 113.39436 23.42839 20150608460030991691721 1532152xxxx 2817 31314 20150608005851 20150608005858 16 113.34354 23.28444 20150608460036360705126 1537219xxxx 2817 22386 20150608005851 20150608005858 16 113.3470139 23.2713194 20150608460036360703708 1532570xxxx 2817 3617 20150608005853 20150608005858 16 113.3468056 23.3133333 20150608460036810304576 1533673xxxx 2817 322 20150608005851 20150608005858 16 113.359375 23.2908333 20150608460036110265159 1530011xxxx 2817 3409 20150608005854 20150608005858 16 113.3260417 23.2946528 20150608460030991632527 1532152xxxx 2817 22384 20150608005854 20150608005858 16 113.3470139 23.2713194 20150608460036671118650 1895716xxxx 2817 31360 20150608005853 20150608005858 16 113.35415 23.30307 20150608460036360702214 1534570xxxx 2817 22386 20150608005851 20150608005858 16 113.3470139 23.2713194 20150608
5.如果訪問不了,可能是沒有啟動hive,服務,則進入hive的bin目錄啟動服務
hive --service hiveserver &
在IDEA上用python來串連叢集上的hive