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
python版本:2.7.3
ps:python2.5版本使用thrift有問題
一:測試前的準備工作
1)首先把mysql資料庫中資料加載到hbase中:
mysql資料如下:
<a href="http://s3.51cto.com/wyfs02/M02/4D/0F/wKiom1RKCu7Acn6aAAEPRQIQRa8209.jpg" target="_blank"></a>
将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中驗證是否加載成功:
<a href="http://s3.51cto.com/wyfs02/M01/4D/10/wKioL1RKDCHRujBiAAJMEeBRwoU194.jpg" target="_blank"></a>
二 Thrift軟體安裝
步驟為:
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
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中有哪些表:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<code>#! /usr/bin/env python</code>
<code>#coding=utf-8</code>
<code>import</code> <code>sys</code>
<code>#Hbase.thrift生成的py檔案放在這裡</code>
<code>sys.path.append(</code><code>'/usr/local/lib/python2.7/site-packages/hbase'</code><code>)</code>
<code>from</code> <code>thrift </code><code>import</code> <code>Thrift</code>
<code>from</code> <code>thrift.transport </code><code>import</code> <code>TSocket</code>
<code>from</code> <code>thrift.transport </code><code>import</code> <code>TTransport</code>
<code>from</code> <code>thrift.protocol </code><code>import</code> <code>TBinaryProtocol</code>
<code>from</code> <code>hbase </code><code>import</code> <code>Hbase</code>
<code>#如ColumnDescriptor 等在hbase.ttypes中定義</code>
<code>from</code> <code>hbase.ttypes </code><code>import</code> <code>*</code>
<code># Make socket</code>
<code>#此處可以修改位址和端口</code>
<code>transport </code><code>=</code> <code>TSocket.TSocket(</code><code>'172.16.41.26'</code><code>, </code><code>9090</code><code>)</code>
<code># Buffering is critical. Raw sockets are very slow</code>
<code># 還可以用TFramedTransport,也是高效傳輸方式</code>
<code>transport </code><code>=</code> <code>TTransport.TBufferedTransport(transport)</code>
<code># Wrap in a protocol</code>
<code>#傳輸協定和傳輸過程是分離的,可以支援多協定</code>
<code>protocol </code><code>=</code> <code>TBinaryProtocol.TBinaryProtocol(transport)</code>
<code>#用戶端代表一個使用者</code>
<code>client </code><code>=</code> <code>Hbase.Client(protocol)</code>
<code>#打開連接配接</code>
<code>transport.</code><code>open</code><code>()</code>
<code>#列印表名</code>
<code>print</code><code>(client.getTableNames())</code>
執行腳本:
<a href="http://s3.51cto.com/wyfs02/M01/4D/10/wKiom1RKEALwhL86AAEY9rF6T_g766.jpg" target="_blank"></a>
到此,通過thrift插件,python就可以和hbase進行通信!
本文轉自 shine_forever 51CTO部落格,原文連結:http://blog.51cto.com/shineforever/1567640