配置Hbase支持Phoenix创建二级索引
1 添加如下配置到Hbase的Hregionserver节点的hbase-site.xml
| hbase.regionserver.wal.codec org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec hbase.region.server.rpc.scheduler.factory.class org.apache.hadoop.hbase.ipc.PhoenixRpcSchedulerFactory Factory to create the Phoenix RPC Scheduler that uses separate queues for index and metadata updates hbase.rpc.controllerfactory.class org.apache.hadoop.hbase.ipc.controller.ServerRpcControllerFactory Factory to create the Phoenix RPC Scheduler that uses separate queues for index and metadata updates |
2 添加如下配置到Hbase中Hmaster节点的hbase-site.xml中
| hbase.master.loadbalancer.class org.apache.phoenix.hbase.index.balancer.IndexLoadBalancer hbase.coprocessor.master.classes org.apache.phoenix.hbase.index.master.IndexMasterObserver |
3 常见问题汇总:
1)注意:网上配置文档里有这一条,但在实际测试中(测试环境hbase-1.3.1,网上0.98.6),加入该条的regionserver会在hbase启动时失败,对应节点上没有HregionServer进程,去掉该配置后正常启动,且能正常创建local index。
| hbase.coprocessor.regionserver.classes org.apache.hadoop.hbase.regionserver.LocalIndexMerger |
2)hbase-site.xml的zookeeeper的配置信息不能加2181,否则在创建local index的时候会报以下异常:

正常配置:
| hbase.zookeeper.quorum hadoop101,hadoop102,hadoop103 |
创建索引
1 phoenix的索引分类
1)global index是默认的索引格式。适用于多读少写的业务场景。写数据的时候会消耗大量开销,因为索引表也要更新,而索引表是分布在不同的数据节点上的,跨节点的数据传输带来了较大的性能消耗。在读数据的时候Phoenix会选择索引表来降低查询消耗的时间。如果想查询的字段不是索引字段的话索引表不会被使用,也就是说不会带来查询速度的提升。
| CREATE INDEX my_index ON my_table (my_index) |
2)Local index适用于写操作频繁的场景。索引数据和数据表的数据是存放在相同的服务器中的,避免了在写操作的时候往不同服务器的索引表中写索引带来的额外开销。查询的字段不是索引字段索引表也会被使用,这会带来查询速度的提升。
| CREATE LOCAL INDEX my_index ON my_table (my_index) |
2 三种提升效率查询方式
| 1) CREATE INDEX my_index ON my_table (v1) INCLUDE (v2) |
| 2) SELECT /*+ INDEX(my_table my_index) */ v2 FROM my_table WHERE v1 = ‘foo’ |
| 3) CREATE LOCAL INDEX my_index ON my_table (v1) |
3 如何删除索引
| DROP INDEX my_index ON my_table |