Phoenix可以使用标准SQL来操作HBase

安装前建议关闭HBase、Zookeeper、Hadoop服务

下载Phoenix

首先应下载Phoenix: Apache Phoenix ,下载apache-phoenix-5.0.0-HBase-2.0-bin.tar.gz,并将其上传至master节点

安装Phoenix

解压到opt目录

tar -zxvf apache-phoenix-5.0.0-HBase-2.0-bin.tar.gz -C /opt/
cd /opt
mv apache-phoenix-5.0.0-HBase-2.0-bin/ phoenix/

复制phoenix目录下的两个jar文件到hbase/lib下

cd /opt/phoenix
cp phoenix-5.0.0-HBase-2.0-client.jar /opt/hbase/lib/
cp phoenix-core-5.0.0-HBase-2.0.jar /opt/hbase/lib/

并将其分发到所有子节点上

scp phoenix-5.0.0-HBase-2.0-client.jar slave1:/opt/hbase/lib/
scp phoenix-core-5.0.0-HBase-2.0.jar slave1:/opt/hbase/lib/

配置Phoenix

将Hadoop的和HBase的配置文件复制到Phoenix/bin下

cp /opt/hadoop/etc/hadoop/core-site.xml /opt/phoenix/bin/
cp /opt/hadoop/etc/hadoop/hdfs-site.xml /opt/phoenix/bin/
cp /opt/hbase/conf/hbase-site.xml /opt/phoenix/bin/

在master上配置环境变量

vi /etc/profile
=> # 最后添加
export PHOENIX_HOME=/opt/phoenix  
export PHOENIX_CLASSPATH=$PHOENIX_HOME  
export PATH=$PATH:$PHOENIX_HOME/bin
<= # :wq
source /etc/profile

设置脚本为可执行

cd /opt/phoenix/bin
chmod +x psql.py
chmod +x sqlline.py

运行Phoenix

安装Phoenix之前不建议开启HBase。如果之前已经启动HBase,需重启

cd /opt/hbase/bin
./stop-hbase.sh # 可能需要手动关闭子节点
./start-hbase.sh

start-hb

验证是否安装成功:进入命令行

cd /opt/phoenix/bin
./sqlline.py master,slave1

sqlline

执行!tables命令,罗列出所有表到客户端的界面

!tables

创建表:

create table student ("id" varchar(20) primary key,"name" varchar(20) ,"age" integer);

插入/更新数据:

upsert into student values('1','zhangsan',22);  
upsert into student values('2', 'lisi',32);  
upsert into student values('3', 'wangwu',52);  
upsert into student values('4', 'zhaoliu',12);  
upsert into student values('5', 'yangqi',43);  
upsert into student values('6', 'wangwu',32);  
upsert into student values('7', 'zhaoliu',72);

读取数据:

select * from student;

select_all

# ID降序
select "id", "name", "age" from student order by to_number("id") desc;

select_all_id_desc

退出命令行

!exit