Phoenix实践——Phoenix配置支持二级索引和使用小记

支持配置:

phoenix的二级索引需要配置进行实现,步骤如下:增加配置不会报错,报错请考虑是版本监控问题。

1、HMaster配置

HMaster节点的$HBASE_HOME/conf/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>

2、HRegionserver配置

在每个HRegionserver节点上的$HBASE_HOME/conf/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>

重启服务

重启hbasephoenix

二级索引:

索引分类:

全局索引(global index)

全局索引是phoenix默认的索引格式。
特点:
适用于多读少写的业务场景。写数据的时候会消耗大量开销,因为索引表也要更新,而索引表是分布在不同的数据节点上的,跨节点的数据传输带来了较大的性能消耗。在读数据的时候Phoenix会选择索引表来降低查询消耗的时间。如果想查询的字段不是索引字段的话索引表不会被使用,也就是说不会带来查询速度的提升。

这里我们还是使用phoenix_table_mapping_user_info_test2,现在没有数据我们导入:

upsert into "phoenix_table_mapping_user_info_test2" values ('1002','Si','Li','aiyunxiao','北京朝阳');
upsert into "phoenix_table_mapping_user_info_test2" values ('1001','San','Zhang','aiyunxiao','北京朝阳');

查询并分析执行计划:
在这里插入图片描述
我们给company.name创建全局索引如下:

create index my_index on "phoenix_table_mapping_user_info_test2"("company"."name");

在这里插入图片描述

强制索引
select /*+ index(items my_index) */ * 
from items 
where price=0.8824734;
drop index my_name on usertable;
覆盖索引

覆盖索引 Covered Indexes,需要include包含需要返回数据结果的列。

create index index1_c on hao1 
(
  age
) 
include(name);  --name已经被缓存在这张索引表里了。

--对于
select name from hao1 where age=2  --查询效率和速度最快
select * from hao1 where age =2 --其他列不在索引表内,会全表扫描
本地索引(Local Index)

特点:
Local index适用于写操作频繁的场景。索引数据和数据表的数据是存放在相同的服务器中的,避免了在写操作的时候往不同服务器的索引表中写索引带来的额外开销。查询的字段不是索引字段索引表也会被使用,这会带来查询速度的提升。

我们创建一张新表phoenix_table_mapping_user_info_test3并准备好数据:

create table "phoenix_table_mapping_user_info_test3"
(
   user_id varchar primary key,
   "name"."firstname" varchar,
   "name"."lastname" varchar,
   "company"."name" varchar,
   "company"."address" varchar
) 
column_encoded_bytes=0;

--数据插入
upsert into "phoenix_table_mapping_user_info_test3" values ('1002','Si','Li','aiyunxiao','北京朝阳');
upsert into "phoenix_table_mapping_user_info_test3" values ('1001','San','Zhang','aiyunxiao','北京朝阳');

在这里插入图片描述
创建本地索引:

create local index my_local_index on "phoenix_table_mapping_user_info_test3"
(
	"company"."address"
);

在这里插入图片描述
相比上面的查询来说性能快了一倍
我们来看执行计划:
在这里插入图片描述

异步创建索引

异步创建索引,创建的索引表中不会有数据,单独使用命令行工具来执行数据的创建

--异步索引创建
create index index1_c on hao1 
(
	age
) 
include(name) async;

hbase org.apache.phoenix.mapreduce.index.indextool --schema my_schema --data-table my_table --index-table async_idx --output-path async_idx_hfiles

索引删除:

drop index my_index ON "phoenix_table_mapping_user_info_test1";
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扫地增

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值