本地操作

  • 启动thrift服务:./bin/hbase-daemon.sh start thrift

  • hbase模块产生:

    • 下载thrfit源码包:thrift-0.8.0.tar.gz

    • 解压安装

    • ./configure

    • make

    • make install

  • 在thrift-0.8.0目录中,lib/py/build/lib.linux-x86_64-2.6/目录下存在thrift的python模块,拷贝出来即可

  • 生成hbase模块

    • 下载源码包:hbase-0.98.24-src.tar.gz

    • 解压,进入下面目录:hbase-0.98.24/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift

    • thrift --gen py Hbase.thrift

  • 创建表格

    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('master' , 9090)
    transport = TTransport.TBufferedTransport(transport)

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

    transport.open()

    base_info_contents = ColumnDescriptor(name='meta-data',maxVersions=1)
    other_info_contents = ColumnDescriptor(name='flags',maxVersions=1)

    client.createTable('new_music_table', [base_info_contents, other_info_contents])

    print client.getTableNames()
  • 写数据

    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('master' , 9090)
    transport = TTransport.TBufferedTransport(transport)

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

    transport.open()

    tableName = 'new_music_table'
    rowKey = '1100'

    mutations = [Mutation(column="meta-data:name" , value="wangqingshui"),\
    Mutation(column="meta-data:tag" , value="pop"),\
    Mutation(column="meta-data:is_valid" , value="TRUE")]
    client.mutateRow(tableName,rowKey,mutations,None)
  • 读数据

    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('master' , 9090)
    transport = TTransport.TBufferedTransport(transport)

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

    transport.open()

    tableName = 'new_music_table'
    rowKey = '1100'

    result = client.getRow(tableName,rowKey,None)
    for r in result:
       print 'the row is ',r.row
       print 'the name is ',r.columns.get('meta-data:name').value
       print 'the name is ',r.columns.get('meta-data:is_valid').value
    • 读多条数据

    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('master' , 9090)
    transport = TTransport.TBufferedTransport(transport)

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

    transport.open()

    tableName = 'new_music_table'

    scan = TScan()
    id = client.scannerOpenWithScan(tableName , scan , None)
    result = client.scannerGetList(id,10)

    for r in result:
       print '==============='
       print 'the row is ' , r.row
       for k,v in r.columns.items():
           print "\t".join([k,v.value])

    # 执行结果
    ===============
    the row is  1100
    meta-data:name  wangqingshui
    meta-data:is_valid      TRUE
    meta-data:tag   pop
    ===============
    the row is  2200
    meta-data:name  langdechuanshuo

python操作Hbase的更多相关文章

  1. python 操作 hbase

    python 是万能的,当然也可以通过api去操作big database 的hbase了,python是通过thrift去访问操作hbase 以下是在centos7 上安装操作,前提是hbase已经 ...

  2. 【Hbase三】Java,python操作Hbase

    Java,python操作Hbase 操作Hbase python操作Hbase 安装Thrift之前所需准备 安装Thrift 产生针对Python的Hbase的API 启动Thrift服务 执行p ...

  3. Hbase理论&&hbase shell&&python操作hbase&&python通过mapreduce操作hbase

    一.Hbase搭建: 二.理论知识介绍: 1Hbase介绍: Hbase是分布式.面向列的开源数据库(其实准确的说是面向列族).HDFS为Hbase提供可靠的底层数据存储服务,MapReduce为Hb ...

  4. Python操作HBase之happybase

    安装Thrift 安装Thrift的具体操作,请点击链接 pip install thrift 安装happybase pip install happybase 连接(happybase.Conne ...

  5. python 操作Hbase 详解

    博文参考:https://www.cnblogs.com/tashanzhishi/p/10917956.html 如果你们学习过Python,可以用Python来对Hbase进行操作. happyb ...

  6. 通过Python操作hbase api

    # coding=utf-8 # Author: ruin """ discrible: """ from thrift.transport ...

  7. 大数据自学5-Python操作Hbase

    在Hue环境中本身是可以直接操作Hbase数据库的,但是公司的环境不知道什么原因一直提示"Api Error:timed out",进度条一直在跑,却显示不出表. 但是在CDH后台 ...

  8. python连接hbase

    安装HBase HBase是一个构建在HDFS上的分布式列存储系统,主要用于海量结构化数据存储.这里,我们的目标只是为Python访问HBase提供一个基本的环境,故直接下载二进制包,采用单机安装.下 ...

  9. Python之操作HBASE数据库

    目前有两个库可以操作HBASE:hbase-thrift 和  happybase happybase使用起来比较简单方便,因此重点学习该库,hbase-thrift只做简要介绍. (一)hbase- ...

随机推荐

  1. hdu 1059 (多重背包) Dividing

    这里;http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意是有价值分别为1,2,3,4,5,6的商品各若干个,给出每种商品的数量,问是否能够分成价值相等的 ...

  2. 探索未知种族之osg类生物---呼吸分解之事件循环三

    那我们就开始处理这些事件中得到的所有的交互事件,首先我们要判断这些事件是否包含osg的退出事件,那什么情况下会触发这个退出事件呢?如果您运行过osg中example中的小例子的,聪明的你一定就会发现当 ...

  3. Luogu 3119 [USACO15JAN]草鉴定Grass Cownoisseur

    思路很乱,写个博客理一理. 缩点 + dp. 首先发现把一个环上的边反向是意义不大的,这样子不但不好算,而且相当于浪费了一次反向的机会.反正一个强连通分量里的点绕一遍都可以走到,所以我们缩点之后把一个 ...

  4. PHP学习笔记(一)

    1.什么是 PHP? PHP 指 PHP:超文本预处理器(译者注:PHP: Hypertext Preprocessor,递归命名) PHP 是一种服务器端的脚本语言,类似 ASP PHP 脚本在服务 ...

  5. Eclipse创建Spring项目 未完

    使用的软件及版本 1)Eclipse:Eclipse Java EE IDE for Web Developers :Version: 2018-09 (4.9.0) 2)JDK:java versi ...

  6. 在vue中的点击事件怎么获取当前点击的元素

    首先 vue的点击事件 是用 @click = “clickfun()” 属性 在html中绑定的,在点击的函数中 添加$event 参数就可以比如<button @click = “click ...

  7. 学习第一天-JAVA

    第一题打印1到1000的奇数 public class OneToThousandOdd{ public static void main(String[] args) { short num = 1 ...

  8. spring学习 三 框架的搭建

    1 导入jar包 spring启来最少要5个包,四个核心包和一个依赖的日志包 2 创建配置文件 在dynamic web project下的src目录下,创建一个spring的xml配置文件,名称可以 ...

  9. rails gem更换ruby-china源

    查看gem源 gem sources -l 换添加源 gem sources --add https://gems.ruby-china.com/ 删除原来的rubygems源 gem sources ...

  10. 2018.10.24 bzoj2064: 分裂(状压dp)

    传送门 状压dp好题. 考虑对于两个给出的集合. 如果没有两个元素和相等的子集,那么只能全部拼起来之后再拆开,一共需要n1+n2−2n1+n2-2n1+n2−2. 如果有呢? 那么对于没有的就是子问题 ...