python Hbase Thrift pycharm 及引入包
cp -r hbase/ /usr/lib/python2.7/site-packages/ 官方示例子http://code.google.com/p/hbase-thrift/source/browse/trunk/python/test/tables.pyhttp://yannramin.com/2008/07/19/using-facebook-thrift-with-python-and-hbase/http://wiki.apache.org/hadoop/Hbase/ThriftApi 将生成的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
好像需要源码,我反正没找到src目录,忘记了 。。。。。。 忘记当初自己怎么装的了。
# --*-- coding:utf-8 --*--import sys import time# 所有thirft编程都需要的 from thrift import Thrift from thrift.transport import TSocket, TTransport from thrift.protocol import TBinaryProtocol # Hbase的 客户端代码 from hbase import ttypes from hbase.Hbase import Client, ColumnDescriptor, Mutation# make socket 这里配置的是hbase zookeeper的地址,因为master只负责负载均衡,读写由zookeeper协调 transport = TSocket.TSocket('localhost', 9090)# buffering is critical . raw sockets are very slow transport = TTransport.TBufferedTransport(transport)# wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport)# create a client to use the protocol encoder client = Client(protocol)# connect transport.open()t = 'tab2'# 扫描所有表获取所有表名称 print 'scanning tables ......' for table in client.getTableNames():print 'found:%s' % tableif client.isTableEnabled(table):print ' disabling table: %s' % t# 置为无效 client.disableTable(table)print 'deleting table: %s' % t# 删除表 client.deleteTable(table)# 创建表 columns = [] col = ColumnDescriptor() col.name = 'entry:' col.maxVersions = 10 columns.append(col) col = ColumnDescriptor() col.name = 'unused:' columns.append(col)try:print 'creating table : % s' % tclient.createTable(t, columns) except Exception, ae:print 'Warn:' + ae.message# 插入数据 invalid = 'foo-\xfc\xa1\xa1\xa1\xa1\xa1' valid = 'foo-\xE7\x94\x9F\xE3\x83\x93\xE3\x83\xBC\xE3\x83\xAB'# non-utf8 is fine for data mutations = [Mutation(column='entry:foo', value=invalid)] print str(mutations) client.mutateRow(t, 'foo', mutations) # foo is row key# try empty strings # cell value empty mutations = [Mutation(column='entry:foo', value='')] # rowkey empty client.mutateRow(t, '', mutations)#this row name is valid utf8 mutations = [Mutation(column='entry:foo', value=valid)] client.mutateRow(t, valid, mutations)# run a scanner on the rows we just created # 全表扫描 print 'starting scanner...' scanner = client.scannerOpen(t, '', ['entry:'])r = client.scannerGet(scanner) while r:#printRow(r[0])r = client.scannerGet(scanner) print 'scanner finished '# 范围扫描 columnNames = [] for (col, desc) in client.getColumnDescriptors(t).items():print 'column with name:', desc.nameprint desccolumnNames.append(desc.name + ':')print 'stating scanner...' scanner = client.scannerOpenWithStop(t, '00020', '00040', columnNames)r = client.scannerGet(scanner) while r:# printRow(r[0])r = client.scannerGet(scanner)client.scannerClose(scanner) print 'scanner finished'# 关闭socket transport.close()
现在我们就可以用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,BatchMutationclass 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:breakcontents = result.columns["contents:"].value#print contentsself.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)
http://blog.csdn.net/poechant/article/details/6618264
http://mmicky.blog.163.com/blog/static/150290154201311801519681/ 按照这个配置python hbase开发环境
编程前切换到/usr/program/python/hbase 然后运行python
>>>from thrift.transport import TSocket
>>>from thrift.protocol import TBinaryProtocol
>>>from hbase import Hbase
都不报错,但是到pycharm报错,原因时python默认搜索当前目录。
到pycharm 需要把 /usr/program/python/hbase 添加到pycharm的path
操作步骤:File>>setting>>project interpreter>>python interpreter>>>paths>>>+ 把/usr/program/python/hbase 文件夹添加进去就好了。
__author__ = 'root'from thrift.transport import TSocket from thrift.protocol import TBinaryProtocol from hbase import Hbasetransport = TSocket.TSocket("localhost", 9090) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol) transport.open() tabs = client.getTableNames() print tabs
python Hbase Thrift pycharm 及引入包相关推荐
- 超适合新手使用的教程:Python环境配置+Pycharm安装+扩展包安装(以Numpy+mkl为例)
目录 一.Python环境配置 1.Python下载 2.python安装 3.Python验证安装 二.Pycharm环境配置 1.pycharm下载 2.pycharm配置python 3.pyc ...
- python hbase 报错by_【hbase】使用thrift with python 访问HBase
HBase 版本: 0.98.6 thrift 版本: 0.9.0 使用 thrift client with python 连接 HBase 报错: 1 Traceback (most rece ...
- python中引入包的时候报错AttributeError: module ‘sys‘ has no attribute ‘setdefaultencoding‘解决方法?
python中引入包的时候报错AttributeError: module 'sys' has no attribute 'setdefaultencoding'解决方法? 参考文章: (1)pyth ...
- python飞机大战(只需要两个python文件)附带pycharm的导包方法
两个python文件实现飞机大战(附带pycharm的导包方法) 开发工具:pycharm 相关注释直接在代码中讲解 运行结果图 代码: plane_main.py import pygame fro ...
- hbase/thrift/go连接失败
问题 在通过Go连接hbase的过程中, 发现 get操作可以查到数据, 但是scanner命令访问数据失败, 也没有报错, 就是单纯的查不到数据. 而且Python PHP都一切正常. 这里简单复述 ...
- Python IDE ——Anaconda+PyCharm的安装与配置
文章目录 ##一 前言 最近莫名其妙地想学习一下Python,想着利用业余时间学习一下机器学习(或许仅仅是脑子一热吧).借着研究生期间对于PyCharm安装的印象,在自己的电脑上重新又安装了一遍.利用 ...
- python软件怎么使用-Python快速入门—如何选择使用包管理工具?
原标题:Python快速入门-如何选择使用包管理工具? 源 | cnblogs文 | 包子 在Python环境中已经有很多成熟的包,可以通过安装这些包来扩展我们的程序. 例如,很多时候Python开发 ...
- HBase thrift C++编程
HBase & thrift & C++编程.pdf 目录 目录 1 1. 前言 1 2. 启动和停止thrift2 1 2.1. 启动thrift2 1 2.2. 停止thrift2 ...
- HBase: Thrift写数据报错——socket.error: [Errno 32] Broken pip
博主用的是python来读写hbase 需要安装 pip install thrift 和 pip install hbase-thrift hbase客户端创建: from thrift impor ...
最新文章
- MECAT: fast mapping,error correction, and de novo assembly for single-molecule sequencing reads
- C# 利用SharpPcap实现网络包捕获嗅探
- Oracle EXPDP/IMPDP示例
- maven deploy上传本地jar至私服
- MCMC方法与变分推断
- 鬼吹灯-漫谈大型网站的架构
- 尴尬了!迪士尼官方声明:从未与VIPKID有任何层面的业务合作关系
- PCB名詞解釋:通孔、盲孔、埋孔(转载)
- Spark中sortByKey和sortBy对(key,value)数据分别 根据key和value排序
- Could not load file or assembly App_Licenses.dll Could not load file or assembly App_Web_
- NTKO控件安装:“不能装载文档控件,请在检查浏览器的选项中检查浏览器的安全设置”问题
- 个性化制作nodemcu-firmware(esp8266/esp8285 固件制作)----包含lua程序bin的制作
- 智慧交通信号控制系统梗概
- 应用市场首发,APP推广如何应对
- 最优化路径和火车票退票
- 趣味密码学入门--cryptohack
- Window10下解决弹出兼容性助手对话框的方法
- PhoneGap移动开发框架2
- 用Linux开发板制作智能音箱,【工程师实战】只要几步,普通音箱秒变小度智能音箱...
- Jquery实现超酷的时间轴特效