前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python连接HBase

python连接HBase

作者头像
py3study
发布2020-01-07 11:59:14
2.4K0
发布2020-01-07 11:59:14
举报
文章被收录于专栏:python3python3

环境

hadoop 2.7.0 hbase 1.2.1 Thrift 0.9.0

启动hbase的Thrift RPC

代码语言:javascript
复制
./hbase-daemon.sh start thrift

生成python的Thrift模块

代码语言:javascript
复制
cd hbase-1.2.1/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift
thrift --gen py Hbase.thrift

#生成gen-py文件
.
├── gen-py
│   ├── hbase
│   │   ├── constants.py
│   │   ├── Hbase.py
│   │   ├── Hbase-remote
│   │   ├── __init__.py
│   │   └── ttypes.py
│   └── __init__.py
└── Hbase.thrift
#把gen-py/hbase复制到项目下
代码语言:javascript
复制
.
├── hbase
│   ├── constants.py
│   ├── Hbase.py
│   ├── Hbase.pyc
│   ├── Hbase-remote
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── ttypes.py
│   └── ttypes.pyc
└── hbase_client.py

hbase_client.py

代码语言:javascript
复制
# # -*- coding: utf-8 -*-

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

from hbase import Hbase
from hbase.ttypes import ColumnDescriptor, Mutation


class HbaseClient(object):
    def __init__(self, host='localhost', port=9090):
        transport = TTransport.TBufferedTransport(TSocket.TSocket(host, port))
        protocol = TBinaryProtocol.TBinaryProtocol(transport)
        self.client = Hbase.Client(protocol)
        transport.open()

    def get_tables(self):
        """
        获取所有表
        """
        return self.client.getTableNames()

    def create_table(self, table, *columns):
        """
        创建表
        """
        self.client.createTable(table, map(lambda column: ColumnDescriptor(column), columns))

    def put(self, table, row, columns, attributes=None):
        """
        添加记录
        @:param columns {"k:1":"11"}
        """
        self.client.mutateRow(table, row, map(lambda (k,v): Mutation(column=k, value=v), columns.items()), attributes)

    def scan(self, table, start_row="", columns=None, attributes=None):
        """
        获取记录
        """

        scanner = self.client.scannerOpen(table, start_row, columns, attributes)
        while True:
            r = self.client.scannerGet(scanner)
            if not r:
                break
            yield dict(map(lambda (k, v): (k, v.value),r[0].columns.items()))

if __name__ == "__main__":
    client = HbaseClient("192.168.19.74", 9090)
    client.create_table("student", "name", "coruse")
    print(client.get_tables())
    client.put("student", "1", {"name:":"zhangsan", "coruse:art": "88", "coruse:math": "12"})
    client.put("student", "2", {"name:":"lisi", "coruse:art": "90", "coruse:math": "100"})
    client.put("student", "3", {"name:":"lisi2"})
    for v in client.scan("student", columns=["name"]):
        print(v)
    for v in client.scan("student"):
        print(v)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 环境
  • 启动hbase的Thrift RPC
  • 生成python的Thrift模块
相关产品与服务
TDSQL MySQL 版
TDSQL MySQL 版(TDSQL for MySQL)是腾讯打造的一款分布式数据库产品,具备强一致高可用、全球部署架构、分布式水平扩展、高性能、企业级安全等特性,同时提供智能 DBA、自动化运营、监控告警等配套设施,为客户提供完整的分布式数据库解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档