目录
[二、Phoenix 安装](#二、Phoenix 安装)
[1. 解压、配置环境](#1. 解压、配置环境)
[2. 将 phoenix-server-hbase-2.5-5.1.3.jar 文件复制到 HBase 的 lib 目录中](#2. 将 phoenix-server-hbase-2.5-5.1.3.jar 文件复制到 HBase 的 lib 目录中)
[3. 重启 HBase 集群](#3. 重启 HBase 集群)
[4. 安装验证](#4. 安装验证)
[(1)连接 HBase](#(1)连接 HBase)
一、主机规划
继续上一篇,本篇介绍在同一环境中安装 Phoenix,并连接上篇部署的 HBase 集群。
所需安装包:Phoenix-5.1.3
下表描述了四个节点上分别将会运行的相关进程。Phoenix 相对于 HBase 来说就是一个支持 SQL 的客户端软件,为能在集群环境中任何节点上都能使用 Phoenix 命令行,在所有节点上都安装。简便起见,安装部署过程中所用的命令都使用操作系统的 root 用户执行。
|---------------|-----------|-----------|-----------|-----------|
| 节点 进程 | node1 | node2 | node3 | node4 |
| Phoenix | * | * | * | * |
二、Phoenix 安装
在所有节点上执行下面第1、2步操作,在 node1 节点上执行第3步操作。
1. 解压、配置环境
# 解压
tar -zxvf phoenix-hbase-2.5-5.1.3-bin.tar.gz
# 编辑 /etc/profile 文件
vim /etc/profile
# 添加下面两行
export PHOENIX_HOME=/root/phoenix-hbase-2.5-5.1.3-bin/
export PHOENIX_CLASSPATH=$PHOENIX_HOME
export PATH=$PHOENIX_HOME/bin:$PATH
# 加载生效
source /etc/profile
2. 将 phoenix-server-hbase-2.5-5.1.3.jar 文件复制到 HBase 的 lib 目录中
cp $PHOENIX_HOME/phoenix-server-hbase-2.5-5.1.3.jar $HBASE_HOME/lib/
3. 重启 HBase 集群
stop-hbase.sh
start-hbase.sh
4. 安装验证
(1)连接 HBase
# 连接,参数为 Zookeeper 节点
sqlline.py node1,node2,node3
# 列出表
!table
输出:
[root@vvml-yz-hbase-test~]#sqlline.py node1,node2,node3
Setting property: [incremental, false]
Setting property: [isolation, TRANSACTION_READ_COMMITTED]
issuing: !connect -p driver org.apache.phoenix.jdbc.PhoenixDriver -p user "none" -p password "none" "jdbc:phoenix:node1,node2,node3"
Connecting to jdbc:phoenix:node1,node2,node3
24/03/07 14:44:45 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
24/03/07 14:44:45 WARN impl.MetricsConfig: Cannot locate configuration: tried hadoop-metrics2-phoenix.properties,hadoop-metrics2.properties
Connected to: Phoenix (version 5.1)
Driver: PhoenixEmbeddedDriver (version 5.1)
Autocommit status: true
Transaction isolation: TRANSACTION_READ_COMMITTED
sqlline version 1.9.0
0: jdbc:phoenix:node1,node2,node3> !table
+-----------+-------------+------------+--------------+---------+-----------+---------------------------+----------------+-------------+-+
| TABLE_CAT | TABLE_SCHEM | TABLE_NAME | TABLE_TYPE | REMARKS | TYPE_NAME | SELF_REFERENCING_COL_NAME | REF_GENERATION | INDEX_STATE | |
+-----------+-------------+------------+--------------+---------+-----------+---------------------------+----------------+-------------+-+
| | SYSTEM | CATALOG | SYSTEM TABLE | | | | | | |
| | SYSTEM | CHILD_LINK | SYSTEM TABLE | | | | | | |
| | SYSTEM | FUNCTION | SYSTEM TABLE | | | | | | |
| | SYSTEM | LOG | SYSTEM TABLE | | | | | | |
| | SYSTEM | MUTEX | SYSTEM TABLE | | | | | | |
| | SYSTEM | SEQUENCE | SYSTEM TABLE | | | | | | |
| | SYSTEM | STATS | SYSTEM TABLE | | | | | | |
| | SYSTEM | TASK | SYSTEM TABLE | | | | | | |
+-----------+-------------+------------+--------------+---------+-----------+---------------------------+----------------+-------------+-+
0: jdbc:phoenix:node1,node2,node3>
默认情况下,直接在 HBase 中创建的表,通过 Phoenix 是查看不到的。如上一篇在 hbase shell 中创建的 test 表,这里没有显示。如果要在 Phoenix 中操作直接在 HBase 中创建的表,则需要在 Phoenix 中进行表的映射。映射方式有两种:视图映射和表映射。
(2)视图映射
Phoenix 创建的视图是只读的,所以只能用来做查询,无法通过视图对源数据进行修改等操作。
0: jdbc:phoenix:node1,node2,node3> create view "test"(pk varchar primary key,"cf"."a" varchar,"cf"."b" varchar,"cf"."c" varchar,"cf"."d" varchar,"cf"."e" varchar);
No rows affected (0.178 seconds)
0: jdbc:phoenix:node1,node2,node3> !table
+-----------+-------------+------------+--------------+---------+-----------+---------------------------+----------------+-------------+-+
| TABLE_CAT | TABLE_SCHEM | TABLE_NAME | TABLE_TYPE | REMARKS | TYPE_NAME | SELF_REFERENCING_COL_NAME | REF_GENERATION | INDEX_STATE | |
+-----------+-------------+------------+--------------+---------+-----------+---------------------------+----------------+-------------+-+
| | SYSTEM | CATALOG | SYSTEM TABLE | | | | | | |
| | SYSTEM | CHILD_LINK | SYSTEM TABLE | | | | | | |
| | SYSTEM | FUNCTION | SYSTEM TABLE | | | | | | |
| | SYSTEM | LOG | SYSTEM TABLE | | | | | | |
| | SYSTEM | MUTEX | SYSTEM TABLE | | | | | | |
| | SYSTEM | SEQUENCE | SYSTEM TABLE | | | | | | |
| | SYSTEM | STATS | SYSTEM TABLE | | | | | | |
| | SYSTEM | TASK | SYSTEM TABLE | | | | | | |
| | | test | VIEW | | | | | | |
+-----------+-------------+------------+--------------+---------+-----------+---------------------------+----------------+-------------+-+
0: jdbc:phoenix:node1,node2,node3> select * from "test";
+------+--------+--------+--------+--------+--------+
| PK | a | b | c | d | e |
+------+--------+--------+--------+--------+--------+
| row1 | value1 | | | | |
| row2 | | value2 | | | |
| row3 | | | value3 | | |
| row4 | | | | value4 | |
| row5 | | | | | value5 |
+------+--------+--------+--------+--------+--------+
5 rows selected (0.021 seconds)
0: jdbc:phoenix:node1,node2,node3>
HBase 严格区分大小写,创建视图时表名、列族、列名需要用双引号括起来。
(3)表映射
0: jdbc:phoenix:node1,node2,node3> drop view "test";
No rows affected (0.011 seconds)
0: jdbc:phoenix:node1,node2,node3> create table "test"(pk varchar primary key,"cf"."a" varchar,"cf"."b" varchar,"cf"."c" varchar,"cf"."d" varchar,"cf"."e" varchar) column_encoded_bytes=0;
5 rows affected (5.676 seconds)
0: jdbc:phoenix:node1,node2,node3> upsert into "test" values('a','1','2','3','4','5');
1 row affected (0.007 seconds)
0: jdbc:phoenix:node1,node2,node3> select * from "test";
+------+--------+--------+--------+--------+--------+
| PK | a | b | c | d | e |
+------+--------+--------+--------+--------+--------+
| a | 1 | 2 | 3 | 4 | 5 |
| row1 | value1 | | | | |
| row2 | | value2 | | | |
| row3 | | | value3 | | |
| row4 | | | | value4 | |
| row5 | | | | | value5 |
+------+--------+--------+--------+--------+--------+
6 rows selected (0.013 seconds)
0: jdbc:phoenix:node1,node2,node3> !quit
Closing: org.apache.phoenix.jdbc.PhoenixConnection
[root@vvml-yz-hbase-test~]#
表映射方式时,数据更新是对源表的操作,删除表也会删除 HBase 中的源表。如果只做查询,强烈建议使用视图方式映射,删除视图不影响 HBase 源数据。Phoenix 4.10 版本后,对列映射做了优化,采用一套新的机制,不再基于列名方式映射到 HBase。如果必须要表映射,则需要禁用列映射规则(column_encoded_bytes=0),但这会降低查询性能。