第一章.整合Phoenix
1.Phoenix介绍
Phoenix定义:
- Phoenix是HBase的开源SQL皮肤,可以使用标准JDBC API代替HBase客户端API来创建表,插入数据和查询HBase数据
Phoenix特点:
- 容易集成,如Spark,Hive,Pig,Flume,和MAP Reduce
- 操作简单,DML命令以及通过DDL命令创建和操作表和版本化增量更改
- 支持HBase二级索引创建
Phoenix架构
![$03[HBase-Phoenix] - 图1](/uploads/projects/liuye-6lcqc@gx6gw9/bbb4f50fbb296ba776c9fa377fa8f7ac.png)
2.Phoenix安装
- 官网地址: http://phoenix.apache.org/
- 上传并解压tar包
tar -zxvf apache-phoenix-5.0.0-HBase-2.0-bin.tar.gz -C /opt/module/mv apache-phoenix-5.0.0-HBase-2.0-bin phoenix-5.0.0
- 复制server包并拷贝到各个节点的hbase-2.0.5/lib
cp /opt/module/phoenix-5.0.0/phoenix-5.0.0-HBase-2.0-server.jar /opt/module/hbase-2.0.5/libxsync /opt/module/hbase-2.0.5/lib/phoenix-5.0.0-HBase-2.0-server.jar
- 配置环境变量
#phoenixexport PHOENIX_HOME=/opt/module/phoenix-5.0.0export PHOENIX_CLASSPATH=$PHOENIX_HOMEexport PATH=$PATH:$PHOENIX_HOME/bin
- 启动HBase(应先启动Zookeeper和Hadoop)
start-hbase.sh
- 连接Phoenix
/opt/module/phoenix-5.0.0/bin/sqlline.py hadoop102,hadoop103,hadoop104:2181
3.Phoenix Shell操作
1.schema的操作
- 创建schema
默认情况下,在Phoenix中不能直接创建schema,需要将如下的参数添加到Hbase中conf目录下的hbase-site.xml和Phoenix中bin目录的hbase-site.xml,更改完之后记得分发到其他节点
<property><name>phoenix.schema.isNamespaceMappingEnabled</name><value>true</value></property>
- 重新启动Hbase和连接Phoenix客户端
start-hbase.shsqlline.py hadoop102,hadoop103,hadoop104:2181
- 创建schema
create schema bigdata;
注意在Phoenix中,schema名表名,字段名等会自动转换为大写,若要小写,使用双引号,如"student"
2. 表的操作
- 显示所有表
! table 或 ! tables
![$03[HBase-Phoenix] - 图2](/uploads/projects/liuye-6lcqc@gx6gw9/50551e6b3ea06b49dcec307da671bd06.png)
- 创建表
# 直接指定单个列作为RowKeycreate table if not exists student(id varchar primary key,name varchar,addr varchar);# 指定多个列的联合作为RowKeycreate table if not exists us_population(state char(2) not null,city varchar not null,Population bigintconstraint my_pk primary key (state,city));
- 插入数据
upsert into student values('1001','zhangsan','beijing');
- 查询记录
select * from student;select * from student where id = '1001';
- 删除记录
delete from student where id = '1001';
- 删除表
drop table student;
- 退出命令行
!quit
3.表的映射
- 表的关系
默认情况下,直接在Hbase中创建的表,通过Phoenix是查看不到的,如果要在Phoenix中操作在Hbase中创建的表,则需要在Phoenix中进行表的映射,映射方式有两种: 视图映射和表映射
- 命令行中创建表test
Hbase中test表结构如下,两个列族info1,info2
| Rowkey | info1 | info2 |
|---|---|---|
| id | name | address |
启动Hbase Shell
hbase shell
创建HBase表test
create 'test','info1','info2'
- 视图映射
Phoenix创建的视图是只读的,所以只能用来做查询,无法通过视图对原数据进行修改等操作,在Phoenix中创建关联test表的视图
create view "test"(id varchar primary key,"info1"."name" varchar,"info2"."address" varchar);
删除视图
drop view "test";
- 表映射
使用Apache Phoenix创建对Hbase的表映射,有两种方法
# HBase中不存在表时可以直接使用create table指令创建需要的表,系统将会自动在Phoenix和Hbase中创建person_infomation的表,并且根据指令内的参数对表结构进行初始化# 当HBase中已经存在表时可以以类似创建视图的方式,创建关联表,只需要将create view 改为create table即可create table "test"(id varchar primary key,"info1"."name" varchar, "info2"."address" varchar) column_encoded_bytes=0;
4.表映射中数值类型的问题
- Hbase中存储数值类型的值(如int,long等)会按照正常数字的补码进行存储. 而phoenix对数字的存储做了特殊的处理. phoenix 为了解决遇到正负数同时存在时,导致负数排到了正数的后面(负数高位为1,正数高位为0,字典序0 < 1)的问题。 phoenix在存储数字时会对高位进行转换.原来为1,转换为0, 原来为0,转换为1.- 因此,如果hbase表中的数据的写是由phoenix写入的,不会出现问题,因为对数字的编解码都是phoenix来负责。如果hbase表中的数据不是由phoenix写入的,数字的编码由hbase负责. 而phoenix读数据时要对数字进行解码。 因为编解码方式不一致。导致数字出错.
- 在hbase中创建表,并插入数值类型的数据
create 'person','info'put 'person','1001','info:salary',Bytes.toBytes(123456)# 注意:如果要插入数字类型,需要通过Bytes.toBytes(123456)来实现
- 在Phoenix创建映射表并查询数据
create table "person"(id varchar primary key,"info"."salary" integer)column_encoded_bytes=0;
![$03[HBase-Phoenix] - 图3](/uploads/projects/liuye-6lcqc@gx6gw9/ec307c1464af847dcb639cf661047900.png)
可以看出: 数字显示有问题
- 解决方法
在Phoenix中创建表时使用无符号的数值类型unsigned_long
create table "person"(id varchar primary key,"info"."salary" unsigned_long)column_encoded_bytes=0;
![$03[HBase-Phoenix] - 图4](/uploads/projects/liuye-6lcqc@gx6gw9/fb69851920443ec9dd62bdb9f90884b1.png)
5.Phoenix JDBC操作
- Thin Client
启动query server
queryserver.py startqu
创建项目并导入依赖
<dependency><groupId>org.apache.phoenix</groupId><artifactId>phoenix-queryserver-client</artifactId><version>5.0.0-HBase-2.0</version></dependency>
编写代码
package com.atguigu.hbaseapi;import org.apache.phoenix.queryserver.client.ThinClientUtil;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;public class PhoenixThin {public static void main(String[] args) throws Exception {// 1. 获取连接对象String url = ThinClientUtil.getConnectionUrl("hadoop102", 8765);Connection connection = DriverManager.getConnection(url);// 2. 编写SQLString sql = "select * from student";// 3. 预编译PreparedStatement preparedStatement = connection.prepareStatement(sql);// 4. 执行ResultSet resultSet = preparedStatement.executeQuery();// 5. 解析Resultwhile (resultSet.next()){String id = resultSet.getString("id");String name = resultSet.getString("name");String addr = resultSet.getString("addr");System.out.println("id: "+id+", name: "+name+", addr: " +addr);}// 6. 关闭资源connection.close();}}
- Thick Client
在pom中添加依赖
<dependency><groupId>org.apache.phoenix</groupId><artifactId>phoenix-core</artifactId><version>5.0.0-HBase-2.0</version><exclusions><exclusion><groupId>org.glassfish</groupId><artifactId>javax.el</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.glassfish</groupId><artifactId>javax.el</artifactId><version>3.0.1-b06</version></dependency>
编写代码
package com.atguigu.hbaseapi;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.Properties;public class PhoenixThink {public static void main(String[] args) throws Exception{// 1. 获取连接对象Properties properties = new Properties();properties.put("phoenix.schema.isNamespaceMappingEnabled", "true");Connection connection = DriverManager.getConnection("jdbc:phoenix:hadoop102:2181",properties);// 2. 编写SQLString sql = "select * from student";// 3. 预编译PreparedStatement preparedStatement = connection.prepareStatement(sql);// 4. 执行ResultSet resultSet = preparedStatement.executeQuery();// 5. 解析Resultwhile (resultSet.next()){String id = resultSet.getString("id");String name = resultSet.getString("name");String addr = resultSet.getString("addr");System.out.println("id: "+id+", name: "+name+", addr: " +addr);}// 6. 关闭资源connection.close();}}
6.Phoenix二级索引
1.二级索引配置文件
添加如下配置到hbase的hbase-site.xml
<!-- phoenix regionserver 配置参数--><property><name>hbase.regionserver.wal.codec</name><value>org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec</value></property>
2.全局二级索引
Global Index是默认的索引格式,创建全局索引时,会在Hbase中建立一张新表,也就是说索引数据和数据表是存放在不同的表中的,因此全局索引适用于多读少写的业务场景写数据的时候,会消耗大量开销,因为索引表也要更新,而索引表是分布在不同的数据节点上的,跨节点的数据传输带来了较大的性能消耗在读数据的时候Phoenix会选择索引表来降低查询消耗的时间
创建单个字段的全局索引
CREATE INDEX my_index ON my_table (my_col);
![$03[HBase-Phoenix] - 图5](/uploads/projects/liuye-6lcqc@gx6gw9/e0e56dad06bc109335a22ac4479958c5.png)
- 在没有索引的情况下
![$03[HBase-Phoenix] - 图6](/uploads/projects/liuye-6lcqc@gx6gw9/3bd06e616fcc8ce69bb81b144079bfec.png)
![$03[HBase-Phoenix] - 图7](/uploads/projects/liuye-6lcqc@gx6gw9/ac460ab7ea545cdca040009fdae0a13d.png)
![$03[HBase-Phoenix] - 图8](/uploads/projects/liuye-6lcqc@gx6gw9/4075aff884248c8333ae52784aee8c2e.png)
- 在name字段上创建索引
create index "index_student_name" on student (name);
查看Phoenix上索引表
![$03[HBase-Phoenix] - 图9](/uploads/projects/liuye-6lcqc@gx6gw9/69a954bc6d3b1534d3aa6057bf3c19ce.png)
![$03[HBase-Phoenix] - 图10](/uploads/projects/liuye-6lcqc@gx6gw9/147767232e53ca65d09e005a0db8bf79.png)
![$03[HBase-Phoenix] - 图11](/uploads/projects/liuye-6lcqc@gx6gw9/22756f5ff47817a6590627f8e667e44b.png)
![$03[HBase-Phoenix] - 图12](/uploads/projects/liuye-6lcqc@gx6gw9/3361f2d5485c613f428443a670f873b0.png)
创建携带其他字段的全局索引
CREATE INDEX my_index ON my_table (v1) INCLUDE (v2);
# 删除索引"index_student_name"drop index "index_student_name" on student;# 创建包含索引"index_student_name_include_addr"(是将数据打包进索引行中,查询时无需查询原表)create index "index_student_name_include_addr" on student (name) include (addr);
- 查看Phoenix上的索引表
![$03[HBase-Phoenix] - 图13](/uploads/projects/liuye-6lcqc@gx6gw9/b1fa1686f94bb2372310a656aed6d6d0.png)
![$03[HBase-Phoenix] - 图14](/uploads/projects/liuye-6lcqc@gx6gw9/7766e274e3679837f050a65798e09b82.png)
![$03[HBase-Phoenix] - 图15](/uploads/projects/liuye-6lcqc@gx6gw9/0f9eb7757ddfad92c9686d68a741f53e.png)
![$03[HBase-Phoenix] - 图16](/uploads/projects/liuye-6lcqc@gx6gw9/c1fb94388f6c34cfa7755f7f08c75477.png)
3.本地二级索引
Local Index使用于写操作频繁的场景索引数据和数据表的数据时存放在同一张表中(且是同一个Region),避免了在写操作的时候往不同的服务器的索引表中写索引带来的额外开销
- 创建本地二级索引在name字段上
create local index "local_index_student_name" on student (name);
- 查看Phoenix上的索引表
![$03[HBase-Phoenix] - 图17](/uploads/projects/liuye-6lcqc@gx6gw9/0c6f747d7af14f6595a3d044ad2b5a83.png)
![$03[HBase-Phoenix] - 图18](/uploads/projects/liuye-6lcqc@gx6gw9/19d8e627c1e4f29fe54c0d281126f53e.png)
![$03[HBase-Phoenix] - 图19](/uploads/projects/liuye-6lcqc@gx6gw9/42a972b78ab0ad8949345e497f2f73bc.png)
![$03[HBase-Phoenix] - 图20](/uploads/projects/liuye-6lcqc@gx6gw9/012ab3966c26546fbfe5630ee2617248.png)
![$03[HBase-Phoenix] - 图21](/uploads/projects/liuye-6lcqc@gx6gw9/d1d779435208b243d28251edfb9ef238.png)
第二章.与Hive的集成
1.HBase与Hive的对比
# Hive(1) 数据分析工具,本质就相当于将HDFS中已将存储的文件在mysql上做了一个双射关系,以方便使用HQL去管理,查询(2) 用于数据分析,清洗,Hive适用于离线的数据分析和清洗,延迟较高(3) 基于HDFS,MapReduce,Hive存储的数据依旧在DataNode上,编写的HQL语句终将转换为MapReduce代码执行------------------------------------------------------------------------------------# HBase(1) 数据库, 是一种面向列族存储的非关系型数据库(2) 用于存储结构化和非结构化的数据, 适用于单表非关系型数据的存储,不适合做关联查询,类似JOIN等操作(3) 基于HDFS, 数据持久化存储的体现形式是HFile,存放于DataNode上,被Resionserver以region的形式进行管理(4) 延迟较低,接入在线业务使用,面对大量的企业数据,Hbase可以直接单表大量数据的存储,同时提供了高效的数据访问速度
2.HBase与Hive的集成使用
在hive-site.xml中添加zookeeper的属性
<property><name>hive.zookeeper.quorum</name><value>hadoop102,hadoop103,hadoop104</value></property><property><name>hive.zookeeper.client.port</name><value>2181</value></property>
案例一
建立Hive表,关联Hbase表,插入数据到Hive表的时候能够影响HBase表
- 在Hive表中创建表的同时关联HBase
CREATE TABLE hive_hbase_emp_table(empno int,ename string,job string,mgr int,hiredate string,sal double,comm double,deptno int)STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:ename,info:job,info:mgr,info:hiredate,info:sal,info:comm,info:deptno")TBLPROPERTIES ("hbase.table.name" = "hbase_emp_table");
提示: 完成之后,可以分别进入Hive和HBase中查看,都生成了对应的表
- 在hive中创建临时中间表,用于load文件中的数据
CREATE TABLE emp(empno int,ename string,job string,mgr int,hiredate string,sal double,comm double,deptno int)row format delimited fields terminated by '\t';
提示: 不能将数据直接load进Hive所关联Hbase的那张表中
- 向HIve中间表load数据
load data local inpath '/home/atguigu/emp.txt' into table emp;
- 通过insert指令将中间表的数据导入到Hive关联Hbase的那张表中
insert into table hive_hbase_emp_table select * from emp;
- 查看Hive以及关联的Hbase表中是否已经同步插入了数据
# Hiveselect * from hive_hbase_emp_table;# HBasescan 'hbase_emp_table'
案例二
在Hbase表中已经存储了某一张表hbase_emp_table,然后在hive中创建一个外部表来关联Hbase中的hbase_emp_table这张表,使之可以借助Hive来分析HBase这张表中的数据(在案例一的基础上)
- 在Hive中创建外部表
CREATE EXTERNAL TABLE relevance_hbase_emp(empno int,ename string,job string,mgr int,hiredate string,sal double,comm double,deptno int)STORED BY'org.apache.hadoop.hive.hbase.HBaseStorageHandler'WITH SERDEPROPERTIES ("hbase.columns.mapping" =":key,info:ename,info:job,info:mgr,info:hiredate,info:sal,info:comm,info:deptno")TBLPROPERTIES ("hbase.table.name" = "hbase_emp_table");
- 关联后就可以使用Hive函数进行一些分析操作
select * from relevance_hbase_emp;
