python怎樣通過thrift方式連接hive

本篇文章給大家分享的是有關(guān)python怎樣通過thrift方式連接hive,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

為澤庫等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及澤庫網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站制作、網(wǎng)站建設(shè)、澤庫網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

hive安裝完成后,如果只是本地使用,啟用

nohup hive --service metastore &

[hadoop@master1 usr]$ hive

Logging initialized using configuration in file:/data/usr/hive/conf/hive-log4j.properties
hive> use fmcm;
OK
Time taken: 0.874 seconds

如果是要腳本調(diào)用,則需要啟用HiveServer2,確保10000端口已經(jīng)被監(jiān)聽(可在hive-site.xml中修改端口)

 nohup hive --service hiveserver2? &

[hadoop@master1 usr]$ netstat -an|grep 10000            
tcp        0      0 0.0.0.0:10000           0.0.0.0:*               LISTEN

HiveServer2為客戶端在遠(yuǎn)程執(zhí)行hive查詢提供了接口,通過Thrift RPC來實現(xiàn),還提供了多用戶并發(fā)和認(rèn)證功能。目前python可以通過pyhs2這個模塊來連接HiveServer2,實現(xiàn)查詢和取回結(jié)果的操作。

不過pyhs2已經(jīng)不在維護(hù),追新的可以參考另外2個很好的python package(已經(jīng)被證明pyhs2存在性能瓶頸,最好盡快切換到pyhive)

https://github.com/dropbox/PyHive

https://github.com/cloudera/impyla

安裝sasl失敗的話,先安裝:
yum install gcc-c++ python-devel.x86_64 cyrus-sasl-devel.x86_64

pyhs2的項目托管在github之上,地址為https://github.com/BradRuderman/pyhs2或在https://pypi.python.org/pypi/pyhs2/0.2直接下載

如果安裝不成功,可以嘗試先安裝以下的組件:

yum install cyrus-sasl-plain
yum install cyrus-sasl-devel

安裝時如果遇到報錯: 

error: sasl/sasl.h: No such file or directory

可以嘗試先安裝sasl , ubantu可以用sudo apt-get install libsasl2-dev, CentOS可以使用anaconda的pip安裝, 或者按照以下步驟安裝:

curl -O -L ftp://ftp.cyrusimap.org/cyrus-sasl/cyrus-sasl-2.1.26.tar.gz
tar xzf cyrus-sasl-2.1.2.26.tar.gz
cd cyrus-sasl-2.1.26.tar.gz
./configure && make install


最后附上測試代碼:
# -*- coding:utf-8 -*-
'''
采用Hive和thrift方式連接數(shù)據(jù)庫
'''
import pyhs2
import sys
reload(sys)
sys.setdefaultencoding('utf8')

class HiveClient:
    def __init__(self, db_host, user, password, database, port=10000, authMechanism="PLAIN"):
      
        self.conn = pyhs2.connect(host=db_host,
                                  port=port,
                                  authMechanism=authMechanism,
                                  user=user,
                                  password=password,
                                  database=database,
                                  )

    def query(self, sql):
        with self.conn.cursor() as cursor:
            cursor.execute(sql)
            return cursor.fetch()

    def close(self):
        self.conn.close()


def main():
    """
    main process
    @rtype:
    @return:
    @note:

    """
    hive_client = HiveClient(db_host='10.24.33.3', port=10000, user='hadoop', password='hadoop',
                             database='fmcm', authMechanism='PLAIN')
    result = hive_client.query('select * from fm_news_newsaction limit 10')
    print result
    hive_client.close()


if __name__ == '__main__':
    main()

以上就是python怎樣通過thrift方式連接hive,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享題目:python怎樣通過thrift方式連接hive
URL鏈接:http://muchs.cn/article10/pdpddo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、面包屑導(dǎo)航、響應(yīng)式網(wǎng)站、網(wǎng)站內(nèi)鏈品牌網(wǎng)站建設(shè)、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)