大数据之Phoenix与Hbase集成(二级索引)

前言

Phoenix的一级索引就是它的主键,对应的就是hbase的rowkey,这个是默认的机制,我们不需要额外操作。

故二级索引就是非主键/rowkey列的索引。创建二级索引的目的就是为了加快查询速度。

Hbase只能基于rowkey去查询数据,要是基于其他列查询数据就只能使用filter的功能,但是效果十分不好。他是全表扫描,性能比较差。通过Phoenix创建索引可以避免全表扫描,因此就增加了查询的效率。

配置文件

Phoenix二级索引需要Hbase的配置支持。

添加如下配置到HBase的HRegionserver节点的hbase-site.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- phoenix regionserver 配置参数-->
<property>
<name>hbase.regionserver.wal.codec</name>
<value>org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec</value>
</property>

<property>
<name>hbase.region.server.rpc.scheduler.factory.class</name>
<value>org.apache.hadoop.hbase.ipc.PhoenixRpcSchedulerFactory</value>
<description>Factory to create the Phoenix RPC Scheduler that uses separate queues for index and metadata updates</description>
</property>

<property>
<name>hbase.rpc.controllerfactory.class</name>
<value>org.apache.hadoop.hbase.ipc.controller.ServerRpcControllerFactory</value>
<description>Factory to create the Phoenix RPC Scheduler that uses separate queues for index and metadata updates</description>
</property>

Phoenix二级索引

全局二级索引

Global Index是默认的索引格式,创建全局索引时,会在HBase中建立一张新表。

也就是说索引数据和数据表是存放在不同的表中的,因此全局索引适用于多读少写的业务场景。
写数据的时候会消耗大量开销,因为索引表也要更新,而索引表是分布在不同的数据节点上的,跨节点的数据传输带来了较大的性能消耗。
在读数据的时候Phoenix会选择索引表来降低查询消耗的时间。

1.创建单个字段的全局索引

1
CREATE INDEX my_index ON my_table (my_col);

如果想查询的字段不是索引字段的话索引表不会被使用,也就是说不会带来查询速度的提升。

2.创建携带其他字段的全局索引

1
CREATE INDEX my_index ON my_table (v1) INCLUDE (v2);

本地二级索引

Local Index适用于写操作频繁的场景。
索引数据和数据表的数据是存放在同一张表中(且是同一个Region),避免了在写操作的时候往不同服务器的索引表中写索引带来的额外开销。

1
CREATE LOCAL INDEX my_index ON my_table (my_column);