python hbase API (一) thrift2環境準備

一、搭建hbase集羣,安裝thrift服務並啓動

安裝步驟:https://blog.csdn.net/qq_21153619/article/details/82250725

  • cd /usr/hdp/2.6.2.0-205/hbase/bin/
  • ./hbase-common.sh start thrift

二、Thrift API 

在網上很多人說在hbase根目錄下存在Hbase.thrift文件,但是我沒有找到,沒辦法,只能去官網下載

http://archive.apache.org/dist/hbase/1.1.2/


hbase-1.1.2-bin.tar.gz 這個文件,然後找到裏面的hbase-thrift文件夾,解壓出來,拷貝到任意目錄,然後使用如下命令,生成python使用的Thrift API: 

  • mkdir -p /usr/local/hbase
  • tar -zxvf hbase-1.1.2-src.tar.gz 
  • cd /usr/local/hbase/hbase-1.1.2/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift
  • thrift –gen py Hbase.thrif 


然後把生成的gen-py文件夾下的hbase文件夾,拷貝到python的包路徑下(/usr/local/python3/lib/python3.6/site-packages)就可以使用了。 
後期修改:發現這個包生成的hbase包已經不能用了,需要使用pip安裝hbase-thrift即可。

三、安裝thrift和hbase-thrift

  • pip install thrift
  • pip install hbase-thrift

注:若裝conda,可以用conda install ... 安裝

安裝完成之後第一次運行,報錯誤:

in <module> from hbase import Hbase File 
"C:\Users\tianxiao\AppData\Local\Programs\Python\Python36\lib\site-packages\hbase\Hbase.py", line 2066 
except IOError, io: ^ SyntaxError: invalid syntax

原因是python版本帶來的語法兼容性問題,下載python3的Hbase文件,替換Hbase文件/usr/local/python3/lib/python3.6/site-packages/hbase/Hbase.py和ttypes.py
下載地址爲:https://github.com/626626cdllp/infrastructure/tree/master/hbase

四、hbase API

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

from thrift import Thrift
from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import *
import pandas as pd
from hbase.Hbase import *

class hbaseUtils(object):
    __slots__ = ['transport', 'client']

    # @staticmethod
    def __init__(self):
        # server端地址和端口,web是HMaster也就是thriftServer主機名,9090是thriftServer默認端口
        transport = TSocket.TSocket('172.8.10.145', 9090)
        # 可以設置超時
        transport.setTimeout(5000)
        # 設置傳輸方式(TFramedTransport或TBufferedTransport)
        self.transport = TTransport.TBufferedTransport(transport)
        # 設置傳輸協議
        protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
        # 確定客戶端
        self.client = Hbase.Client(protocol)

詳細API見:python hbase  API (二) 

https://blog.csdn.net/qq_21153619/article/details/86502624

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章