利用python访问Hbase(Thrift模块安装与测试)
hadoop环境介绍:
master服务:node1
slave服务器:node2,node3,node4
mysql服务器:node29
Thrift安装在node1服务器上!
相关软件版本:
hadoop版本:hadoop-0.20.2
sqoop版本:sqoop-1.2.0-CDH3B4
java版本:jdk1.7.0_67
mysql版本:5.1.65
Thrift版本:thrift-0.9.0
thrift安装链接:http://thrift.apache.org/download/
python版本:2.7.3
ps:python2.5版本使用thrift有问题
一:测试前的准备工作
1)首先把mysql数据库中数据加载到hbase中:
mysql数据如下:
将mysql的数据导入hbase的命令格式为:
sqoop import --connect jdbc:mysql://mysqlserver_IP/databaseName --username --password password --table datatable --hbase-create-table --hbase-table hbase_tablename --column-family col_fam_name --hbase-row-key key_col_name
说明:databaseName 和datatable 是mysql的数据库和表名,hbase_tablename是要导成hbase的表名,key_col_name可以指定datatable中哪一列作 为hbase新表的rowkey,col_fam_name是除rowkey之外的所有列的列族名
2)在node1上加载mysql数据(node29)到hbase中:
sqoop import --connect jdbc:mysql://172.16.41.29/sqoop --username sqoop --password routon --table students --hbase-create-table --hbase-table students --column-family stuinfo --hbase-row-key id
在hbase中验证是否加载成功:
二 Thrift软件安装
python版本:2.7.3
步骤为:
1) 安装python2.7.3
说明:python2.7.3与thrift结合没问题,python2.5版本好像不行!
生成的Hbase.py文件中的语法rhel5 自带的python2.4 不支持
tar fvxj Python-2.7.3.tar.bz2
./configure --prefix=/usr/local/python2.7
make && make install
python2.7.3路径为:
/usr/local/python2.7/bin/python
修改默认的python版本为2.7
把python2.7设置为环境变量,系统默认python版本为2.4
rm -rf /usr/bin/python
ln -s /usr/local/python2.7/bin/python /usr/bin/python
[root@node1 thrift-0.9.0]# python -V
Python 2.7.3
2)安装thrift
tar fvxz thrift-0.9.0.tar.gz
cd thrift-0.9.0
./configure
make && make install
thrift 0.9.0
Building C++ Library ......... : no
Building C (GLib) Library .... : yes
Building Java Library ........ : no
Building C# Library .......... : no
Building Python Library ...... : yes
Building Ruby Library ........ : no
Building Haskell Library ..... : no
Building Perl Library ........ : no
Building PHP Library ......... : no
Building Erlang Library ...... : no
Building Go Library .......... : no
Building D Library ........... : no
Python Library:
Using Python .............. : /usr/bin/python
可以看到thrift支持很多语言,根据目前需求,支持python就可以了!
查看Thrift版本:
[root@node1 thrift-0.9.0]# thrift -version
Thrift version 0.9.0
3)让thrift支持hbase
执行以下命令:
thrift --gen py /usr/local/hbase-0.90.5/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift
会在当前目前下创建一个目录,目录名称为:
[hadoop@node1 ~]$ ll
total 7056
-rw-rw-r-- 1 hadoop hadoop 3045 Oct 14 13:55 access_log2.txt
-rw-r--r-- 1 hadoop hadoop 7118627 Feb 1 2012 access_log.txt
-rw-rw-r-- 1 hadoop hadoop 3500 Oct 22 10:17 derby.log
drwxrwxr-x 3 hadoop hadoop 4096 Oct 24 15:28 gen-py
-rw-rw-r-- 1 hadoop hadoop 3551 Oct 13 11:21 pig_1413170429087.log
gen-py目录结构如下:
[hadoop@node1 ~]$ tree gen-py/
gen-py/
|-- __init__.py
`-- hbase
|-- Hbase-remote
|-- Hbase.py
|-- __init__.py
|-- constants.py
`-- ttypes.py
1 directory, 6 files
4)把gen-py目录复制到python相关目录中:
cp -r gen-py/hbase/ /usr/local/python2.7/lib/python2.7/site-packages/.
5)让python可以import thrift模块:
[root@node1 ~]# ln -s /usr/lib/python2.7/site-packages/thrift* /usr/local/python2.7/lib/python2.7/site-packages/.
[root@node1 ~]# ls -l /usr/local/python2.7/lib/python2.7/site-packages/
total 12
drwxr-xr-x 2 root root 4096 Oct 24 15:32 hbase
-rw-r--r-- 1 root root 119 Oct 24 11:30 README
lrwxrwxrwx 1 root root 39 Oct 24 15:50 thrift -> /usr/lib/python2.7/site-packages/thrift
lrwxrwxrwx 1 root root 60 Oct 24 15:50 thrift-0.9.0-py2.7.egg-info -> /usr/lib/python2.7/site-packages/thrift-0.9.0-py2.7.egg-info
6)启动thrift服务:
hbase thrift -p 9090 start
7)在node1上编写python脚本,查看hbase中有哪些表:
#! /usr/bin/env python#coding=utf-8import sys#Hbase.thrift生成的py文件放在这里sys.path.append('/usr/local/lib/python2.7/site-packages/hbase')from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom hbase import Hbase#如ColumnDescriptor 等在hbase.ttypes中定义from hbase.ttypes import *# Make socket#此处可以修改地址和端口transport = TSocket.TSocket('172.16.41.26', 9090)# Buffering is critical. Raw sockets are very slow# 还可以用TFramedTransport,也是高效传输方式transport = TTransport.TBufferedTransport(transport)# Wrap in a protocol#传输协议和传输过程是分离的,可以支持多协议protocol = TBinaryProtocol.TBinaryProtocol(transport)#客户端代表一个用户client = Hbase.Client(protocol)#打开连接transport.open()#打印表名print(client.getTableNames())
执行脚本:
到此,通过thrift插件,python就可以和hbase进行通信!