# Phoenix

# 一、安装

  1. 官方 (opens new window)下载解压
[root@hdp1 opt]# tar -xzf phoenix-hbase-2.4-5.1.2-bin.tar.gz
1
  1. 复制jar包到到hbase目录

新版本:

[root@hdp1 lib]# cp /opt/phoenix/phoenix-server-hbase-2.4-5.1.2.jar /opt/hbase/lib/
1

老版本:

cp phoenix-core-5.0.0-HBase-2.0.jar /opt/hbase/lib/
cp phoenix-5.0.0-HBase-2.0-server.jar /opt/hbase/lib/
-- 还要下载htrace-core-3.2.0-incubating.jar 扔进去
1
2
3
  1. 重启Hbase
bin/stop-hbase.sh
bin/start-hbase.sh
1
2
  1. 配置相关Hbase、Hadoop配置文件
[root@hdp1 bin]# cd /opt/phoenix/bin
[root@hdp1 bin]# ln -s $HBASE_HOME/conf/hbase-site.xml .
[root@hdp1 bin]# ln -s $HADOOP_HOME/etc/hadoop/core-site.xml .
[root@hdp1 bin]# ln -s $HADOOP_HOME/etc/hadoop/hdfs-site.xml .
1
2
3
4
  1. 启动Phoenix客户端验证是否成功
[root@hdp1 bin]# ./sqlline.py hdp1:2181
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:hdp1:2181"
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/phoenix-hbase-2.4-5.1.2-bin/phoenix-client-hbase-2.4-5.1.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hadoop-2.10.1/share/hadoop/common/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
Connecting to jdbc:phoenix:hdp1:2181
22/01/20 18:47:45 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

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:hdp1:2181>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  1. 开启 hbase 的 namespace 和 phoenix 的 schema 的映射

为了开启 hbase 的 namespace 和 phoenix 的 schema 的映射,在程序中需要加这 个配置文件,另外在 linux 服务上,也需要在 hbase 以及 phoenix 的 hbase-site.xml 配置 文件中,加上以上两个配置,并使用 xsync 进行同步。

    <property> 
        <name>phoenix.schema.isNamespaceMappingEnabled</name> 
        <value>true</value>
    </property>
    <property> 
        <name>phoenix.schema.mapSystemTablesToNamespace</name> 
        <value>true</value>
    </property>
1
2
3
4
5
6
7
8
  1. 配置HBase支持Phoenix二级索引

在每一个HRegionServer的hbase-site.xml加入以下属性

    <property>
        <name>hbase.regionserver.wal.codec</name>
        <value>org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec</value>
    </property>
1
2
3
4

# 二、使用

# 2.1 基础使用

# 0.基础

-- 显示表列表
!table

-- 查看表内容
0: jdbc:phoenix:hdp1:2181> select * from "user_info";

-- 创建数据库schema
create schema dice_realtime
1
2
3
4
5
6
7
8

# 1.创建一个用户表,该用户有以下列

ID 姓名 年龄 性别 地址
1 张三 30 北京西城区
2 李四 20 上海闵行区
-- 创建表
create table if not exists "user_info"(
    "id" varchar primary key,
    "cf"."name" varchar,
    "cf"."age" integer,
    "cf"."sex" varchar,
    "cf"."address" varchar
);

-- 新增数据
upsert into "user_info" values('1','张三',30,'男','北京西城区');
upsert into "user_info" values('2','李四',20,'女','上海市闵行区');
1
2
3
4
5
6
7
8
9
10
11
12

# 2.修改id为1用户的年龄为45

-- 修改数据
upsert into "user_info"("id","age") values('1',35);
1
2

# 3.删除id为2的用户数据

-- 删除数据
delete from "user_info" where "id"='2';
1
2

# 4.建立映射视图

hbase建表见hbase文档

-- 创建映射
create view if not exists "employee"(
    "rowid" varchar not null primary key,
    "company"."name" varchar,
    "company"."position" varchar,
    "family"."tel" varchar
);

-- 查询
0: jdbc:phoenix:hdp1:2181> select * from "employee" where "name"='ted';
+-------+------+----------+-------------+
| rowid | name | position |     tel     |
+-------+------+----------+-------------+
| row1  | ted  | worker   | 13600912345 |
+-------+------+----------+-------------+
1 row selected (0.022 seconds)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 2.2 创建二级索引

HBase通过rowkey来查询,否则就必须逐行地比较每一列的值,即全表扫描。

# 使用Phoenix创建二级索引

  1. 创建索引
create local index "idx_tel" on "employee"("family"."tel");
1
  1. 查看执行计划,检查是否查询二级索引
explain select * from "employee" where "name"='ted';
explain select * from "employee" where "tel"='1234912341324';
1
2
  1. 删除索引
drop index "idx_tel" on "employee";
1
更新时间: 1/27/2022, 6:04:00 PM