3.1 配置Hbase支持Phoenix建立二級索引
3.1.1 添加以下配置到Hbase的Hregionserver節點的hbase-site.xmlhtml
<!-- 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>
添加以下配置到Hbase中Hmaster節點的hbase-site.xml中apache
<!-- 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>
對於cdh配置,須要配置hbase-site.xml高級代碼段客戶端配置增長如下段,支持hbase的命名空間服務器
<property> <name>phoenix.schema.isNamespaceMappingEnabled</name> <value>true</value></property><property> <name>phoenix.schema.mapSystemTablesToNamespace</name> <value>true</value></property>
3.1.3 驗證效果:app
3.2 建立索引
3.2.1 phoenix的索引分類
1)global index是默認的索引格式。適用於多讀少寫的業務場景。寫數據的時候會消耗大量開銷,由於索引表也要更新,而索引表是分佈在不一樣的數據節點上的,跨節點的數據傳輸帶來了較大的性能消耗。在讀數據的時候Phoenix會選擇索引表來下降查詢消耗的時間。若是想查詢的字段不是索引字段的話索引表不會被使用,也就是說不會帶來查詢速度的提高。分佈式
CREATE INDEX my_index ON my_table (my_col)
2)Local index適用於寫操做頻繁的場景。索引數據和數據表的數據是存放在相同的服務器中的,避免了在寫操做的時候往不一樣服務器的索引表中寫索引帶來的額外開銷。查詢的字段不是索引字段索引表也會被使用,這會帶來查詢速度的提高。
創建local index可能出現的問題
hbase-site.xml的zookeeeper的配置信息不能加2181,不然在建立local index的時候
正常配置:
hbase.zookeeper.quorum
hadoop101,hadoop102,hadoop103
Local index 和 Global index區別:ide
Local index 和 Global index區別:
Local index 因爲是數據與索引在同一服務器上,因此要查詢的數據在哪臺服務器的哪一個region是沒法定位的,只能先找到region而後在利用索引。
Global index 是一種分佈式索引,能夠直接利用索引定位服務器和region,速度更快,可是因爲分佈式的緣由,數據一旦出現新增變化,分佈式的索引要進行跨服務的同步操做,帶來大量的通訊消耗。因此在寫操做頻繁的字段上不適合創建Global index。oop
三種提高效率查詢方式
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)
如何刪除索引
DROP INDEX my_index ON my_table性能