Phoenix 在 HBase 生态系统中占据了非常重要的地位,本文主要包括以下几方面内容:
Phoenix 介绍
CDH HBase 集成 Phoenix
使用 Phoenix 创建 HBase 二级索引
Phoenix 索引类型介绍
Phoenix 是构建在 HBase 之上的高效的 SQL 引擎,同时具备 OLTP 和 OLAP 能力,作为 HBase 生态系统中非常重要的组件,重点的特性包括:
底层存储基于 HBase,并提供一套标准的 JDBC API 作为 HBase SQL 层;
支持标准 SQL,以及完整 ACID 事务特性;
为 HBase 提供了二级索引解决方案;
此外,Phoenix 还和很多其他组件做了集成,比如 Spark、Hive、Flume 等。Phoenix 与 HBase 集成,其最大的特点就是为 HBase 提供了二级索引,后文会重点介绍。下图是 Phoenix 的基本架构:
http://phoenix.apache.org/download.html;
高版本 CDH 安装 Phoenix 可以参考:产品 | Cloudera正式宣布在CDH中支持Apache Phoenix
http://archive.cloudera.com/cloudera-labs/phoenix/parcels/
首先到官网下载适合自己环境的 Parcel 安装包,并发布到 httpd 服务:
[root@hadoop-01 /var/www/html/phoenix/4.14.0]$ ll
total 300524
-rw-r--r-- 1 root root 307722240 Feb 3 19:30 APACHE_PHOENIX-4.14.0-cdh5.11.2.p0.3-el7.parcel
-rw-r--r-- 1 root root 178 Feb 3 19:28 APACHE_PHOENIX-4.14.0-cdh5.11.2.p0.3-el7.parcel.sha512
-rw-r--r-- 1 root root 5081 Feb 3 19:30 manifest.json
(可左右滑动)
然后配置成 CDH 远程 Parcel 存储库 url:接下来下载,分配,激活完成安装即可。安装完 Phoenix 后,需要做一些必要配置才能使用 Phoenix,CDH HBase 配置界面配置如下两处:
1. hbase-site.xml 的 HBase 服务高级配置代码段(安全阀)
2. hbase-site.xml 的 HBase 客户端高级配置代码段(安全阀)
添加如下参数配置:
hbase.regionserver.wal.codecorg.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec
phoenix.schema.isNamespaceMappingEnabledtrue
phoenix.schema.mapSystemTablesToNamespacetrue
(可左右滑动)
然后,按照提示重启HBase服务并重新部署客户端配置即可。
CDH 安装后环境变量都已经配置好了,可以直接使用 phoenix-sqlline.py,如下:
[root@hadoop-01 ~]$ phoenix-
phoenix-performance.py phoenix-psql.py phoenix-sqlline.py phoenix-utils.py
(可左右滑动)
执行 phoenix-sqlline.py 初始化使用 Phoenix:
然后我们查看下 HBase 中 Phoenix 的系统表:
hbase(main):003:0> list
SYSTEM:CATALOG
SYSTEM:FUNCTION
SYSTEM:LOG
SYSTEM:MUTEX
SYSTEM:SEQUENCE
SYSTEM:STATS
(可左右滑动)
接下来看一下如何在 Phoenix 中创建 HBase 表的二级索引。
当前 HBase 中存在一张操作日志表 ns1000:operate_log,数据量近280w,包括14个字段,如下:
hbase(main):017:0> count 'ns1000:operate_log', INTERVAL => 100000
...
2799827 row(s) in 173.4200 seconds
=> 2799827
hbase(main):018:0> scan 'ns1000:operate_log', LIMIT => 1
ROW COLUMN+CELL
x00x00x12x12x00x00x00x0D1538216707720 column=f:appVersion, timestamp=1538216707892, value=2.22.0
x00x00x12x12x00x00x00x0D1538216707720 column=f:area, timestamp=1538216707892, value=xE6xB1x9FxE5x8Cx97xE5x8CxBA
x00x00x12x12x00x00x00x0D1538216707720 column=f:authId, timestamp=1538216707892, value=
x00x00x12x12x00x00x00x0D1538216707720 column=f:city, timestamp=1538216707892, value=xE9x87x8DxE5xBAx86xE5xB8x82
x00x00x12x12x00x00x00x0D1538216707720 column=f:imei, timestamp=1538216707892, value=AF36147F-8106-47F0-B58F-A3FB75DBE325
x00x00x12x12x00x00x00x0D1538216707720 column=f:lat, timestamp=1538216707892, value=29.577587127685547
x00x00x12x12x00x00x00x0D1538216707720 column=f:lon, timestamp=1538216707892, value=106.50493621826172
x00x00x12x12x00x00x00x0D1538216707720 column=f:memberType, timestamp=1538216707892, value=0
x00x00x12x12x00x00x00x0D1538216707720 column=f:mobileManufacturer, timestamp=1538216707892, value=iPhone
x00x00x12x12x00x00x00x0D1538216707720 column=f:mobileModel, timestamp=1538216707892, value=iPhone 6 Plus
x00x00x12x12x00x00x00x0D1538216707720 column=f:province, timestamp=1538216707892, value=xE9x87x8DxE5xBAx86xE5xB8x82
x00x00x12x12x00x00x00x0D1538216707720 column=f:systemType, timestamp=1538216707892, value=1
x00x00x12x12x00x00x00x0D1538216707720 column=f:systemVersion, timestamp=1538216707892, value=12.0
x00x00x12x12x00x00x00x0D1538216707720 column=f:time, timestamp=1538216707892, value=1538216707720
1 row(s) in 0.0460 seconds
(可左右滑动)
2. Phoenix 中创建与 namespace 名称一致的 schema
0: jdbc:phoenix:> create schema if not exists "ns1000";
No rows affected (0.012 seconds)
(可左右滑动)
3. Phoenix 中创建视图,并查询数据及条数
0: jdbc:phoenix:> use "ns1000";
No rows affected (0.021 seconds)
0: jdbc:phoenix:> create view "operate_log"(
. . . . . . . . > "pk" varchar primary key,
. . . . . . . . > "f"."appVersion" varchar,
. . . . . . . . > "f"."city" varchar,
. . . . . . . . > "f"."lat" varchar,
. . . . . . . . > "f"."lon" varchar,
. . . . . . . . > "f"."memberType" varchar,
. . . . . . . . > "f"."time" varchar);
No rows affected (6.555 seconds)
0: jdbc:phoenix:> !tables
+------------+--------------+--------------+---------------+----------+------------+----------------------------+-----------------+--------------+-----------------+---------------+---------------+-----------------+------------+--------+
| TABLE_CAT | TABLE_SCHEM | TABLE_NAME | TABLE_TYPE | REMARKS | TYPE_NAME | SELF_REFERENCING_COL_NAME | REF_GENERATION | INDEX_STATE | IMMUTABLE_ROWS | SALT_BUCKETS | MULTI_TENANT | VIEW_STATEMENT | VIEW_TYPE | INDEX_ |
+------------+--------------+--------------+---------------+----------+------------+----------------------------+-----------------+--------------+-----------------+---------------+---------------+-----------------+------------+--------+
| | SYSTEM | CATALOG | SYSTEM TABLE | | | | | | false | | false | | | |
| | SYSTEM | FUNCTION | SYSTEM TABLE | | | | | | false | | false | | | |
| | SYSTEM | LOG | SYSTEM TABLE | | | | | | true | 32 | false | | | |
| | SYSTEM | SEQUENCE | SYSTEM TABLE | | | | | | false | | false | | | |
| | SYSTEM | STATS | SYSTEM TABLE | | | | | | false | | false | | | |
| | ns1000 | operate_log | VIEW | | | | | | false | | false | | MAPPED | |
+------------+--------------+--------------+---------------+----------+------------+----------------------------+-----------------+--------------+-----------------+---------------+---------------+-----------------+------------+--------+
0: jdbc:phoenix:> !columns "operate_log";
+------------+--------------+--------------+--------------+------------+------------+--------------+----------------+-----------------+-----------------+-----------+----------+-------------+----------------+-------------------+--------+
| TABLE_CAT | TABLE_SCHEM | TABLE_NAME | COLUMN_NAME | DATA_TYPE | TYPE_NAME | COLUMN_SIZE | BUFFER_LENGTH | DECIMAL_DIGITS | NUM_PREC_RADIX | ABLE | REMARKS | COLUMN_DEF | SQL_DATA_TYPE | SQL_DATETIME_SUB | CHAR_O |
+------------+--------------+--------------+--------------+------------+------------+--------------+----------------+-----------------+-----------------+-----------+----------+-------------+----------------+-------------------+--------+
| | ns1000 | operate_log | pk | 12 | VARCHAR | | | | | 0 | | | | | |
| | ns1000 | operate_log | appVersion | 12 | VARCHAR | | | | | 1 | | | | | |
| | ns1000 | operate_log | city | 12 | VARCHAR | | | | | 1 | | | | | |
| | ns1000 | operate_log | lat | 12 | VARCHAR | | | | | 1 | | | | | |
| | ns1000 | operate_log | lon | 12 | VARCHAR | | | | | 1 | | | | | |
| | ns1000 | operate_log | memberType | 12 | VARCHAR | | | | | 1 | | | | | |
| | ns1000 | operate_log | time | 12 | VARCHAR | | | | | 1 | | | | | |
+------------+--------------+--------------+--------------+------------+------------+--------------+----------------+-----------------+-----------------+-----------+----------+-------------+----------------+-------------------+--------+
0: jdbc:phoenix:> select * from "operate_log" limit 1;
+------------------------+-------------+-------+---------------------+---------------------+-------------+----------------+
| pk | appVersion | city | lat | lon | memberType | time |
+------------------------+-------------+-------+---------------------+---------------------+-------------+----------------+
1538216707720 | 2.22.0 | 重庆市 | 29.577587127685547 | 106.50493621826172 | 0 | 1538216707720 |
+------------------------+-------------+-------+---------------------+---------------------+-------------+----------------+
1 row selected (0.115 seconds)
0: jdbc:phoenix:> select count(*) from "operate_log";
+-----------+
| COUNT(1) |
+-----------+
| 2799827 |
+-----------+
1 row selected (3.848 seconds)
(可左右滑动)
4. 根据字段 time 进行时间范围查询:0: jdbc:phoenix:> select count(*) from "operate_log" where "f"."time" between '1538216707720' and '1538223834000';
+-----------+
| COUNT(1) |
+-----------+
| 5883 |
+-----------+
1 row selected (5.241 seconds)
(可左右滑动)
这里还要有两点说明:
Phoenix 会自动将表名、字段名都转成大写,如果要区分大小写使用双引号括起来即可。
这里我们创建的是视图,相当于外部表,也可以 create table 创建表,视图的特点是删除时不会删除 HBase 表,但是视图创建的二级索引不会自动更新,如果要实时更新的话,只能使用 create table,然后通过 Phoenix jdbc 的方式写入数据,只有通过 Phoenix 写,然后用 Phoenix 实现的协处理器才能实现实时更新的索引。
0: jdbc:phoenix:> create index index_operate_log_time on "operate_log" ("f"."time");
2,799,827 rows affected (95.814 seconds)
(可左右滑动)
2. 再次根据 time 字段做范围查询
00: jdbc:phoenix:> select count(*) from "operate_log" where "f"."time" between '1538216707720' and '1538223834000';
+-----------+
| COUNT(1) |
+-----------+
| 5883 |
+-----------+
1 row selected (0.049 seconds)
(可左右滑动)
这里基本上查询都在 50 ms 左右。这就是通过 Phoenix 的二级索引带来的性能提升。
Phoenix 提供了多种索引类型,包括覆盖索引、函数索引,以及全局索引与本地索引等,具体介绍如下。
覆盖索引是在索引表中直接存储某些常用字段,当查询时所有字段仅涉及索引表中包含的字段时,则无需再在基于 rowkey 索引的数据表中查询,提高了查询的效率。
比如,我们在operate_log 表 "f"."time" 列上创建一个索引,并在索引中包含 "f"."lat