cp -r hbase/ /usr/lib/python2.7/site-packages/

官方示例子
http://code.google.com/p/hbase-thrift/source/browse/trunk/python/test/tables.py
http://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./site-packages/
。启动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' % table
if 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' % t
client.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.name
print desc
columnNames.append(desc.name + ':') print 'stating scanner...'
scanner = client.scannerOpenWithStop(t, '', '', 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- -*-
#!/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=)
col2 = ColumnDescriptor(name="contents:",maxVersions=)
col3 = ColumnDescriptor(name="info:",maxVersions=)
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","","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 Hbase transport = TSocket.TSocket("localhost", 9090)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)
transport.open()
tabs = client.getTableNames()
print tabs

python Hbase Thrift pycharm 及引入包的更多相关文章

  1. python中引入包的时候报错AttributeError: module 'sys' has no attribute 'setdefaultencoding'解决方法?

    python中引入包的时候报错:import unittestimport smtplibimport timeimport osimport sysimp.reload(sys)sys.setdef ...

  2. Python入门之PyCharm的快捷键与常用设置和扩展(Win系统)

    1.  PyCharm的快捷键 2 . PyCharm的常用设置和扩展 ---------------------------------------------------------------- ...

  3. Python入门之PyCharm的快捷键与常用设置和扩展(Mac系统)

    1. 快捷键 2 . PyCharm的常用设置和扩展 ------------------------------------------------------------------------- ...

  4. (转载)Python 的 JPype 模块调用 Jar 包

    Python 的 JPype 模块调用 Jar 包 背景与需求 最近学习并安装使用了HttpRunner框架去尝试做接口测试,并有后续在公司推广的打算. HttpRunner由Python开发,调用接 ...

  5. PyCharm导入tensorflow包报错的问题

    [注]PyCharm导入tensorflow包报错的问题 若是你也遇到这个问题,说明你也没有理解tensorflow到底在哪里. 当安装了anaconda3.6后,在PyCharm中设置interpr ...

  6. 【Python】Java程序员学习Python(十)— 类、包和模块

    我觉得学习到现在应该得掌握Python的OOP编程了,但是现在还没有应用到,先留一个坑. 一.类和对象 说到类和对象其实就是在说面向对象编程,学完Java以后我觉得面向对象编程还是很不错的,首先封装了 ...

  7. [Python开发工具] Pycharm之快捷键

    [Python开发工具] Pycharm之快捷键 1 全局搜索: Ctrl+Shift+F,不过PyCharm的更强大, 你可以点选左侧某个目录后再按Ctrl+Shift+F, 这样默认会搜索改目录; ...

  8. HBase & thrift & C++编程

    目录 目录 1 1. 前言 1 2. 启动和停止thrift2 1 2.1. 启动thrift2 1 2.2. 停止thrift2 1 2.3. 启动参数 2 3. hbase.thrift 2 3. ...

  9. Golang&Python测试thrift

    接上篇,安装好之后,就开始编写IDL生成然后测试. 一.生成运行 参考 http://www.aboutyun.com/thread-8916-1-1.html 来个添加,查询. namespace ...

随机推荐

  1. memcached的基本命令(安装、卸载、启动、配置相关)

    memcached的基本命令(安装.卸载.启动.配置相关):-p 监听的端口 -l 连接的IP地址, 默认是本机  -d start 启动memcached服务 -d restart 重起memcac ...

  2. jsp页面写入中文到mysql时出现了乱码(转)

    今天自己在用jsp把中文写入mysql的时候出现乱码,从数据库中读取出来的时候也显示为“??”,感觉应该出现了编码转换过程中的字符信息丢失.然后在mysql中直接执行该命令,发现中文是正常的,所有认为 ...

  3. mac下versions 提交提示 SVN Working Copy xxx locked

    终端进入工程目录,执行 find . | grep ".svn/lock" | xargs rm

  4. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(27)-权限管理系统-分配用户给角色

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(27)-权限管理系统-分配用户给角色 分配用户给角色,跟分配角色给用户操作是基本一致的. 打开模块维护,展 ...

  5. SpringMVC DispatcherServlet 说明与web配置

    使用Spring MVC,配置DispatcherServlet是第一步. DispatcherServlet是一个Servlet,所以能够配置多个DispatcherServlet. Dispatc ...

  6. Extending Robolectric

    Robolectric is a work in progress, and we welcome contributions from the community. We encourage dev ...

  7. HTTP协议基础与实验

    一. HTTP协议(Hypetext Transfer Protoacal,超文本传输协议) HTTP协议规定了Web基本的运作过程,以及Web服务器之间的通信细节. Http协议采用客户端/服务器端 ...

  8. python s12 day3

    python s12 day3   深浅拷贝 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  9. HttpContext.Current

    HttpContext. Response 直接这样写会报错 是因为 httpcontext没有提供response 这个静态的方法. 通过这样写就可以 ASP.NET还为它提供了一个静态属性Http ...

  10. 表达式:使用API创建表达式树(5)

    一.ConditionalExpression:表达式 生成如 IIF((a == b), "a和b相等", "a与b不相等") 式子. 使用: Paramet ...