使用 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。

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.html




二、shell 命令

通过案例,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.2sqlline version 4.2 ,输入Phoenix支持的命令 !tables 可以查看当前集群中存在的数据表,能够看到有些是 SYSTEM TABLE ,其它的都是自己建立的;

下面通过脚本来模拟下使用Phoenix建立数据表、修改表、添加数据、修改数据、删除数据、删除表等操作:

1、新建一张 Person 表,含有 IDCardNumName Age 三个字段 , testtable_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)

上图看到已经新增了列 sex ,每行的默认值为 null ,那么怎么样修改这些值呢?

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)

where + group by 语句例子:

jdbc: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 的例子:

0: 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

6、删除数据及删除表,标准sql如下:

delete from test.Person where idcardnum=100;
drop table test.person;

Phoenix中同标准sql一样,案例如下:

0: 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             |
+------------------------------------------+------------------------------------------+------------------------------------------+-------------------+
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值