前置条件:您已经安装好Hbase、python2.7

题外话:最好自己安装个虚拟环境,以下操作都是在虚拟环境中的

(ma) hadoop@master:/usr/local/pycharm/bin$ sudo pip install thrift
[sudo] password for hadoop:
The directory '/home/hadoop/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/hadoop/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting thrift
  Downloading thrift-0.10.0.zip (87kB)
    100% |████████████████████████████████| 92kB 415kB/s
Requirement already satisfied: six>=1.7.2 in /usr/local/lib/python2.7/dist-packages (from thrift)
Installing collected packages: thrift
  Running setup.py install for thrift ... done
Successfully installed thrift-0.10.0
 
(ma) hadoop@master:/usr/local/pycharm/bin$ sudo pip install hbase-thrift
[sudo] password for hadoop:
The directory '/home/hadoop/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/hadoop/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting hbase-thrift
  Downloading hbase-thrift-0.20.4.tar.gz
Requirement already satisfied: Thrift in /usr/local/lib/python2.7/dist-packages (from hbase-thrift)
Requirement already satisfied: six>=1.7.2 in /usr/local/lib/python2.7/dist-packages (from Thrift->hbase-thrift)
Installing collected packages: hbase-thrift
  Running setup.py install for hbase-thrift ... done
Successfully installed hbase-thrift-0.20.4

Hbase的bin目录下启动bin/./hbase-daemon.sh start thrift
hadoop@master:/opt/Hadoop/hbase-1.3.1/bin$ ./hbase-daemon.sh start thrift
启动pycharm
注意在虚拟环境中启动,其它环境中有可能程序运行不了。
(ma) hadoop@master:/usr/local/pycharm/bin$ ./pycharm.sh

参考文档:http://www.cnblogs.com/hitandrew/archive/2013/01/21/2870419.html,此文档中有的例子运行有问题

创建hbase表:

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 *

transport = TSocket.TSocket('localhost', 9090);

transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport);

client = Hbase.Client(protocol)
transport.open()

contents = ColumnDescriptor(name='cf:', maxVersions=1)
client.createTable('test', [contents])

print client.getTableNames()

输出内容:
/usr/bin/python2.7 /home/py/PycharmProjects/ThirdTest/testThrift.py
['member', 'test']

Process finished with exit code 0

在hbase shell中用list查看有刚才创建的test.

插入数据:

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 *

transport = TSocket.TSocket('localhost', 9090)

transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)

transport.open()

row = 'row-key1'

mutations = [Mutation(column="cf:a", value="1")]
client.mutateRow('test', row, mutations)

在hbase shell中用scan 'test'查看有刚才创建的test.

hbase(main):001:0> scan 'test'
ROW                   COLUMN+CELL                                               
 row-key1             column=cf:a, timestamp=1506406128150, value=1             
1 row(s) in 0.3570 seconds

获取一行数据:

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 *

transport = TSocket.TSocket('localhost', 9090)
transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)

transport.open()

tableName = 'test'
rowKey = 'row-key1'

result = client.getRow(tableName, rowKey)
print result
for r in result:
    print 'the row is ' , r.row
    print 'the values is ' , r.columns.get('cf:a').value

输出内容:

/usr/bin/python2.7 /home/py/PycharmProjects/ThirdTest/getOneRow.py
[TRowResult(columns={'cf:a': TCell(timestamp=1506406612641, value='2')}, row='row-key1')]
the row is  row-key1
the values is  2

查询多行:
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 *

transport = TSocket.TSocket('localhost', 9090)
transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)
transport.open()

tableName = 'test'
id = client.scannerOpenWithStop(tableName,'','','')

result2 = client.scannerGetList(id, 10)

print result2

输出内容:

/usr/bin/python2.7 /home/py/PycharmProjects/ThirdTest/getMultiRow.py
[TRowResult(columns={'cf:a': TCell(timestamp=1506406612641, value='2')}, row='row-key1'), TRowResult(columns={'cf:a': TCell(timestamp=1506406650902, value='2')}, row='row-key2')]

转载于:https://www.cnblogs.com/herosoft/p/8134173.html

python2.7.12操作Hbase相关推荐

  1. python hbase_Python操作Hbase

    Python操作Hbase的增删改查操作 增改数据put, row操作 put(row, data, timestamp=None, wal=True)---> 插入数据,无返回值 row--- ...

  2. python提取hbase数据_详解python操作hbase数据的方法介绍

    配置 thrift python使用的包 thrift 个人使用的python 编译器是pycharm community edition. 在工程中设置中,找到project interpreter ...

  3. HBaseAPI——IDEA操作HBase数据库HBase与Hive的集成

    目录 一.IDEA操作HBase数据库 (一)添加依赖 (二)配置log4j (三)IDEA连接HBase并插入数据 1.代码实现 2.查看命名空间的表 (四)java操作HBase数据库--单元测试 ...

  4. hive删除hbase数据_Hive进阶:Hive通过外部表操作Hbase数据

    概述: HBase: 查询效率比较高,常为实时业务提供服务,但是其查询方式比较单一,只能通过row方式get单条数据,或者通过scan加过滤器的方式扫描数据表获取数据. Hive: hive用来存储结 ...

  5. PHP通过Thrift操作Hbase

    HBase是一个开源的NoSQL产品,它是实现了Google BigTable论文的一个开源产品,和Hadoop和HDFS一起,可用来存储和处理海量column family的数据.官方网址是:htt ...

  6. linux升级Python2.7.12

    第一步:下载 # wget https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tar.xz 第二步: 解压刚刚下载的压缩包 #tar -xv ...

  7. HBase 6、用Phoenix Java api操作HBase

    开发环境准备:eclipse3.5.jdk1.7.window8.hadoop2.2.0.hbase0.98.0.2.phoenix4.3.0 1.从集群拷贝以下文件:core-site.xml.hb ...

  8. c# 访问hbase_大数据技术 windows下C#通过Thrift操作HBase

    本篇教程探讨了大数据技术 windows下C#通过Thrift操作HBase,希望阅读本篇文章以后大家有所收获,帮助大家对大数据技术的理解更加深入. < 1.到apache官网下载Thrift源 ...

  9. win7 64+python2.7.12安装numpy+scipy+matplotlib+scikit-learn

    win7 64+python2.7.12安装numpy+scipy+matplotlib+scikit-learn python包下载网址 http://www.lfd.uci.edu/~gohlke ...

最新文章

  1. 【转】C# using的三种使用方法
  2. 如何用python驱动器调用neo4j算法包
  3. RabbitMQ的消息确认ACK机制
  4. docker(iptables)目标地址转换,运行中的容器映射端口
  5. python linux调试_python调试
  6. php 滑动 图片,JQuery图片滑动
  7. Codeforces 895 B XK Segments 思维 二分
  8. 为链接加上加载等待信息
  9. Firewalld 允许指定IP访问端口
  10. 2019美团技术沙龙合辑PPT下载
  11. 微信小程序,Python爬虫抓包采集实战,采集某成考题库小程序
  12. 社会调查报告包括哪几个部分?
  13. 解决PHP上传文件时大小受限制问题
  14. 跟谁一起工作,到底有多重要?
  15. 【项目总结】论文复现与改进:一般选择模型的产品组合优化算法(Research@收益管理)
  16. 电脑打不开浏览器--解决方法
  17. 北京农行研发中心面试总结(夏季实习生)
  18. 软硬件协同设计的系统级开发环境~BPS软件介绍
  19. 财付通找不到服务器,为什么我的财付通就是打不开啊?一直都是找不到服务器?...
  20. Intel CPU集成显卡被UEFI BIOS禁用想开启的设置

热门文章

  1. POJ3070 Fibonacci(矩阵快速幂)
  2. Java基础学习总结(24)——Java单元测试之JUnit4详解
  3. asp.net 研发,测试,或现网....非本机环境采用附加进程的方式在本地调试
  4. 创建VLAN的两种方法
  5. SAS 中计算总和或者计算总数的方法
  6. Vue CLI 3 脚手架搭建
  7. MVC与单元测试实践之健身网站(三)-角色与权限
  8. 第一阶段冲刺周期10天的第6天进展报告
  9. Java的值传递和引用值传递的区别
  10. 34、JS/AJAX