python2.7.12操作Hbase

摘要:
节俭跑步设置复制安装…完成成功安装节俭-0.1.0(ma)hadoop@master:/usr/local/pycharm/bin$sudopipinstallhbase rift[sudo]hadoop密码:(ma)hadoop@master :

前置条件:您已经安装好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')]

























免责声明:文章转载自《python2.7.12操作Hbase》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇iOS二十种超酷时尚艺术滤镜汇总【附源码】Linux 进程管理 ps、top、pstree命令下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Java代码常用写法总结

1.字符串是否为空判断 以下是java 判断字符串是否为空的四种方法:方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低: if(s == null ||"".equals(s));方法二: 比较字符串长度, 效率高, 是我知道的最好一个方法:                      if(s == null || s.length() <...

Halcon 识别车牌学习笔记

有一张车牌照片如下: read_image (Image, 'C:/Users/LiZhiqiang/Desktop/车牌2.jpg')//读取照片 rgb1_to_gray (Image, GrayImage)//转化成灰度图像 threshold (GrayImage, Regions, 21, 163)//设定阈值 connection (Reg...

iphone(ios)不同设备的内存和游戏不闪退峰值

ios内存限制 不同内存的苹果机型上(1G,2G,3G,4G…),游戏内存的峰值一般最高多少能保证不闪退? 一般来讲最保险的就是不超过机器总内存的50%,具体每个机型的内存限制在列出在下面。 原贴:《ios app maximum memory budget》 注意事项:查看当前app占用多少内存,通过OS的API来获取,而不要通过引擎自己的API来获取。...

C语言中的stdin,stdout,stderr[转]

我们在写C程序时经常遇到printf(),fprintf(),perror(),这些东西到底有什么作用。说到这不得不提及stdin,stdout,stderr。想想,我们在用C去写文件时的操作,File *fp=fopen(),这个fp就是我们向系统申请的,相当于一通往文件的通道。 其实,stdin,stdout,stderr就是这个fp,不过他是随着计算...

Makefile简介

一、为什么要写Makefile         首先要确定我们的目标,Makefile是用来干嘛的?         曾经很长时间我都是在从事Windows环境下的开发,所以根本不知道Makefile是个什么东西。因为早已经习惯了使用VS、Eclipse等等优秀的IDE做开发,只要点一个按钮,程序就可以运行啦。但是进入公司以后,从事的是Unix环境下的开发...

Oracle学习笔记:使用replace、regexp_replace实现字符替换、姓名脱敏

  在数据库中难免会遇到需要对数据进行脱敏的操作,无论是姓名,还是身份证号。   最近遇到一个需求,需要对姓名进行脱敏: 姓名长度为2,替换为姓+*; 姓名长度为3,替换中间字符为*; 姓名长度为4,替换第3个字符为*;   经过一番搜索之后,最终找到了3种方式的实现,具体如下。 一、先查找,再替换 select replace('陈宏宏',subst...