1.看不同语言对hbase的thrift访问demo,可以参考examples/thrift/DemClient.*,有c++,java,php,pl,py,rb等多种语言
2.使用python访问hbase需要借助thrift的帮助,首先通过 --gen生成python的hbase的thrift脚本
使用命令:
cd $HBASE_HOME
thrift --gen py /src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift
这样在当前目录就生成了gen-py目录
Hbase.py 中定义了一些HbaseClient可以使用的方法
ttypes.py中定义了HbaseClient传输的数据类型
将生成的hbase目录copy到python的包下
cp -r hbase /usr/lib/python2.4/site-packages/
3。启动hbase和thrift服务
./bin/start-hbase.sh
./bin/hbase-daemon.sh start thrift
现在我们就可以用python来和hbase通信了
#-*-coding:utf-8 -*-
#!/usr/bin/python
from thrift import Thrift
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,BatchMutation
class HbaseWriter:
"""
IP地址
端口
表名
"""
def __init__(self,address,port,table='user'):
self.tableName = table
#建立与hbase的连接
self.transport=TTransport.TBufferedTransport(TSocket.TSocket(address,port))
self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
self.client=Hbase.Client(self.protocol)
self.transport.open()
tables = self.client.getTableNames()
if self.tableName not in tables:
print "not in tables"
self.__createTable()
self.write("hell,babay!!!")
self.read()
#关闭
def __del__(self):
self.transport.close()
#建表
def __createTable(self):
col1 = ColumnDescriptor(name="person:",maxVersions=1)
col2 = ColumnDescriptor(name="contents:",maxVersions=1)
col3 = ColumnDescriptor(name="info:",maxVersions=1)
self.client.createTable(self.tableName,[col1,col2,col3])
def write(self,content):
row="abc"
mutations=[Mutation(column="person:",value=content),Mutation(column="info:",value=content)]
self.client.mutateRow(self.tableName,row,mutations)
def read(self):
scannerId = self.client.scannerOpen(self.tableName,"",["contents:",])
while True:
try:
result = self.client.scannerGet(scannerId)
except:
break
contents = result.columns["contents:"].value
#print contents
self.client.scannerClose(scannerId)
if __name__ == "__main__":
client = HbaseWriter("192.168.239.135","9090","person")
我们看下使用thrift生成的代码中都提供了那些方法
提供的方法有:
void enableTable(Bytes tableName)
enable表
void disableTable(Bytes tableName)
disable表
bool isTableEnabled(Bytes tableName)
查看表状态
void compact(Bytes tableNameOrRegionName)
void majorCompact(Bytes tableNameOrRegionName)
getTableNames()
getColumnDescriptors(Text tableName)
getTableRegions(Text tableName)
void createTable(Text tableName, columnFamilies)
void deleteTable(Text tableName)
get(Text tableName, Text row, Text column)
getVer(Text tableName, Text row, Text column, i32 numVersions)
getVerTs(Text tableName, Text row, Text column, i64 timestamp, i32 numVersions)
getRow(Text tableName, Text row)
getRowWithColumns(Text tableName, Text row, columns)
getRowTs(Text tableName, Text row, i64 timestamp)
getRowWithColumnsTs(Text tableName, Text row, columns, i64 timestamp)
getRows(Text tableName, rows)
getRowsWithColumns(Text tableName, rows, columns)
getRowsTs(Text tableName, rows, i64 timestamp)
getRowsWithColumnsTs(Text tableName, rows, columns, i64 timestamp)
void mutateRow(Text tableName, Text row, mutations)
void mutateRowTs(Text tableName, Text row, mutations, i64 timestamp)
void mutateRows(Text tableName, rowBatches)
void mutateRowsTs(Text tableName, rowBatches, i64 timestamp)
i64 atomicIncrement(Text tableName, Text row, Text column, i64 value)
void deleteAll(Text tableName, Text row, Text column)
void deleteAllTs(Text tableName, Text row, Text column, i64 timestamp)
void deleteAllRow(Text tableName, Text row)
void deleteAllRowTs(Text tableName, Text row, i64 timestamp)
ScannerID scannerOpenWithScan(Text tableName, TScan scan)
ScannerID scannerOpen(Text tableName, Text startRow, columns)
ScannerID scannerOpenWithStop(Text tableName, Text startRow, Text stopRow, columns)
ScannerID scannerOpenWithPrefix(Text tableName, Text startAndPrefix, columns)
ScannerID scannerOpenTs(Text tableName, Text startRow, columns, i64 timestamp)
ScannerID scannerOpenWithStopTs(Text tableName, Text startRow, Text stopRow, columns, i64 timestamp)
scannerGet(ScannerID id)
scannerGetList(ScannerID id, i32 nbRows)
void scannerClose(ScannerID id)
不当之处,请指正
分享到:
相关推荐
python通过thrift访问hbase.docx
python3 使用 thrift 操作hbase 安装hbase-thirft后有一个Hbase报错 使用这个修改完成的Hbase类替换掉原来的Hbase类问题全部解决 主要是因为python版本兼容性带来的问题
python连接hbase需要用到hbase-thirft类库,但是hbase-thirft只在python2下能正常使用,如果在python3下,会报错,主要有一下几个错误 except IOError, io: SyntaxError: invalid syntax ModuleNotFoundError: No ...
python3通过thrift连接hbase模块修改版,其中hbase-python3,里面的path,根据实际实际路径修改.
最新的thrift2协议python链接hbase代码,附代码片段,get,put,delete...
因工作需要用python通过hbase的thrift服务读取Hbase表数据,发现公司的测试环境还不支持,于是自己动手准备环境,在此我将在安装步骤尽可能描述清楚,旨在给第一次动手安装的朋友,此过程亲测成功! 安装过程如下: ...
HBase是一个分布式的、面向列的开源数据库,其是Apache的Hadoop项目的子项目。这篇文章主要介绍了python3.7通过thrift操作hbase的示例代码,需要的朋友可以参考下
博客配套文件,详细演示了python通过thrift2接口查询hbase的方法。
博客配套文件,详细演示了python通过thrift1接口查询hbase的方法。
thrift生成好的python hbase库,配合thrift库可直接操作hbase
Python环境下通过thrift操作hbase所需的模块
Python 通过Thrift操作Hbase数据库 使用手册
hbase thrift python gen-py
hbase-thrift库需要替换的文件 替换项目 C:\ProgramData\Anaconda3\Lib\site-packages\hbase
python使用的包 thrift 个人使用的python 编译器是pycharm community edition. 在工程中设置中,找到project interpreter, 在相应的工程下,找到package,然后选择 “+” 添加, 搜索 hbase-thrift (Python client ...
python3中直接安装hbase-thrift模块时,import会报Except语法错误 将资源解压后的Hbase.py和ttypes.py两个文件替换至site-packages/hbase/目录下即可解决