Python连接HBase

测试环境:Python3.5、HBase1.2.0

1、安装依赖包

sudo pip install thrift
sudo pip install hbase-thrift

2、替换本地py文件(python3版本需要)
替换/usr/local/lib/python3.5/site-paceages/hbase/下的Hbase.py、ttypes.py两个文件;
获取路径:https://github.com/626626cdllp/infrastructure/tree/master/hbase

否则会出现错误:except IOError, io:
Python连接HBase_第1张图片
3、确认HBase Thrift Server的协议和传输方式配置

hbase.regionserver.thrift.compact=false
hbase.regionserver.thrift.framed=false

4、测试连接HBase(步骤3配置均为flase)

#-*- coding: utf-8 -*-
from thrift.transport import TSocket ,TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase

#server端地址和端口,web是HMaster也就是thriftServer主机名,thriftServer默认端口是9090
socket = TSocket.TSocket('192.168.1.145',9090)
#可以设置超时
socket.setTimeout(5000)

#设置传输方式(TFramedTransport或TBufferedTransport)
transport = TTransport.TBufferedTransport(socket)

#设置传输协议,缺省简单的二进制序列化协议
protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)
transport.open()

print(client.getTableNames())

5、测试连接HBase(步骤3配置均为true)

#-*- coding: utf-8 -*-

from thrift.transport import TSocket ,TTransport
from thrift.protocol import TCompactProtocol
from hbase import Hbase

#server端地址和端口,web是HMaster也就是thriftServer主机名,thriftServer默认端口是9090
socket = TSocket.TSocket('192.168.1.145',9090)
#可以设置超时
socket.setTimeout(5000)

#设置传输方式(TFramedTransport或TBufferedTransport)
transport = TTransport.TFramedTransport(socket)

#设置传输协议,高效的二进制序列化协议
protocol = TCompactProtocol.TCompactProtocol(transport)

client = Hbase.Client(protocol)
transport.open()

print(client.getTableNames())

你可能感兴趣的:(Python连接HBase)