HBase Python API

來源:互聯網
上載者:User

標籤:沒有   code   min   table   主機   open   ati   base   編程   

HBase Python API

HBase通過thrift機制可以實現多語言編程,資訊通過連接埠傳遞,因此Python是個不錯的選擇

吐槽

博主在Mac上配置HBase,奈何Zoomkeeper一直報錯,結果Ubuntu虛擬機器上10min解決……但是虛擬機器裡沒有IDE寫Java代碼還是不方便,因此用Mac主機串連虛擬機器的想法孕育而生,這樣又可以愉快地使用主機的IDE了~

一、服務端啟動Hbase Thrift RPC

HBase的啟動方式有很多,這裡不再贅述,Ubuntu啟動HBase之後,啟動thrift

hbase-daemon.sh start thrift

預設的服務連接埠是9090

二、用戶端安裝依賴包
sudo pip install thriftsudo pip install hbase-thrift
三、編寫用戶端代碼
# coding=utf-8from thrift.transport import TSocketfrom thrift.transport.TTransport import TBufferedTransportfrom thrift.protocol import TBinaryProtocolfrom hbase import Hbasefrom hbase.ttypes import ColumnDescriptorfrom hbase.ttypes import Mutationclass HBaseClient(object):    def __init__(self, ip, port=9090):        """        建立與thrift server端的串連        """        # server端地址和連接埠設定        self.__transport = TBufferedTransport(TSocket.TSocket(ip, port))        # 設定傳輸協議        protocol = TBinaryProtocol.TBinaryProtocol(self.__transport)        # 用戶端        self.__client = Hbase.Client(protocol)        # 開啟串連        self.__transport.open()    def __del__(self):        self.__transport.close()    def get_tables(self):        """        獲得所有表        :return:表名列表        """        return self.__client.getTableNames()    def create_table(self, table, *columns):        """        建立表格        :param table:表名        :param columns:列族名        """        func = lambda col: ColumnDescriptor(col)        column_families = map(func, columns)        self.__client.createTable(table, column_families)    def put(self, table, row, columns):        """        添加記錄        :param table:表名        :param row:行鍵        :param columns:列名        :return:        """        func = lambda (k, v): Mutation(column=k, value=v)        mutations = map(func, columns.items())        self.__client.mutateRow(table, row, mutations)    def delete(self, table, row, column):        """        刪除記錄        :param table:表名        :param row:行鍵        """        self.__client.deleteAll(table, row, column)    def scan(self, table, start_row="", columns=None):        """        獲得記錄        :param table: 表名        :param start_row: 起始行        :param columns: 列族        :param attributes:        """        scanner = self.__client.scannerOpen(table, start_row, columns)        func = lambda (k, v): (k, v.value)        while True:            r = self.__client.scannerGet(scanner)            if not r:                break            yield dict(map(func, r[0].columns.items()))if __name__ == ‘__main__‘:    client = HBaseClient("10.211.55.7")    # client.create_table(‘student‘, ‘name‘, ‘course‘)    client.put("student", "1",               {"name:": "Jack",                "course:art": "88",                "course:math": "12"})    client.put("student", "2",               {"name:": "Tom", "course:art": "90",                "course:math": "100"})    client.put("student", "3",               {"name:": "Jerry"})    client.delete(‘student‘, ‘1‘, ‘course:math‘)    for v in client.scan(‘student‘):        print v
四、測試結果
{‘course:art‘: ‘88‘, ‘name:‘: ‘Jack‘}{‘course:art‘: ‘90‘, ‘name:‘: ‘Tom‘, ‘course:math‘: ‘100‘}{‘name:‘: ‘Jerry‘}
五、小結

有了Python介面後,編寫簡單任務指令碼變得非常方便,這大大得益於RPC機制,很好地解耦了Client和Server,方便開發人員合作。

HBase Python API

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.