HBase继承Phoenix插件用SQL来管理HBase

1. 安装使用

(1)拷贝对应的安装包,本文使用的是apache-phoenix-4.14.0-cdh5.14.2-bin.tar.gz,解压到你的安装目录,本文的目录为/opt/modules/cdh5.14.2/

(2)将Phoenix安装目录下的phoenix-4.14.0-cdh5.14.2-client.jar拷贝到HBase的lib目录下

(3)将Phoenix安装目录下的phoenix-core-4.14.0-cdh5.14.2.jar 拷贝到HBase的lib目录下

(4)将HBase/conf目录下hbase-site.xml文件覆盖放入Phoenix的bin目录下

安装完成!

2. 启动说明

(1)启动HBase

1. 启动Zookeeper

[fanl@centos7 zookeeper-3.4.5-cdh5.14.2]$ bin/zkServer.sh start

2. 启动HBase

[fanl@centos7 hbase-1.2.0-cdh5.14.2]$ bin/hbase-daemon.sh start master
[fanl@centos7 hbase-1.2.0-cdh5.14.2]$ bin/hbase-daemon.sh start regionServer
[fanl@centos7 cdh5.14.2]$ jps
9808 DataNode
11188 HRegionServer
10997 HMaster
10567 QuorumPeerMain
9675 NameNode
9979 SecondaryNameNode
12188 Jps

(2)启动Phoenix

[fanl@centos7 phoenix-4.14.0-cdh5.14.2]$ bin/sqlline.py centos7:2181

Done
sqlline version 1.2.0
0: jdbc:phoenix:centos7:2181> 

3. 操作说明

(1)查看内置命令

0: jdbc:phoenix:centos7:2181> !help

(2)查看已经映射的表

0: jdbc:phoenix:centos7:2181> !tables

HBase继承Phoenix插件用SQL来管理HBase_第1张图片

 (3)创建表create

0: jdbc:phoenix:centos7:2181> create table user_ph(
. . . . . . . . . . . . . . > id varchar primary key,
. . . . . . . . . . . . . . > info.name varchar,
. . . . . . . . . . . . . . > info.pwd varchar);
No rows affected (2.364 seconds)

Phoenix客户端中的表名和字段名不加双引号则默认都为大写,加上双引号则区分大小写。

在HBase客户端查看表的创建

hbase(main):001:0> list
TABLE      
USER_PH 

(5)新增/修改数据upsert

    插入数据时表名和列名因为都是小写的所以要加双引号,值为字符串时只能用单引号引起否则报错。

0: jdbc:phoenix:centos7:2181> upsert into user_ph(id,info.name,info.pwd) 
. . . . . . . . . . . . . . > values('001','fanl01','123456');
1 row affected (0.039 seconds)

查看数据 scan

(6)删除数据,表和其他关系型数据差不多,不再演示。

(7)退出客户端的命令为!quit

4. 案例操作

案例1:分组聚合查询:以州为维度统计表中每个州的城市数量及总人口,并以总人口数量降序排序

数据准备

create table "us_population"(
"state" varchar(2),
"city" varchar(15),
"info"."population" bigint,
constraint pk primary key("state","city")
);
upsert into "us_population"("state","city","info"."population") values('NY','New York',8143197);  
upsert into "us_population"("state","city","info"."population") values('CA','Los Angeles',3844829); 
upsert into "us_population"("state","city","info"."population") values('IL','Chicago',2842518);
upsert into "us_population"("state","city","info"."population") values('TX','Houston',2016582);
upsert into "us_population"("state","city","info"."population") values('PA','Philadelphia',1463281);  
upsert into "us_population"("state","city","info"."population") values('AZ','Phoenix',1461575);
upsert into "us_population"("state","city","info"."population") values('TX','San Antonio',1256509);  
upsert into "us_population"("state","city","info"."population") values('CA','San Diego',1255540);
upsert into "us_population"("state","city","info"."population") values('TX','Dallas',1213825);
upsert into "us_population"("state","city","info"."population") values('CA','San Jose',912332);

查询结果

0: jdbc:phoenix:centos7:2181> select * from  "us_population" ;
+--------+---------------+-------------+
| state  |     city      | population  |
+--------+---------------+-------------+
| AZ     | Phoenix       | 1461575     |
| CA     | Los Angeles   | 3844829     |
| CA     | San Diego     | 1255540     |
| CA     | San Jose      | 912332      |
| IL     | Chicago       | 2842518     |
| NY     | New York      | 8143197     |
| PA     | Philadelphia  | 1463281     |
| TX     | Dallas        | 1213825     |
| TX     | Houston       | 2016582     |
| TX     | San Antonio   | 1256509     |
+--------+---------------+-------------+
10 rows selected (0.037 seconds)
0: jdbc:phoenix:centos7:2181> 

 聚合查询

0: jdbc:phoenix:centos7:2181> select "state" as "State",
. . . . . . . . . . . . . . > count("city") "City",
. . . . . . . . . . . . . . > sum("population") "Population" from "us_population" 
. . . . . . . . . . . . . . > group by "state" order by sum("population") desc;
+--------+-------+-------------+
| State  | City  | Population  |
+--------+-------+-------------+
| NY     | 1     | 8143197     |
| CA     | 3     | 6012701     |
| TX     | 3     | 4486916     |
| IL     | 1     | 2842518     |
| PA     | 1     | 1463281     |
| AZ     | 1     | 1461575     |
+--------+-------+-------------+
6 rows selected (0.038 seconds)
0: jdbc:phoenix:centos7:2181> 

案例二:在Phoenix端关联HBase中已经存在的表

方式1:创建映射视图,删除视图不影响HBase中的数据

create view "teacher"(
"ROW" varchar primary key,
"info"."name" varchar,
"info"."age"  varchar,
"info"."gender" varchar,
"contact"."address" varchar,
"contact"."phone" varchar
);

方式2:创建映射表,删除表/修改表会同步影响HBase中的数据

create table "teacher"(
"ROW" varchar primary key,
"contact"."address" varchar,
"contact"."phone" varchar,
"info"."age"  varchar,
"info"."gender" varchar,
"info"."name" varchar
)
column_encoded_bytes=0;

案例三:执行脚本

$ bin/psql.py 192.168.134.101:2181 你的脚本路径

 

你可能感兴趣的:(大数据)