本地操作

  • 启动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. mybatis入门--mybatis和hibernate比较

    mybatis和hibernate的比较 Mybatis和hibernate不同,它不完全是一个ORM框架,因为MyBatis需要程序员自己编写Sql语句,不过mybatis可以通过XML或注解方式灵 ...

  2. JS-事件心得

    写在前面的话:就我目前的水平来看,这两种方法不能一起使用,用on添加的事件removeEventListener()没办法删除,反之一样 注册事件的两种方式: on+事件名称 addEventList ...

  3. Luogu 3690 LCT - 模板

    推荐几篇比较好的博客: FlashHu 的 讲解比较好 : 传送门 Candy 的 代码~ : 传送门 以及神犇Angel_Kitty的 学习笔记: 传送门 Code V 模板 #include< ...

  4. C++动态链接库

    1.动态链接库概述: 动态链接库通常都不能直接运行,也不能接受消息:只有在其他模块调用动态链接库中的函数时,它才发挥作用. Windows API中所有的函数都包含在动态链接库中. 动态链接库分静态库 ...

  5. Tinyos学习笔记(一)

    简述:发送和接受数据的程序分别烧录到两个节点上,发送方发送流水灯数据,接受方接受数据并实现流水灯 1.发送和接受程序用到的组件及其接口如图(通过make telosb docs获得)所示:   2.发 ...

  6. sex在软件开发中的运用--SIX技术

    开篇:省略xxx字 keyword:sex . female, male .SIX ,sex integer extention technolgolsl 前言: 对于sex字段的研究,国内,国际尚为 ...

  7. [Chrome Headless + Python] 截长图 (Take Full-page Screenshot)

    # -*- coding: utf-8 -*- import time import os from selenium import webdriver from selenium.webdriver ...

  8. openssl编译安装-各种蛋疼

    arm平台  ubuntu Linux xxxxxx 3.0.62 #2 PREEMPT Tue Apr 2 20:14:12 CST 2013 armv7l armv7l armv7l GNU/Li ...

  9. mysql 5.7 linux环境下解压安装

    在CentOS linux环境安装mysql 一般rpm(或者yum),预编译和源码安装. 如果采用rpm或者yum安装,mysql的数据文件一般存放在/var/lib/mysql目录下,也就是会把d ...

  10. LD_LIBRARY_PATH

    LD_LIBRARY_PATH是Linux环境变量名,该环境变量主要用于指定查找共享库(动态链接库)时除了默认路径之外的其他路径. 在linux下可以用export命令来设置这个值,比如 在linux ...