HBase python client

HBase python client

介绍

hbase 提供thrift接口,python可通过该接口和hbase通信。happybase是python基于thrift协议的一个hbase客户端库,其配置使用简单。使用步骤如下:

happybase 使用

  1. 启动hbase master 节点上的thrift接口服务:
1
$ bin/hbase thrift start

thrift默认端口是9090。

  1. 安装happybase

    1
    
    $ pip install happybase
    
  2. python

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    import happybase
    
    hbase_host='10.0.48.219'
    conn = happybase.Connection(hbase_host)
    print conn.tables()
    
    conn.create_table('table_name', {'cf1': dict(), 'cf2': dict()})
    t1 = conn.table('table_name')
    t1.put('row-key-1', {'cf1:name1':'value1'})
    
    #
    t1.row('row-key-1')
    
    t1.delete('row-key-1')
    
    '''
    批量
    '''
    b=t1.batch()
    b.put(...)
    ...
    b.send()
    
    1
    
    
    

常见问题

  1. TTransportException: TTransportException(message='TSocket read 0 bytes', type=4)

    • 原因:thrift 的server端和client端的协议不匹配造成的。

    • 解决

      修改hbase-site.xml,禁用TFramedTransport和TCompactProtocol功能,即:

      1
      2
      3
      4
      5
      6
      7
      8
      
         <property>
            <name>hbase.regionserver.thrift.framed</name>
            <value>false</value>
         </property>
         <property>
            <name>hbase.regionserver.thrift.compact</name>
            <value>false</value>
         </property>
      
updatedupdated2024-05-102024-05-10