HBase(七)—— HBase集成Phoenix

文章目录

  • 1. Phoenix简介
  • 2. 安装配置(各个节点上都需要配置)
  • 3. 基本命令
  • 4. 映射表

1. Phoenix简介

可以把Phoenix理解为HBase的查询引擎,它相当于一个Java中间件,帮助开发者,像使用JDBC访问关系型数据库一样,访问NoSQL数据库HBase。Phoenix由saleforce.com开源的一个项目,后又捐给了Apache。Phoenix操作的表及数据,存储在HBase上。Phoenix只是需要和HBase进行表关联起来。然后再用工具进行一些读或写操作。
可以把Phoenix只看成一种代替HBase的语法的一个工具。虽然可以用Java可以用JDBC来连接Phoenix,然后操作HBase,但是在生产环境中,不可以用在OLTP中。在线事务处理的环境中,需要低延迟,而Phoenix在查询HBase时,虽然做了一些优化,但延迟还是不小。所以依然是用在OLAT中,再将结果返回存储下来。

2. 安装配置(各个节点上都需要配置)

(1) 解压、重命名

tar -zxvf apache-phoenix-4.14.1-HBase-1.2-bin.tar.gz -C /opt/module
mv apache-phoenix-4.14.1-HBase-1.2-bin phoenix-4.14.1

(2) 修改环境变量

vim /etc/profile 
export PHOENIX_HOME=/opt/module/phoenix-4.14.1
export PATH=$PATH:$PHOENIX_HOME/bin
source /etc/profile

(3) 修改hbase-site.xml 将HBase的hbase-site.xml替换phoenix-4.14.1/bin/下的hbase-site.xml

cp hbase-site.xml /opt/module/phoenix-4.14.1/bin/

(4) 拷贝jar包到HBase下

cp /opt/module/phoenix-4.14.1/phoenix-4.14.1-HBase-1.3-server.jar /opt/module/hbase-1.3.1/lib/
cp /opt/module/phoenix-4.14.1/phoenix-core-4.14.1-HBase-1.3.jar /opt/module/hbase-1.3.1/lib/

(5) 将主节点的Phoenix包传到从节点

scp -r phoenix-4.14.1 root@bigdata13:/opt/module
scp -r phoenix-4.14.1 root@bigdata12:/opt/module

(6) 启动Phoenix
因为往HBase里拷贝了两个jar包,所以要重启一下HBase

stop-hbase.sh
start-hbase.sh
sqlline.py bigdata11,bigdata12,bigdata13:2181

3. 基本命令

-- 展示表
> !table

-- 创建表(不加引号建的表的表名默认是大写)
> create table test(id integer not null primary key,name varchar); -- 表名为TEST
> create table "test1"(d integer not null primary key,name varchar);

-- 删除表
> drop table test;

-- 插入数据
> upsert into test values(1,'aaa');
> upsert into "test1" values(1,'aaa');
> upsert into users(id) values(2);

-- 查询数据
phoenix > select * from test;
hbase > scan 'test'

-- 删除数据
delete from "test1" where id=4;

-- sum函数的使用
select sum(id) from "test1";

-- 增加一列
alter table "test1" add address varchar;

-- 删除一列
alter table "test1" drop column address;

-- 退出phoenix
> !q

其他语法详见:http://phoenix.apache.org/language/index.html

注:

(1) Phoenix查看HBase的表,只能查看到通过Phoenix创建的表,HBase原有的表查不到。
(2) 通过Phoenix创建的表,在HBase中查看,row和column的值都是16进制的 \x80\x00\x00\x01。

4. 映射表

-- HBase中创建表
create 'teacher1','info','contact'

-- 插入数据
put 'teacher1','1001','info:name','Jack'
put 'teacher1','1001','info:age','28'
put 'teacher1','1001','info:gender','male'
put 'teacher1','1001','contact:address','shanghai'
put 'teacher1','1001','contact:phone','13458646987'
put 'teacher1','1002','info:name','Jim'
put 'teacher1','1002','info:age','30'
put 'teacher1','1002','info:gender','male'
put 'teacher1','1002','contact:address','tianjian'
put 'teacher1','1002','contact:phone','13512436987'

-- 在Phoenix创建映射表,表名要和HBase中的表名一致,如果HBase中的表名或者字段是小写,这里一定要加上""。
create view "teacher1"(
"id" varchar primary key,
"contact"."address" varchar,
"contact"."phone" varchar,
"info"."age"  varchar,
"info"."gender" varchar,
"info"."name" varchar
);

-- 在Phoenix查找数据
select * from "teacher1";

你可能感兴趣的:(#,HBase,大数据,hbase,phoenix)