您的位置:首页 > 编程语言 > Python开发

pyspark Python 连接 HBase thrift

2015-11-16 14:40 841 查看

0.引言

HBase-thrift项目是对HBase Thrift接口的封装,屏蔽底层的细节,使用户可以方便地通过HBase Thrift接口访问HBase集群,python通过thrift访问HBase。

1.thrift安装

python客户端机器安装:

thrift官网

下载thrift-0.9.3.tar.gz

下载后解压到当前文件夹

tar xvf thrift-0.9.3.tar.gz

进入thrift-0.9.3文件夹

cd thrift-0.9.3

构建

./configure –prefix=/usr/qy/thrift

错误:configure:error:Bison 2.5 or higher must be installed on the system.

查看bison版本:

which bison

/usr/bin/bison -V (我的是2.4)

下载bison 2.5

tar xvf bison-2.5.tar.gz
cd bison-2.5
./configure --prefix=/usr(覆盖原有的bison)
或者./configure --prefix=/usr/qy/bison-2.5
make
make install(卸载的话直接在当前目录(bison-2.5)里make uninstall)
然后在vim ~/.bashrc 中添加路径
即:原来是:
export PATH="/usr/qy/anaconda/bin:$PATH"
现在是:
export PATH="/usr/qy/ananconda/bin:/usr/qy/bison/bin:$PATH"
然后source ~/.bashrc 使配置的环境变量生效


现在再查看

bison -V (2.5了)

继续进入thrift文件夹 并configure

cd thrift-0.9.3

./configure

在configure的时候可以选择配置接口

./configure –with-python=yes –with-lua=no –with-java=no(选择配置python接口,其他的不需要配置)



make  【(如果不是root用户的话)sudo make】


make install 【sudo make install】
# that’s OK
which thrift #usr/local/bin/thrift
thrift -version # Thrift version 0.9.3


2.生成hbase python client

1.HBase部分

HBase Master节点安装:

下载HBase源码包

wget http://mirrors.cnnic.cn/apache/hbase/1.1.2/hbase-1.1.2-src.tar.gz
hbase-1.1.2-src.tar.gz

tar zxf hbase-1.1.2-src.tar.gz
cd ./hbase-1.1.2/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift


ps. HBase有两个thrift版本的接口,称为thrift和thrift2,thrift1很可能被抛弃。。

thrift --gen py ./Hbase.thrift
ls -l
# 会看到生成一个gen-py/hbase的目录
# ./Hbase.py 中定义了一些HbaseClient可以使用的方法
# ./ttypes.py中定义了HbaseClient传输的数据类型
# 调用client脚本的服务器上需要安装thrift
total 28
drwxr-xr-x 3 root root    4096 Dec 13 18:26 gen-py
-rw-r--r-- 1 1000 hadoop 23671 Nov 15 05:58 Hbase.thrift

# 将产生的gen-py目录拷贝到python目录
cp -rp gen-py/hbase/ /usr/lib64/python2.6/site-packages/




2.python 部分

python客户端安装:

pip install thrift # successfully installed thrift 0.9.3(事先装了anaconda)

3.测试使用python通过thrift接口访问hbase中的表

1. 启动hbase的thrift服务器

要使用HBase的thrift接口,必须将它的服务启动

hbase thrift -p 9090 start #thrift默认的监听端口是9090,可以用 netstat -nl | grep 9090 查看该端口是否有服务。
或者
./hbase-daemon.sh start thrift(好像用这个比较好)
ps:结束thrift进程:
./hbase-daemon.sh stop thrift
jps
------------------
9524 JobTracker
11430 Jps
9369 NameNode
11227 ThriftServer
11115 HRegionServer
10797 QuorumPeerMain
10973 HMaster




2.测试一:列出hbase中的表

编辑python测试程序:

[root@test1 thrift_test]# python
Python 2.6.6 (r266:84292, May  1 2012, 13:52:17)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> import sys
>>> sys.path.append('/usr/lib64/python2.6/site-packages/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('192.168.1.131', 9090)
>>> transport = TTransport.TBufferedTransport(transport)
>>> protocol = TBinaryProtocol.TBinaryProtocol(transport)
>>> client = Hbase.Client(protocol)
>>> transport.open()
>>> print(client.getTableNames())
['member']


reference

*【 有用!】蛋疼的thrift安装及简单的操作hbase表

* python调用HBase范例

* Python 连接Hbase (centos6.5)

* Thrift1与Thrift2的比较
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: