python连接HBase
来源:互联网 发布:java与安卓 编辑:程序博客网 时间:2024/06/05 15:08
环境
hadoop 2.7.0
hbase 1.2.1
Thrift 0.9.0
启动hbase的Thrift RPC
./hbase-daemon.sh start thrift
生成python的Thrift模块
cd hbase-1.2.1/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thriftthrift --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复制到项目下
.├── hbase│ ├── constants.py│ ├── Hbase.py│ ├── Hbase.pyc│ ├── Hbase-remote│ ├── __init__.py│ ├── __init__.pyc│ ├── ttypes.py│ └── ttypes.pyc└── hbase_client.py
hbase_client.py
# # -*- coding: utf-8 -*-from thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom hbase import Hbasefrom hbase.ttypes import ColumnDescriptor, Mutationclass 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)
0 0
- python连接Hbase
- Python 连接 Hbase
- python连接HBase
- Python 连接Hbase (centos6.5)
- pyspark Python 连接 HBase thrift
- python通过thrift连接Hbase
- python操作hbase 远程连接
- Python 使用thrift连接hbase || 远程连接hbase
- [原创]Python通过Thrift连接HBase
- HBase连接
- Python 通过thrift接口连接Hbase读取存储数据
- Python通过thrift连接Hbase(详细配置步骤)
- python hbase
- hbase总结:hbase连接异常
- hive连接hbase方法
- c++连接hbase指南
- hbase连接java(转载)
- Java连接HBASE数据库
- 6.Spark streaming技术内幕 : Job动态生成原理与源码解析
- iOS通知模式
- android 跳转Intent (第三方应用) & 去掉标题栏 &可见性&透明背景
- Java编程思想第四版第六章学习——访问权限设置
- intellij idea无法访问静态资源
- python连接HBase
- day5.17总结_加载图片(圆圈和压缩、一级、二级缓存)
- 矩阵的三角分解算法实验报告
- ZOJ 3946 Highway Project(多属性边权最短路)
- Java设计模式之代理模式
- 改变DM6467的内存划分
- 设置FLAG_ACTIVITY_NEW_TASK导致Activity打开两次
- POJ——1195Mobile phones(二维树状数组点修改矩阵查询)
- mina服务关闭