Phoenix是Hbase的开源SQL皮肤。可以使用标准JDBC API代替Hbase客户端API来创建表,插入数据和查询Hbase数据。
Phoenix特点1)容易集成:如Spark,Hive,Pig,Flume和Map Reduce;
2)操作简单:DML命令以及通过DDL命令创建和操作表和版本化增量更改;
3)支持Hbase二级索引创建。
安装Overview | Apache Phoenix
0)安装bsdtar3
sudo yum install -y epel-release sudo yum install -y bsdtar3
1)上传并解压tar包(这里可能会报不识别的文件头,忽略即可,或者可以改用bsdtar)
tar -zxvf /opt/software/apache-phoenix-5.0.0-Hbase-2.0-bin.tar.gz -C /opt/module mv apache-phoenix-5.0.0-Hbase-2.0-bin phoenix
2)复制server包并拷贝到各个节点的hbase/lib
3)复制client包并拷贝到各个节点的hbase/lib
cp phoenix-5.0.0-Hbase-2.0-client.jar /opt/module/hbase/lib/ cp phoenix-5.0.0-Hbase-2.0-server.jar /opt/module/hbase/lib/
4)配置环境变量
#phoenix export PHOENIX_HOME=/opt/module/phoenix export PHOENIX_CLASSPATH=$PHOENIX_HOME export PATH=$PATH:$PHOENIX_HOME/bin
5)启动Phoenix
sqlline.py hadoop102,hadoop103,hadoop104:2181
thin client queryserver.py stop/startPhoenix Shell操作
1)显示所有表
!table 或 !tables
2)创建表
直接指定单个列作为RowKey
create table if not exists student(id integer primary key, name varchar);
在phoenix中,表名等会自动转换为大写,若要小写,使用双引号,如"us_population"。
指定多个列的联合作为RowKey
CREATE TABLE IF NOT EXISTS us_population ( State CHAr(2) NOT NULL, City VARCHAR NOT NULL, Population BIGINT CONSTRAINT my_pk PRIMARY KEY (state, city));
3)插入数据
upsert into student values(1001,'zhangsan');
4)查询记录
select * from student; select * from student where id='1001';
5)删除记录
delete from student where id='1001';
6)删除表
drop table student;
7)退出命令行
!quit表的映射
1)表的关系
默认情况下,直接在Hbase中创建的表,通过Phoenix是查看不到的。如果要在Phoenix中操作直接在Hbase中创建的表,则需要在Phoenix中进行表的映射。映射方式有两种:视图映射和表映射。
2)命令行中创建表test
Hbase 中test的表结构如下,两个列族info1、info2。
| Rowkey | info1 | info2 |
| id | name | address |
启动Hbase Shell
hbase shell
创建Hbase表test
hbase(main):001:0> create 'test','info1','info2'
3)视图映射
Phoenix创建的视图是只读的,所以只能用来做查询,无法通过视图对源数据进行修改等操作。在phoenix中创建关联test表的视图
0: jdbc:phoenix:hadoop101,hadoop102,hadoop103> create view "test"(id varchar primary key,"info1"."name" varchar, "info2"."address" varchar);
删除视图
0: jdbc:phoenix:hadoop101,hadoop102,hadoop103> drop view "test";
4)表映射
使用Apache Phoenix创建对Hbase的表映射,有两种方法:
(1)Hbase中不存在表时,可以直接使用create table指令创建需要的表,系统将会自动在Phoenix和Hbase中创建person_infomation的表,并会根据指令内的参数对表结构进行初始化。
(2)当Hbase中已经存在表时,可以以类似创建视图的方式创建关联表,只需要将create view改为create table即可。
0: jdbc:phoenix:hadoop101,hadoop102,hadoop103> create table "test"(id varchar primary key,"info1"."name" varchar, "info2"."address" varchar) column_encoded_bytes=0;Phoenix JDBC操作
依赖
org.apache.phoenix phoenix-queryserver-client5.0.0-Hbase-2.0 junit junit4.12 org.apache.logging.log4j log4j-slf4j-impl2.12.0
package phoenix;
import org.apache.phoenix.queryserver.client.ThinClientUtil;
import java.sql.*;
public class Phoenix {
public static void main(String[] args) throws SQLException {
//1.获取jdbc url
String url = ThinClientUtil.getConnectionUrl("hadoop102", 8765);
System.out.println(url);
//2.根据url获取连接
Connection connection = DriverManager.getConnection(url);
PreparedStatement preparedStatement = connection.prepareStatement("select * from student");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
System.out.println(resultSet.getInt(1) + "t"
+ resultSet.getString(2));
}
}
}
Phoenix二级索引
二级索引配置文件
添加如下配置到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 hbase.master.loadbalancer.class org.apache.phoenix.hbase.index.balancer.IndexLoadBalancer hbase.coprocessor.master.classes org.apache.phoenix.hbase.index.master.IndexMasterObserver
全局二级索引
Global Index是默认的索引格式,创建全局索引时,会在Hbase中建立一张新表。也就是说索引数据和数据表是存放在不同的表中的,因此全局索引适用于多读少写的业务场景。
写数据的时候会消耗大量开销,因为索引表也要更新,而索引表是分布在不同的数据节点上的,跨节点的数据传输带来了较大的性能消耗。
在读数据的时候Phoenix会选择索引表来降低查询消耗的时间。
未建立student列索引
1)创建单个字段的全局索引
为student列创建索引
CREATE INDEX student_index on student (name);
hbase中创建了索引表。
建立索引前后查询对比
2)多级索引
当查询含有非索引列时,索引失效
explain select id,name,sex from test where name='2';
此时应用可建立多级索引
CREATE INDEX test_name_index_sex_index on test (name,sex);
使用索引时顺序需要与建立的索引表一致否则索引失效
顺序一致才能使用索引
删除索引
DROP INDEX test_name_index_sex_index on test;
3)创建携带其他字段的全局索引
CREATE INDEX test_name_index_all ON test (name) INCLUDE (sex,address);
查询主键内含有列时可以直接使用range scan ,将include内容插入索引表中
索引的缺点:1.浪费空间。2.数据更新时协处理器会更新所有的索引表。
本地二级索引Local Index适用于写操作频繁的场景。
索引数据和数据表的数据是存放在同一张表中(且是同一个Region),避免了在写操作的时候往不同服务器的索引表中写索引带来的额外开销。查询的字段不是索引字段索引表也会被使用,这会带来查询速度的提升。
CREATE LOCAL INDEX test_index_local ON test (name);
索引数据和数据表的数据是存放在同一张表中
hbase中并没有本地所有表
本地索引将索引值和主键拼接成key插入
查找时,通过过滤信息找到主键,再通过主键进行行匹配。
总结:全局索引查询速度更快,全局索引空间代价更高,本地索引删改更快。
全局索引适用于查多写少,本地索引适用于修改更多的表。



