1. 程式人生 > >使用Phoenix通過sql語句更新操作hbase資料

使用Phoenix通過sql語句更新操作hbase資料

hbase 提供很方便的shell指令碼,可以對資料表進行 CURD 操作,但是畢竟是有一定的學習成本的,基本上對於開發來講,sql 語句都是看家本領,那麼,有沒有一種方法可以把 sql 語句轉換成 hbase的原生API呢? 這樣就可以通過普通平常的 sql 來對hbase 進行資料的管理,使用成本大大降低。Apache Phoenix 元件就完成了這種需求,官方註解為 “Phoenix - we put the SQL back in NoSql”,通過官方說明,Phoenix 的效能很高,相對於 hbase 原生的scan 並不會差多少,而對於類似的元件 hive、Impala等,效能有著顯著的提升,詳細請閱讀 https://phoenix.apache.org/performance.html。

一、安裝使用
Phoenix 安裝很簡單,下載對應hbase版本的Phoenix,下載地址,以phoenix-4.4.0-HBase-0.98-bin.tar.gz為例,解壓檔案,將phoenix-4.4.0-server.jar 拷貝到hbase安裝目錄的lib下,注意:每臺regionserver均需要拷貝,重啟hbase server即可,官方如下:

  • download and expand the latest phoenix-[version]-bin.tar.
  • Add the phoenix-[version]-server.jar to the classpath of all HBase region server and master and remove any previous version. An easy way to do this is to copy it into the HBase lib directory (use phoenix-core-[version].jar for Phoenix 3.x)
  • restart the region servers
  • Add the phoenix-[version]-client.jar to the classpath of any Phoenix client.
  • download and setup SQuirrel as your SQL client so you can issue
    -adhoc SQL against your HBase cluster

通過案例,create 表,插入語句,更新語句,刪除語句案例,詳細可參考:https://phoenix.apache.org/faq.html
Phoenix 連線hbase的命令如下,sqlline.py [zookeeper] :

[hadoop@slave2 lib]$ ./sqlline.py 10.35.66.72
Setting property: [isolation, TRANSACTION_READ_COMMITTED]
issuing: !connect jdbc:phoenix:10.35.66.72 none none org.apache.phoenix.jdbc.PhoenixDriver
Connecting to jdbc:phoenix:10.35.66.72
15/06/24 13:06:29 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Connected to: Phoenix (version 4.2)
Driver: PhoenixEmbeddedDriver (version 4.2)
Autocommit status: true
Transaction isolation: TRANSACTION_READ_COMMITTED
Building list of tables and columns for tab-completion (set fastconnect to true to skip)...
193/193 (100%) Done
Done
sqlline version 1.1.2
0: jdbc:phoenix:10.35.66.72>!tables
+------------------------------------------+------------------------------------------+------------------------------------------+-------------------+
|                TABLE_CAT                 |               TABLE_SCHEM                |                TABLE_NAME                |                TA |
+------------------------------------------+------------------------------------------+------------------------------------------+-------------------+
| null                                     | WL                                       | BIG_LOG_DEVUTRACEID_INDEX                | INDEX             |
| null                                     | WL                                       | MSGCENTER_PUSHMESSAGE_V2_OWNERPAGE_INDEX | INDEX             |
| null                                     | SYSTEM                                   | CATALOG                                  | SYSTEM TABLE      |
| null                                     | SYSTEM                                   | SEQUENCE                                 | SYSTEM TABLE      |
| null                                     | SYSTEM                                   | STATS                                    | SYSTEM TABLE      |
| null                                     | DMO                                      | SOWNTOWN_STATICS                         | TABLE             |
| null                                     | OL                                       | BIGLOG                                   | TABLE             |
| null                                     | WL                                       | BIG_LOG                                  | TABLE             |
| null                                     | WL                                       | ERROR_LOG                                | TABLE             |
| null                                     | WL                                       | MSGCENTER_PUSHMESSAGE                    | TABLE             |
| null                                     | WL                                       | MSGCENTER_PUSHMESSAGE_V2                 | TABLE             |
+------------------------------------------+------------------------------------------+------------------------------------------+------------------

從上面能夠看到,已經連線到了hbase叢集上面,Phoenix version 4.2 ,sqlline version 4.2 ,輸入Phoenix支援的命令!tables可以檢視當前叢集中存在的資料表,能夠看到有些是 SYSTEM TABLE,其它的都是自己建立的;

下面通過指令碼來模擬下使用Phoenix建立資料表、修改表、新增資料、修改資料、刪除資料、刪除表等操作:

1、新建一張Person表,含有IDCardNum,Name,Age 三個欄位 ,test 為table_schem ,標準sql如下:

create table IF NOT EXISTS test.Person (IDCardNum INTEGER not null primary key, Name varchar(20),Age INTEGER);

在 Phoenix 中使用如下:

0: jdbc:phoenix:10.35.66.72> create table IF NOT EXISTS test.Person (IDCardNum INTEGER not null primary key, Name varchar(20),Age INTEGER);
No rows affected (0.344 seconds)
0: jdbc:phoenix:10.35.66.72> !tables
+------------------------------------------+------------------------------------------+------------------------------------------+-------------------+
|                TABLE_CAT                 |               TABLE_SCHEM                |                TABLE_NAME                |                TA |
+------------------------------------------+------------------------------------------+------------------------------------------+-------------------+
| null                                     | WL                                       | BIG_LOG_DEVUTRACEID_INDEX                | INDEX             |
| null                                     | WL                                       | MSGCENTER_PUSHMESSAGE_V2_OWNERPAGE_INDEX | INDEX             |
| null                                     | SYSTEM                                   | CATALOG                                  | SYSTEM TABLE      |
| null                                     | SYSTEM                                   | SEQUENCE                                 | SYSTEM TABLE      |
| null                                     | SYSTEM                                   | STATS                                    | SYSTEM TABLE      |
| null                                     | DMO                                      | SOWNTOWN_STATICS                         | TABLE             |
| null                                     | OL                                       | BIGLOG                                   | TABLE             |
| null                                     | TEST                                     | PERSON                                  | TABLE             |
| null                                     | WL                                       | BIG_LOG                                  | TABLE             |
| null                                     | WL                                       | ERROR_LOG                                | TABLE             |
| null                                     | WL                                       | MSGCENTER_PUSHMESSAGE                    | TABLE             |
| null                                     | WL                                       | MSGCENTER_PUSHMESSAGE_V2                 | TABLE             |
+------------------------------------------+------------------------------------------+------------------------------------------+-------------------+
0: jdbc:phoenix:10.35.66.72> select * from TEST.PERSON;
+------------------------------------------+----------------------+------------------------------------------+
|                IDCARDNUM                 |         NAME         |                   AGE                    |
+------------------------------------------+----------------------+------------------------------------------+
+------------------------------------------+----------------------+------------------------------------------+

可以看到,hbase中已經存在資料表 Person了,包含了三列

2、對錶進行插入操作,sql如下:

insert into Person (IDCardNum,Name,Age) values (100,'小明',12);
insert into Person (IDCardNum,Name,Age) values (101,'小紅',15);
insert into Person (IDCardNum,Name,Age) values (103,'小王',22);

在 Phoenix 中插入的語句為 upsert ,具體如下:

0: jdbc:phoenix:10.35.66.72> upsert into test.Person (IDCardNum,Name,Age) values (100,'小明',12);
1 row affected (0.043 seconds)
0: jdbc:phoenix:10.35.66.72> upsert into test.Person (IDCardNum,Name,Age) values (101,'小紅',15);
1 row affected (0.018 seconds)
0: jdbc:phoenix:10.35.66.72> upsert into test.Person (IDCardNum,Name,Age) values (103,'小王',22);
1 row affected (0.009 seconds)
0: jdbc:phoenix:10.35.66.72> select * from test.Person;
+------------------------------------------+----------------------+------------------------------------------+
|                IDCARDNUM                 |         NAME         |                   AGE                    |
+------------------------------------------+----------------------+------------------------------------------+
| 100                                      | 小明                   | 12                                       |
| 101                                      | 小紅                   | 15                                       |
| 103                                      | 小王                   | 22                                       |
+------------------------------------------+----------------------+------------------------------------------+
3 rows selected (0.115 seconds)

從上面可以看到,三條資料已經進入hbase裡面了;好了,現在要對錶新增一列 sex 性別操作,怎麼辦?

3、alter 修改表資料,sql如下:

ALTER TABLE test.Persion ADD sex varchar(10);

Phoenix 中操作如下:

0: jdbc:phoenix:10.35.66.72> ALTER TABLE test.Person ADD sex varchar(10);
No rows affected (0.191 seconds)
: jdbc:phoenix:10.35.66.72> select * from test.person;
+------------------------------------------+----------------------+------------------------------------------+------------+
|                IDCARDNUM                 |         NAME         |                   AGE                    |    SEX     |
+------------------------------------------+----------------------+------------------------------------------+------------+
| 100                                      | 小明                   | 12                                       | null       |
| 101                                      | 小紅                   | 15                                       | null       |
| 103                                      | 小王                   | 22                                       | null       |
+------------------------------------------+----------------------+------------------------------------------+------------+
3 rows selected (0.113 seconds)

4、 更新表資料 ,標準的sql 如下:

update test.Person set sex='男' where IDCardNum=100;
update test.Person set sex='女' where IDCardNum=101;
update test.Person set sex='男' where IDCardNum=103;

Phoenix中不存在update的語法關鍵字,而是upsert ,功能上替代了Insert+update,官方說明為:

UPSERT VALUES
Inserts if not present and updates otherwise the value in the table. The list of columns is optional >and if not present, the values will map to the column in the order they are declared in the >schema. The values must evaluate to constants.

根據介紹,只需要在upsert語句中制定存在的idcardnum即可實現更新,在 Phoenix 客戶端中操作如下

0: jdbc:phoenix:10.35.66.72> upsert into test.person (idcardnum,sex) values (100,'男');
1 row affected (0.083 seconds)
0: jdbc:phoenix:10.35.66.72> upsert into test.person (idcardnum,sex) values (101,'女');
1 row affected (0.012 seconds)
0: jdbc:phoenix:10.35.66.72> upsert into test.person (idcardnum,sex) values (103,'男');
1 row affected (0.008 seconds)
0: jdbc:phoenix:10.35.66.72> select * from test.person;
+------------------------------------------+----------------------+------------------------------------------+------------+
|                IDCARDNUM                 |         NAME         |                   AGE                    |    SEX     |
+------------------------------------------+----------------------+------------------------------------------+------------+
| 100                                      | 小明                   | 12                                       | 男          |
| 101                                      | 小紅                   | 15                                       | 女          |
| 103                                      | 小王                   | 22                                       | 男          |
+------------------------------------------+----------------------+------------------------------------------+------------+
3 rows selected (0.087 seconds)

5、複雜查詢,通過Phoenix可以支援 where、group by、case when 等複雜的查詢條件,案例如下:

**現增加幾條資料**
0: jdbc:phoenix:10.35.66.72> upsert into test.Person (IDCardNum,Name,Age,sex) values (104,'小張',23,'男');
1 row affected (0.012 seconds)
0: jdbc:phoenix:10.35.66.72> upsert into test.Person (IDCardNum,Name,Age,sex) values (105,'小李',28,'男');
1 row affected (0.015 seconds)
0: jdbc:phoenix:10.35.66.72> upsert into test.Person (IDCardNum,Name,Age,sex) values (106,'小李',33,'男');
1 row affected (0.011 seconds)
0: jdbc:phoenix:10.35.66.72> select * from test.person;
+------------------------------------------+----------------------+------------------------------------------+------------+
|                IDCARDNUM                 |         NAME         |                   AGE                    |    SEX     |
+------------------------------------------+----------------------+------------------------------------------+------------+
| 100                                      | 小明                   | 12                                       | 男          |
| 101                                      | 小紅                   | 15                                       | 女          |
| 103                                      | 小王                   | 22                                       | 男          |
| 104                                      | 小張                   | 23                                       | 男          |
| 105                                      | 小李                   | 28                                       | 男          |
| 106                                      | 小李                   | 33                                       | 男          |
+------------------------------------------+----------------------+------------------------------------------+------------+
6 rows selected (0.09 seconds)