您好,欢迎来到要发发知识网。
搜索
您的当前位置:首页python 通过thrift访问hbase

python 通过thrift访问hbase

来源:要发发知识网

服务器环境
安装chd5.9.1

通过cloudera manager启用 HBase Thrift Server 实例,此时就可以通过thrift来访问hbase了。

客户端

在要访问hbase的电脑安装python,然后通过pip 安装thrift 和 hbase-thrift

pip install thrift
pip install hbase-thrift

例子

1、新建一个Hbase_client.py文件,然后将以下内容复制到里面

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

from hbase import Hbase
from hbase.ttypes import *

class HbaseClient(object):
    def __init__(self, host='HBase Thrift Server的ip地址', port=9090):
        self.transport = TTransport.TBufferedTransport(TSocket.TSocket(host, port))
        protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
        self.client = Hbase.Client(protocol)

    def get_tables(self):
        self.transport.open()
        tables = self.client.getTableNames()
        self.transport.close()
        return tables

2、新建一个text.py文件然后输入一下内容

import Hbase_client as hc

client = hc.HbaseClient()
print client.get_tables()

3、运行text.py文件,就可以获取到hbase中所有的表了。

Copyright © 2019- net188.cn 版权所有 湘ICP备2022005869号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务