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。html
Apache Phoenix 官方站點:https://phoenix.apache.org/
Phoenix支持的sql語句: https://phoenix.apache.org/language/index.html
Phoenix 支持的DataTypes:https://phoenix.apache.org/language/datatypes.html
Phoenix 支持的函數:https://phoenix.apache.org/language/functions.htmljava
Phoenix 安裝很簡單,下載對應hbase版本的Phoenix,下載地址,以phoenix-4.4.0-HBase-0.98-bin.tar.gz
爲例,解壓文件,將phoenix-4.4.0-server.jar
拷貝到hbase安裝目錄的lib下,注意:每臺regionserver均須要拷貝,重啓hbase server便可,官方以下:mysql
- 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
詳情查看:Phoenix-in-15-minutes 。spring
經過案例,create 表,插入語句,更新語句,刪除語句案例,詳細可參考:https://phoenix.apache.org/faq.htmlsql
Phoenix 鏈接hbase的命令以下,sqlline.py [zookeeper]
:shell
bash[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
,其它的都是本身創建的;apache
下面經過腳原本模擬下使用Phoenix創建數據表、修改表、添加數據、修改數據、刪除數據、刪除表等操做:api
一、新建一張Person
表,含有IDCardNum
,Name
,Age
三個字段 ,test
爲table_schem
,標準sql以下:bash
sqlcreate table IF NOT EXISTS test.Person (IDCardNum INTEGER not null primary key, Name varchar(20),Age INTEGER);
在 Phoenix 中使用以下:mvc
bash0: 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
了,包含了三列。
二、對錶進行插入操做,sql以下:
sqlinsert 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
,具體以下:
bash0: 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
性別操做,怎麼辦?
三、alter 修改表數據,sql以下:
sqlALTER TABLE test.Persion ADD sex varchar(10);
Phoenix 中操做以下:
bash0: 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)
上圖看到已經新增了列sex
,每行的默認值爲 null
,那麼怎麼樣修改這些值呢?
四、 更新表數據 ,標準的sql 以下:
sqlupdate 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 客戶端中操做以下:
bash0: 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)
五、複雜查詢,經過Phoenix能夠支持 where、group by、case when 等複雜的查詢條件,案例以下:
bash# 現增長几條數據 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)
where + group by 語句例子:
bashjdbc:phoenix:10.35.66.72> select sex ,count(sex) as num from test.person where age >20 group by sex; +------------+------------------------------------------+ | SEX | NUM | +------------+------------------------------------------+ | 男 | 4 | +------------+------------------------------------------+
case when 的例子:
bash0: jdbc:phoenix:10.35.66.72> select (case name when '小明' then '明明啊' when '小紅' then '紅紅啊' else name end) as showname from test.person; +------------------------------------------+ | SHOWNAME | +------------------------------------------+ | 明明啊 | | 紅紅啊 | | 小王 | | 小張 | | 小李 | | 小李 | +------------------------------------------+
更多支持語法參考:https://phoenix.apache.org/language/index.html
六、刪除數據及刪除表,標準sql以下:
sqldelete from test.Person where idcardnum=100; drop table test.person;
Phoenix中同標準sql同樣,案例以下:
bash0: jdbc:phoenix:10.35.66.72> delete from test.Person where idcardnum=100; 1 row affected (0.072 seconds) 0: jdbc:phoenix:10.35.66.72> select * from test.Person where idcardnum=100; +------------------------------------------+----------------------+------------------------------------------+------------+ | IDCARDNUM | NAME | AGE | SEX | +------------------------------------------+----------------------+------------------------------------------+------------+ +------------------------------------------+----------------------+------------------------------------------+------------+ 0: jdbc:phoenix:10.35.66.72> drop table test.person; No rows affected (1.799 seconds) 0: jdbc:phoenix:10.35.66.72> select * from test.person; Error: ERROR 1012 (42M03): Table undefined. tableName=TEST.PERSON (state=42M03,code=1012) 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 | +------------------------------------------+------------------------------------------+------------------------------------------+-------------------+
若是你不喜歡 終端下的腳本命令,青睞於GUI化的客戶端,那麼 SQuirrel是個好的選擇,就跟平日裏使用 MsSqlServer client、Navicat client 同樣,效果以下圖:
使用方法:(能夠參照官網英文說明)
1. 下載SQuirrel 客戶端 ,地址 http://squirrel-sql.sourceforge.net/
2. 解壓縮,刪除 lib/
下老版本的 phoenix-[oldversion]-client.jar
文件,將你剛剛下載的Phoenix文件夾下最新的文件 拷貝過去;
3. 啓動SQuirrel客戶端,選擇 Drivers-new driver ,名稱隨便,url格式: jdbc:phoenix:(zk地址)
,class name textbox 填寫org.apache.phoenix.jdbc.PhoenixDriver
4. ok, 點擊 connect便可完成鏈接
squirrel 客戶端的用法 和 Phoenix 自帶終端同樣,都是常見的sql語句,你們能夠本身搭建練習。
java api 徹底能夠採用傳統的 jdbc 鏈接的形式,案例如官方提供:
建立test.java
類,內容以下:
javaimport java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.PreparedStatement; import java.sql.Statement; public class test { public static void main(String[] args) throws SQLException { Statement stmt = null; ResultSet rset = null; Connection con = DriverManager.getConnection("jdbc:phoenix:[zookeeper]"); stmt = con.createStatement(); stmt.executeUpdate("create table test (mykey integer not null primary key, mycolumn varchar)"); stmt.executeUpdate("upsert into test values (1,'Hello')"); stmt.executeUpdate("upsert into test values (2,'World!')"); con.commit(); PreparedStatement statement = con.prepareStatement("select * from test"); rset = statement.executeQuery(); while (rset.next()) { System.out.println(rset.getString("mycolumn")); } statement.close(); con.close(); } }
在終端使用javac
編譯,經過Phoenix客戶端執行,就能看到結果:
bash$ javac test.java $ java -cp "../phoenix-[version]-client.jar:." test # You should get the following output Hello World!
固然,在生產使用中,每每採用的是 spring mvc + mybaits
的框架來進行訪問的,Phoenix 徹底支持這種形式,就像日常寫mysql、SqlServer同樣,對應的jdbc.properties
中的驅動修改成 org.apache.phoenix.jdbc.PhoenixDriver
便可,其它的寫法通普通的同樣。
原文首發於:http://ixirong.com/2015/06/24/how-hbase-use-apache-phoenix/