Hbase Python 操作准备

来源:互联网 发布:贵阳大数据交易所地址 编辑:程序博客网 时间:2024/05/29 14:27
网上的教程是Java操作hbase的实例,我这里使用python,所以之后的文章都是通过thrift API操作hbase   

1、安装apache thrift(根据自己系统选择安装)
http://thrift.apache.org/docs/install/centos
按照上面步骤一步一步来,每一步都不能少,即使安装的有,也可以使用这些命令来检查安装的是否有问题,注意每一步涉及到安装的都需要使用sudo来运行,否则提示无权限。
但是在最后一步的时候,运行sudo make时,还是遇到了如下错误:
这里写图片描述
在网上搜索之后找到答案,进入lib/cpp/src/thrift/文件夹下,找到Thrift.h文件,在如图位置添加:#define __STDC_FORMAT_MACROS,之后再次进行make即可
这里写图片描述
验证thrift是否正确安装,输入命令:thrift –help查看帮助信息即可.
2、Thrift API
在网上很多人说在hbase根目录下存在Hbase.thrift文件,但是我没有找到,没办法,只能去官网下载https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/1.2.6/
hbase-1.2.6-src.tar.gz 这个文件,然后找到里面的hbase-thrift文件夹,解压出来,拷贝到任意目录,然后使用如下命令,生成python使用的Thrift API:
thrift –gen py hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrif
然后把生成的gen-py文件夹下的hbase文件夹,拷贝到python的包路径下(site-packages)就可以使用了。
后期修改:发现这个包生成的hbase包已经不能用了,需要使用pip安装hbase-thrift即可。
3、测试
1 开启thrift服务:
hbase thrift start
2 编写python程序,操作数据库

# coding=utf-8from thrift.transport.TSocket import TSocketfrom thrift.transport.TTransport import TBufferedTransportfrom thrift.protocol import TBinaryProtocolfrom hbase import Hbasehost = 'localhost'port = 9090transport = TBufferedTransport(TSocket(host, port))transport.open()protocol = TBinaryProtocol.TBinaryProtocol(transport)client = Hbase.Client(protocol)print client.getTableNames()transport.close()

3 运行结果:
使用hbase shell运行结果:
这里写图片描述
运行程序得到输出:
这里写图片描述

原创粉丝点击