分享

Phoenix系列入门(七)二级索引之— —Local Indexing

本帖最后由 levycui 于 2015-11-27 14:11 编辑
问题导读
1、Phoenix支持哪两种类型的索引技术?

2、什么是Local Indexing?
3、Local Indexing如何配置?



1. 说明

在HBase中,只有一个单一的按照字典序排序的rowKey索引,当使用rowKey来进行数据查询的时候速度较快,但是如果不使用rowKey来查询的话就会使用filter来对全表进行扫描,很大程度上降低了检索性能。而Phoenix提供了二级索引技术来应对这种使用rowKey之外的条件进行检索的场景。

Phoenix支持两种类型的索引技术:Global Indexing和Local Indexing,这两种索引技术分别适用于不同的业务场景(主要是偏重于读还是偏重于写)。下面分别对这两种索引技术简单使用一下,具体性能方面没有进行测试。

以上文字摘自官方文档
http://phoenix.apache.org/secondary_indexing.html

本篇主要介绍Local Indexing相关技术。

2. Local Indexing

Local indexing targets write heavy, space constrained use cases. Just like with global indexes, Phoenix will automatically select whether or not to use a local index at query-time. With local indexes, index data and table data co-reside on same server preventing any network overhead during writes. Local indexes can be used even when the query isn’t fully covered (i.e. Phoenix automatically retrieve the columns not in the index through point gets against the data table). Unlike global indexes, all local indexes of a table are stored in a single, separate shared table.At read time when the local index is used, every region must be examined for the data as the exact region location of index data cannot be predetermined.Thus some overhead occurs at read-time.

Local indexing适用于写操作频繁的场景。与Global indexing一样,Phoenix会自动判定在进行查询的时候是否使用索引。使用Local indexing时,索引数据和数据表的数据是存放在相同的服务器中的避免了在写操作的时候往不同服务器的索引表中写索引带来的额外开销。使用Local indexing的时候即使查询的字段不是索引字段索引表也会被使用,这会带来查询速度的提升,这点跟Global indexing不同。一个数据表的所有索引数据都存储在一个单一的独立的可共享的表中。在读取数据的时候,标红的那句话不会翻译大意就是在读数据的时候因为存储数据的region的位置无法预测导致性能有一定损耗。

2.1 配置hbase-site.xml

使用Local Indexing的话需要配置hbase-site.xml,在HBase集群的master节点的hbase-site.xml中添加如下配置并重启HBase集群。

Local indexing also requires special configurations in the master to ensure data table and local index regions co-location.

配置这个参数的目的是确保数据表与索引表协同定位。

[mw_shl_code=applescript,true]<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>[/mw_shl_code]

高能预警:如果使用的是Phoenix 4.3+的版本的话还需要在HBase集群的每个regionserver节点的hbase-site.xml中添加如下配置并重启HBase集群。

    To support local index regions merge on data regions merge you will need to add the following parameter to hbase-site.xml in all the region servers and restart. (It’s applicable for Phoenix 4.3+ versions)

    这个配置是为了支持在数据region合并之上进行索引region合并(这句话感觉翻译的不太准确)。

[mw_shl_code=applescript,true]<property>
    <name>hbase.coprocessor.regionserver.classes</name>
    <value>org.apache.hadoop.hbase.regionserver.LocalIndexMerger</value>
</property>[/mw_shl_code]

2.2 创建表


进入phoenix的CLI的界面创建company表。

[mw_shl_code=applescript,true]create table company(id varchar primary key, name varchar, address varchar);[/mw_shl_code]
20150509115725684.png

查看company表索引

[mw_shl_code=applescript,true]!indexes company[/mw_shl_code]
20150509115655792.png

2.3 创建索引

对company表的name字段创建索引,索引名为my_index。

[mw_shl_code=applescript,true] create local index my_index on company(name);[/mw_shl_code]

查看当前所有表会发现多一张MY_INDEX索引表,查询该表数据。
20150510102811243.png

通过squirrel来查看company的索引字段。
20150510102812597.png

从HBase的CLI界面查看当前所有表。

[mw_shl_code=applescript,true] list[/mw_shl_code]
20150510102916651.png

高能预警:这里的索引表并不叫MY_INDEX,而是叫_LOCAL_IDX_COMPANY,但是在Phoenix的CLI中进行数据查询的时候仍然是使用MY_INDEX进行查询,应该是做了映射。

2.4 插入数据

在company表中添加测试数据。

[mw_shl_code=applescript,true]upsert into company(id, name, address) values('001', 'dimensoft', 'nanjing');[/mw_shl_code]

2.5 查询数据

查看company表数据以及索引表my_index数据。

[mw_shl_code=applescript,true] select * from company;
select * from my_index;[/mw_shl_code]
20150510103909062.png

从HBase的CLI界面查看索引表_LOCAL_IDX_COMPANY。

[mw_shl_code=applescript,true]scan '_LOCAL_IDX_COMPANY'[/mw_shl_code]
20150510104102147.png

3个索引字段_INDEX_ID、NAME和ID的值被合并为索引表的rowKey,其中_INDEX_ID并没有值(\x000是十六进制表示,转换为字符串是空格)。


原文:http://blog.csdn.net/maomaosi2009/article/details/45619047

已有(1)人评论

跳转到指定楼层
ljlinux2012 发表于 2017-3-2 00:08:58
ddddddddddddd
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条