1. Phoenix创建Hbase二级索引
配置Hbase支持Phoenix创建二级索引
1. 添加如下配置到Hbase的Hregionserver节点的hbase-site.xml
<!-- 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>
2. 添加如下配置到Hbase中Hmaster节点的hbase-site.xml中
<!-- phoenix master 配置参数 --> <property> <name>hbase.master.loadbalancer.class</name> <value>org.apache.phoenix.hbase.index.balancer.IndexLoadBalancer</value> </property> <property> <name>hbase.coprocessor.master.classes</name> <value>org.apache.phoenix.hbase.index.master.IndexMasterObserver</value> </property>
3. 常见问题汇总:
1)注意:网上配置文档里有这一条,但在实际测试中(测试环境hbase-1.3.1,网上0.98.6),加入该条的regionserver会在hbase启动时失败,对应节点上没有HregionServer进程,去掉该配置后正常启动,且能正常创建local index。
<property> <name>hbase.coprocessor.regionserver.classes</name> <value>org.apache.hadoop.hbase.regionserver.LocalIndexMerger</value> </property>
2)hbase-site.xml的zookeeeper的配置信息不能加2181,否则在创建local index的时候会报以下异常:
正常配置:
<property> <name>hbase.zookeeper.quorum</name> <value>hadoop101,hadoop102,hadoop103</value> </property>
2. 创建索引
phoenix的索引分类
1)global index是默认的索引格式。
适用于多读少写的业务场景。写数据的时候会消耗大量开销,因为索引表也要更新,而索引表是分布在不同的数据节点上的,跨节点的数据传输带来了较大的性能消耗。在读数据的时候Phoenix会选择索引表来降低查询消耗的时间。如果想查询的字段不是索引字段的话索引表不会被使用,也就是说不会带来查询速度的提升。
CREATE INDEX "my_index" ON "student"("info"."dt");
2)Local index适用于写操作频繁的场景。
索引数据和数据表的数据是存放在相同的服务器中的,避免了在写操作的时候往不同服务器的索引表中写索引带来的额外开销。查询的字段不是索引字段索引表也会被使用,这会带来查询速度的提升。
CREATE LOCAL INDEX my_index ON my_table (my_index)
3)异步创建索引
一般我们可以使用CREATE INDEX来创建一个索引,这是一种同步的方法。但是有时候我们创建索引的表非常大,我们需要等很长时间。Phoenix 4.5以后有一个异步创建索引的方式,使用关键字ASYNC来创建索引:
create index MY_INDEX on FRUITS("info"."name") include ("info"."color") async;
这时候创建的索引表中不会有数据。你还必须要单独的使用命令行工具来执行数据的创建。当语句给执行的时候,后端会启动一个map reduce任务,只有等到这个任务结束,数据都被生成在索引表中后,这个索引才能被使用。启动工具的方法:
${HBASE_HOME}/bin/hbase org.apache.phoenix.mapreduce.index.IndexTool \ > --data-table FRUITS --index-table MY_INDEX \ > --output-path ASYNC_IDX_HFILES
注: --schema MY_SCHEMA,不加这个就是默认的schema
注: 上边尽量不要用小写,即使""双引号里边写的是小写它也不识别;
2019-10-10 15:04:01,579 INFO [main] index.IndexTool: Loading HFiles from ASYNC_IDX_HFILES/MY_INDEX 2019-10-10 15:04:01,723 WARN [main] mapreduce.LoadIncrementalHFiles: Skipping non-directory hdfs://hadoop101:9000/user/kris/ASYNC_IDXEX/_SUCCESS 2019-10-10 15:04:01,909 INFO [LoadIncrementalHFiles-0] hfile.CacheConfig: Created cacheConfig: CacheConfig:disabled 2019-10-10 15:04:01,985 INFO [LoadIncrementalHFiles-0] mapreduce.LoadIncrementalHFiles: Trying to load hfile=hdfs://hadoop101:9000/usIDX_HFILES/MY_INDEX/info/6b69a7babd4e4c629b6bee988cf15c5d first=water\x001005 last=watermelon\x001006 2019-10-10 15:04:02,136 INFO [main] index.IndexToolUtil: Updated the status of the index MY_INDEX to ACTIVE
这个任务不会因为客户端给关闭而结束,是在后台运行。你可以在指定的文件ASYNC_IDX_HFILES中找到最终实行的结果。
测试:
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)
如何删除索引
use "test"再删索引 drop index "my_index" on "student"不行,必须前边加schema,默认的不需要加
DROP INDEX "my_index" ON "test"."student"