Phoenix快速入门
Phoenix简介
Phoenix定义
Phoenix是HBase的开源SQL皮肤。可以使用标准JDBC API代替HBase客户端API来创建表,插入数据和查询HBase数据。
Phoenix特点
- 容易集成:如SparkHivePigFlumeMap Reduce
- 操作简单: DML命令以及通过DDL命令创建和操作表和版本化增量更改
- 支持HBase二级索引[^1]创建
扩展:想要实现高效查询非RowKey字段,可以建立一张二级索引表,将RowKey与其他字段建立映射关系。
Phoenix架构
Phoenix快速入门
安装
-
官网地址
http;//phoenix.apache.org/
-
Phoenix部署
注意:Hbase2.0.5与Phoenix2.0有一点小的不兼容。上传并解压tar包
tar -zxvf 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
-
配置环境变量
#phoenix export PHOENIX_HOME=/opt/module/phoenix export PHOENIX_CLASSPATH=$PHOENIX_HOME export PATH=$PATH:$PHOENIX_HOME/bin
-
复制server包(协处理器)并拷贝到各个节点的Hbase/lib
[atguigu@hadoop102 module]$ cd /opt/module/phoenix/ [atguigu@hadoop102 phoenix]$ cp /opt/module/phoenix/phoenix-5.0.0-HBase-2.0-server.jar /opt/module/hbase/lib/ [atguigu@hadoop102 phoenix]$ xsync /opt/module/hbase/lib/phoenix-5.0.0-HBase-2.0-server.jar
-
重启Hbase
-
连接Phoenix (thick)
[atguigu@hadoop101 phoenix]$ /opt/module/phoenix/bin/sqlline.py hadoop102,hadoop103,hadoop104:2181
Phoenix Shell操作
表的操作
-
创建schema
create schema bigdata;
!:创建表之前,需要在hbase/bin/hbase-site.xml和phoenix/bin/hbase-site.xml中添加参数phoenix.schema.isNamespaceMappingEnabled
在phoenix中,表名等会自动转换为大写,若要小写,使用双引号,如"us_population"。
-
使用schema
use bigdata;
-
查看所有表
!table 或 !tables8
-
创建表
CREATE TABLE IF NOT EXISTS student( id VARCHAR primary key, name VARCHAR, addr VARCHAR);
# 指明多个列联合作为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))
建表时 phoenix 至Hbase中列名编码功能转换??
-
插入数据
upsert into student values('1001','zhangsan','beijing');
-
查询记录
select * from student; select * from student where id = '1001'
!: 在Hbase中查看在Phoenix中插入的数据时,会发现多一条数据。这条数据是为了插入的数据只有RowKey,其他字段为空。因为Hbase存储的是kv对,而只有RowKey插入时,需要一个value来帮助数据插入。
-
删除记录
delete from student where id = '1001';
-
删除表
delete table student;
-
退出命令行
!quit
表的映射
默认情况下,直接在HBase中创建的表,通过Phoenix是查看不到的。如果要在Phoenix中操作直接在HBase中创建的表,则需要在Phoenix中进行表的映射。映射方式有两种:视图映射和表映射。
首先在Hbase中创建test
[atguigu@hadoop102 ~]$ /opt/module/hbase/bin/hbase
hbase(main):001:0> create 'test','info1','info2'
-
视图映射</