1 Phoenix简介

1.1 Phoenix定义

Phoenix是HBase的开源SQL皮肤。可以使用标准JDBC API代替HBase客户端API来创建表,插入数据和查询HBase数据。

1.2 Phoenix特点

1)容易集成:如Spark,Hive,Pig,Flume和Map Reduce;

2)操作简单:DML命令以及通过DDL命令创建和操作表和版本化增量更改;

3)支持HBase二级索引创建。

1.3 Phoenix架构


2 Phoenix快速入门

2.1 安装

1.官网地址

http://phoenix.apache.org/

2.Phoenix部署

1)上传并解压tar包

[bigdata@hadoop102 module]$ tar -zxvf apache-phoenix-5.0.0-HBase-2.0-bin.tar.gz -C /opt/module/

[bigdata@hadoop102 module]$ mv apache-phoenix-5.0.0-HBase-2.0-bin phoenix

2)复制server包并拷贝到各个节点的hbase/lib

[bigdata@hadoop102 module]$ cd /opt/module/phoenix/

[bigdata@hadoop102 phoenix]$ cp /opt/module/phoenix/phoenix-5.0.0-HBase-2.0-server.jar /opt/module/hbase/lib/

[bigdata@hadoop102 phoenix]$ xsync /opt/module/hbase/lib/phoenix-5.0.0-HBase-2.0-server.jar

4)配置环境变量

#phoenixexport PHOENIX_HOME=/opt/module/phoenixexport PHOENIX_CLASSPATH=$PHOENIX_HOMEexport PATH=$PATH:$PHOENIX_HOME/bin

5)重启HBase

[bigdata@hadoop102 ~]$ stop-hbase.sh

[bigdata@hadoop102 ~]$ start-hbase.sh

6) 连接Phoenix

[bigdata@hadoop101 phoenix]$ bin/sqlline.py hadoop102,hadoop103,hadoop104:2181

2.2 Phoenix Shell操作

1.表的操作

1)显示所有表

!table 或 !tables

2)创建表

直接指定单个列作为RowKey

CREATE TABLE IF NOT EXISTS student(id VARCHAR primary key,name VARCHAR,addr VARCHAR);

在phoenix中,表名等会自动转换为大写,若要小写,使用双引号,如"us_population"。

指定多个列的联合作为RowKey

CREATE TABLE IF NOT EXISTS us_population (State CHAR(2) NOT NULL,City VARCHAR NOT NULL,Population BIGINTCONSTRAINT my_pk PRIMARY KEY (state, city));

3)插入数据

upsert into student values('1001','zhangsan','beijing');

4)查询记录

select * from student;

select * from student where id='1001';

5)删除记录

delete from student where id='1001';

6)删除表

drop table student;

7)退出命令行

!quit

2.表的映射

1)表的关系

默认情况下,直接在HBase中创建的表,通过Phoenix是查看不到的。如果要在Phoenix中操作直接在HBase中创建的表,则需要在Phoenix中进行表的映射。映射方式有两种:视图映射和表映射。

2)命令行中创建表test

HBase 中test的表结构如下,两个列族info1、info2。


hbase存储的中文是什么格式 hbase存储数据_hbase存储的中文是什么格式


启动HBase Shell

[bigdata@hadoop102 ~]$ /opt/module/hbase/bin/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;

2.3 Phoenix JDBC操作

1.启动query server

[bigdata@hadoop102 ~]$ queryserver.py start

2.创建项目并导入依赖

org.apache.phoenix            phoenix-queryserver-client            5.0.0-HBase-2.0

3.编写代码

package com.bigdata;import java.sql.*;import org.apache.phoenix.queryserver.client.ThinClientUtil;public class PhoenixTest {public static void main(String[] args) throws SQLException {        String connectionUrl = ThinClientUtil.getConnectionUrl("hadoop102", 8765);        System.out.println(connectionUrl);        Connection connection = DriverManager.getConnection(connectionUrl);        PreparedStatement preparedStatement = connection.prepareStatement("select * from student");        ResultSet resultSet = preparedStatement.executeQuery();        while (resultSet.next()) {            System.out.println(resultSet.getString(1) + "" + resultSet.getString(2));        }        //关闭        connection.close();}}

3 Phoenix二级索引

3.1 二级索引配置文件

1.添加如下配置到HBase的HRegionserver节点的hbase-site.xml

hbase.regionserver.wal.codecorg.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodechbase.region.server.rpc.scheduler.factory.classorg.apache.hadoop.hbase.ipc.PhoenixRpcSchedulerFactoryFactory to create the Phoenix RPC Scheduler that uses separate queues for index and metadata updateshbase.rpc.controllerfactory.classorg.apache.hadoop.hbase.ipc.controller.ServerRpcControllerFactoryFactory to create the Phoenix RPC Scheduler that uses separate queues for index and metadata updates

3.2 全局二级索引

Global Index是默认的索引格式,创建全局索引时,会在HBase中建立一张新表。也就是说索引数据和数据表是存放在不同的表中的,因此全局索引适用于多读少写的业务场景。

写数据的时候会消耗大量开销,因为索引表也要更新,而索引表是分布在不同的数据节点上的,跨节点的数据传输带来了较大的性能消耗。

在读数据的时候Phoenix会选择索引表来降低查询消耗的时间。

1.创建单个字段的全局索引

CREATE INDEX my_index ON my_table (my_col);


hbase存储的中文是什么格式 hbase存储数据_hbase存储的中文是什么格式_02


如果想查询的字段不是索引字段的话索引表不会被使用,也就是说不会带来查询速度的提升。

2.创建携带其他字段的全局索引

CREATE INDEX my_index ON my_table (v1) INCLUDE (v2);


hbase存储的中文是什么格式 hbase存储数据_apache_03


3.3 本地二级索引

Local Index适用于写操作频繁的场景。

索引数据和数据表的数据是存放在同一张表中(且是同一个Region),避免了在写操作的时候往不同服务器的索引表中写索引带来的额外开销。

CREATE LOCAL INDEX my_index ON my_table (my_column);


hbase存储的中文是什么格式 hbase存储数据_hadoop_04