在IDEA上用python來串連叢集上的hive

來源:互聯網
上載者:User

標籤:

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.