Hbase二级索引_Hive on Hbase 及phoenix详解
知识点02:课程目标
- SQL on Hbase
- 使用SQL语句来操作Hbase
- Hbase不支持SQL接口
- 额外的工具来实现
- Hive on Hbase【了解】
- 使用Hive中的SQL语句来实现对Hbase数据的操作
- 本质:通过MapReduce来实现读写Hbase
- Phoenix【重点】
- 专门为Hbase所设计的一个工具
- 本质:直接封装Hbase的JavaAPI来实现的
- 功能、应用场景、基本原理、特点
- 基本使用:语法【upsert、delete、select】
知识点03:SQL on Hbase
- 问题
- Hbase是列存储NoSQL,不支持SQL,开发接口不方便大部分用户使用,怎么办?
- 分析
- 应用场景:应用系统或者大数据存储系统
- 大数据存储系统:大数据工程师
- 利用Hbase来存储大量要分析处理的数据
- 使用JavaAPI通过MapReduce或者通过Spark来实现数据的读写
- Java
- Scala
- 应用系统:Java工程师、数据分析师
- 利用Hbase来存储大量的商品数据、订单数据,来提供高性能的查询
- 问题:Java人员不会Hbase Java API,对于数据库会JDBC
- 解决:需要一个工具能让Hbase支持SQL,支持JDBC方式对Hbase进行处理
- Hbase的结构是否能实现基于SQL的查询操作?
- 普通表数据:按行操作
id name age sex addr 001 zhangsan 18 null shanghai 002 lisi 20 female null 003 wangwu null male beijing ……
- Hbase数据:按列操作
rowkey cf1:id cf1:name cf1:age cf2:sex cf2:addr zhangsan_001 001 zhangsan 18 null shanghai lisi_002 002 lisi 20 female null wangwu_003 003 wangwu null male beijing ……
- 可以基于Hbase数据构建结构化的数据形式
- 可以用SQL来实现处理
- 实现
- 将Hbase表中每一行对应的所有列构建一张完整的结构化表
- 如果这一行没有这一列,就补null
- Hive:通过MapReduce来实现
- Phoenix:通过Hbase API封装实现的
- 总结
- 原因:满足各种应用场景下,对于Hbase使用的方式,基于SQL方式会更加通用
- 实现:将整张表的数据构建结构化形式,每一行没有列就补null
- 原理:将SQL转换成了Hbase的客户端操作来实现的
知识点04:Hive on Hbase 介绍
- 功能:实现Hive与Hbase集成,使用Hive SQL对Hbase的数据进行处理
- 原理
- Hive的功能:使用HQL对表的数据进行处理
- 本质:通过MapReduce对HDFS中的文件进行处理
- 原理
- TextInputFormat:读文件
- TextOutputFormat:写文件
- MapReduce的功能:读取数据进行分布式计算
- InputFormat:输入类
- TextInputFormat:默认的输入类,用于读取文件系统
- DBInputFormat:用于读取JDBC数据库
- 实现Sqoop导入的:将MySQL数据导入到Hive或者HDFS
- TableInputFormat:用于读取Hbase数据
- OutputFormat:输出类
- TextOutputFormat:默认的输出类,用于将结果写入文件系统
- DBOutputFormat:用于写入JDBC数据库
- 实现Sqoop导出的:将HDFS数据写入MySQL
- TableOutputFormat:用于写入HBase数据库
- 原理:Hive可以通过MapReduce来实现映射读写Hbase表的数据
- 特点
- 优点:支持完善的SQL语句,可以实现各种复杂SQL的数据处理及计算,通过分布式计算程序实现,对大数据量的数据处理比较友好
- 缺点:不支持二级索引,数据量不是特别大的情况下,性能一般
- 应用
- 基于大数据高性能的离线读写,并且使用SQL来开发
知识点05:Hive on Hbase 配置
- 需求
- 配置Hive与Hbase集成,实现Hive中可以读写Hbase表
- 分析
- step1:修改Hive配置文件,指定Hbase的Zookeeper地址
- step2:按顺序启动HDFS、ZK、Hbase、Hive
- 实现
- 全部操作在第三台机器
- 修改hive-site.xml:Hive通过SQL访问Hbase,就是Hbase的客户端,就要连接zookeeper
cd /export/server/hive-2.1.0-bin/ vim conf/hive-site.xml
<property> <name>hive.zookeeper.quorum</name> <value>node1,node2,node3</value> </property> <property> <name>hbase.zookeeper.quorum</name> <value>node1,node2,node3</value> </property> <property> <name>hive.server2.enable.doAs</name> <value>false</value> </property>
- 修改hive-env.sh
export HBASE_HOME=/export/server/hbase-2.1.0
- 启动HDFS、ZK、Hbase:第一台机器
start-dfs.sh /export/server/zookeeper-3.4.6/bin/start-zk-all.sh start-hbase.sh
- 启动Hive和YARN:第三台机器
#启动YARN start-yarn.sh #先启动metastore服务 start-metastore.sh #然后启动hiveserver start-hiveserver2.sh #然后启动beeline start-beeline.sh
- 总结
- 先配置Hive的配置文件:添加Hbase的地址
- 然后按照先后顺序启动即可
知识点06:Hive on Hbase 实现
- 需求
- 在Hive中实现对Hbase表的数据读写
- 分析
- step1:如果表在Hbase中没有,Hive中没有,在Hive中创建表,指定在Hbase中创建关联表
- 场景比较少
- 在Hive中建一张表,自动在Hbase中也创建一张对应的表
- step2:如果表在Hbase中有,但是Hive中没有,Hive中创建一张外部表,关联Hbase表
- 主要应用的方式
- Hbase中的表已经存在,已经有数据,构建一张Hive关联表,使用SQL进行查询
- 实现
- 第三台机器测试
- 如果Hbase中表不存在:【用的比较少】
- 创建测试数据文件
vim /export/data/hive-hbase.txt 1,zhangsan,80 2,lisi,60 3,wangwu,30 4,zhaoliu,70
- 创建测试表
--创建测试数据库 create database course; --切换数据库 use course; --创建原始数据表 create external table if not exists course.score( id int, cname string, score int ) row format delimited fields terminated by ',' stored as textfile ; --加载数据文件 load data local inpath '/export/data/hive-hbase.txt' into table score;
- 创建一张Hive与HBASE的映射表
create table course.hbase_score( id int, cname string, score int ) stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties("hbase.columns.mapping" = "cf:name,cf:score") tblproperties("hbase.table.name" = "hbase_score");
- 将测试表的数据写入映射表
set hive.exec.mode.local.auto=true; insert overwrite table course.hbase_score select id,cname,score from course.score;
- 如果Hbase中表已存在,只能创建外部表
create external table course.t1( key string, name string, age string, addr string, phone string ) stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties("hbase.columns.mapping" = ":key,basic:name,basic:age,other:addr,other:phone") tblproperties("hbase.table.name" = "itcast:t1");
- [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gNcVCpdb-1616545523608)(20210323_分布式NoSQL列存储数据库Hbase(六).assets/image-20210323160741636.png)]
- 总结
- Hive中的只是关联表,并没有数据,数据存储在Hbase表中
- 在Hive中创建Hbase的关联表,关联成功后,使用SQL处理关联表
- 如果Hbase中表不存在,默认使用Hive的第一列作为rowkey
- 如果Hbase中表已存在,只能建外部表,使用:key来表示rowkey
- HIve中与Hbase关联的表,不能使用load加载,只能使用insert,通过MR读写数据
知识点07:二级索引问题
- 问题
- Hbase使用Rowkey作为唯一索引,需要构建二级索引来解决查询问题,如何构建二级索引以及维护索引表?
- 分析
- step1:基于存储和常用查询需求,构建数据表
- step2:基于其他查询需求,构建索引表
- step3:先查询索引表,再查询数据表
- step4:自动维护索引表与原始数据表的数据一致性
- 实现
- 构建数据表
rowkey:name_id id name age sex addr zhangsan_001 001 zhangsan 18 male shanghai lisi_002 002 lisi 18 female beijing zhangsan_003 003 zhangsan 20 male ……
- 构建索引表
rowkey:id_name col:原始数据表的rowkey 001_zhangsan zhangsan_001 002_lisi lisi_002 003_zhangsan zhangsan_003 ……
- 查询:根据id查询
- 先查询索引表,获取原表的Rowkey
- 再根据原表Rowkey查询原表的数据
- 维护
- 当原表数据需要进行增删改时,索引表自动进行同步增删改对应的数据,保持一致性
- 解决方案
- 方案一:客户端操作实现
put1 put2 table1.put(put1) table2.put(put2)
- 方案二:协处理器实现
- 自己开发代码
- 让Hbase监听原表,原表更改一条,Hbase自动对索引表更改一条
- 缺点:开发比较麻烦
- 方案三:第三方工具
- Phoenix:将所有协处理器都封装好了
- 支持SQL
- 支持自动二级索引的构建及维护
create index
- 总结
- 需求:必须根据不同的查询条件,创建不同的索引表,并且维护所有索引表与原始数据表的同步
- 解决:通过Phoenix自带的协处理器来实现
知识点08:Phoenix的介绍
- 功能
- 专门基于Hbase所设计的SQL on Hbase 工具
- 使用Phoenix实现基于SQL操作Hbase
- 使用Phoenix自动构建二级索引并维护二级索引
- 原理
- 上层提供了SQL接口
- 底层全部通过Hbase Java API来实现,通过构建一系列的Scan和Put来实现数据的读写
- 功能非常丰富
- 底层封装了大量的内置的协处理器,可以实现各种复杂的处理需求,例如二级索引等
- 特点
- 优点
- 支持SQL接口
- 支持自动维护二级索引
- 缺点
- SQL支持的语法不全面
- Bug比较多
- Hive on Hbase对比
- Hive:SQL更加全面,但是不支持二级索引,底层通过分布式计算工具来实现
- Phoenix:SQL相对支持不全面,但是性能比较好,直接使用HbaseAPI,支持索引实现
- 应用
- Phoenix适用于任何需要使用SQL或者JDBC来快速的读写Hbase的场景
- 或者需要构建及维护二级索引场景
知识点09:Phoenix的安装配置
- 需求
- http://phoenix.apache.org/
- 安装部署配置Phoenix,集成Hbase
- 分析
- step1:上传解压安装
- step2:修改配置,指定Hbase连接地址
- step3:启动Phoenix,连接Hbase
- 实现
- 下载:http://phoenix.apache.org/download.html
- 第一台机器上传
cd /export/software/ rz
- 第一台机器解压
tar -zxvf apache-phoenix-5.0.0-HBase-2.0-bin.tar.gz -C /export/server/ cd /export/server/ mv apache-phoenix-5.0.0-HBase-2.0-bin phoenix-5.0.0-HBase-2.0-bin
- 修改三台Linux文件句柄数
vim /etc/security/limits.conf #在文件的末尾添加以下内容,*号不能去掉 * soft nofile 65536 * hard nofile 131072 * soft nproc 2048 * hard nproc 4096
- 将Phoenix所有jar包分发到Hbase的lib目录下
#拷贝到第一台机器 cd /export/server/phoenix-5.0.0-HBase-2.0-bin/ cp phoenix-* /export/server/hbase-2.1.0/lib/ cd /export/server/hbase-2.1.0/lib/ #分发给第二台和第三台 scp phoenix-* node2:$PWD scp phoenix-* node3:$PWD
- 修改hbase-site.xml,添加一下属性
cd /export/server/hbase-2.1.0/conf/ vim hbase-site.xml
<!-- 关闭流检查,从2.x开始使用async --> <property> <name>hbase.unsafe.stream.capability.enforce</name> <value>false</value> </property> <!-- 支持HBase命名空间映射 --> <property> <name>phoenix.schema.isNamespaceMappingEnabled</name> <value>true</value> </property> <!-- 支持索引预写日志编码 --> <property> <name>hbase.regionserver.wal.codec</name> <value>org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec</value> </property> <!-- 配置NS映射 --> <property> <name>phoenix.schema.isNamespaceMappingEnabled</name> <value>true</value> </property>
- 同步给其他两台机器
scp hbase-site.xml node2:$PWD scp hbase-site.xml node3:$PWD
- 同步给Phoenix
cp hbase-site.xml /export/server/phoenix-5.0.0-HBase-2.0-bin/bin/
- 重启Hbase
stop-hbase.sh start-hbase.sh
- 启动Phoenix
cd /export/server/phoenix-5.0.0-HBase-2.0-bin/ bin/sqlline.py node1:2181
- 测试
!tables
- [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KuMfWPJo-1616545523612)(20210323_分布式NoSQL列存储数据库Hbase(六).assets/image-20210323170434725.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nqVKRbLI-1616545523613)(20210323_分布式NoSQL列存储数据库Hbase(六).assets/image-20210323170543556.png)]
- 总结
- 解压安装
- 修改配置
- 启动服务
- 测试环境
知识点10:Phoenix的语法:DDL:NS
- 实现基于SQL的数据库管理:创建、切换、删除
- 分析
- step1:创建Namespace
- step2:切换Namespace
- step3:删除Namespace
- 实现
- 创建NS
create schema if not exists student;
- 切换NS
use student;
- 删除NS
drop schema if exists student;
- 总结
- 基本与SQL语法一致
- 注意:Phoenix中默认会将所有字符转换为大写,如果想要使用小写字母,必须加上双引号
知识点11:Phoenix的语法:DDL:Table
- 需求
- 实现基于SQL的数据表管理:创建、列举、查看、删除
- 分析
- step1:列举当前所有的表
- step2:创建表
- step3:查询表信息
- step4:删除表
- 实现
- 列举
!tables
- 创建
CREATE TABLE my_schema.my_table ( id BIGINT not null primary key, date Date ); CREATE TABLE my_table ( id INTEGER not null primary key desc, m.date DATE not null, m.db_utilization DECIMAL, i.db_utilization ) m.VERSIONS='3'; CREATE TABLE stats.prod_metrics ( host char(50) not null, created_date date not null, txn_count bigint CONSTRAINT pk PRIMARY KEY (host, created_date) ); CREATE TABLE IF NOT EXISTS "my_case_sensitive_table"( "id" char(10) not null primary key, "value" integer ) DATA_BLOCK_ENCODING='NONE',VERSIONS=5,MAX_FILESIZE=2000000 split on (?, ?, ?); CREATE TABLE IF NOT EXISTS my_schema.my_table ( org_id CHAR(15), entity_id CHAR(15), payload binary(1000), CONSTRAINT pk PRIMARY KEY (org_id, entity_id) ) TTL=86400
- 如果Hbase中没有这个表
use default; create table if not exists ORDER_DTL( ID varchar primary key, C1.STATUS varchar, C1.PAY_MONEY float, C1.PAYWAY integer, C1.USER_ID varchar, C1.OPERATION_DATE varchar, C1.CATEGORY varchar );
- 如果Hbase中已存在会自动关联
create table if not exists ORDER_INFO( "ROW" varchar primary key, "C1"."USER_ID" varchar, "C1"."OPERATION_DATE" varchar, "C1"."PAYWAY" varchar, "C1"."PAY_MONEY" varchar, "C1"."STATUS" varchar, "C1"."CATEGORY" varchar ) column_encoded_bytes=0 ;
- 查看
!desc order_info;
- 删除
drop table if exists order_dtl;
- 总结
- 创建表时,必须指定主键作为Rowkey,主键列不能加列族
- create table if not exists ORDER_INFO(
–不能这么写
“C1”.“ROW” varchar primary key, “C1”.“USER_ID” varchar, “C1”.“OPERATION_DATE” varchar, “C1”.“PAYWAY” varchar, “C1”.“PAY_MONEY” varchar, “C1”.“STATUS” varchar, “C1”.“CATEGORY” varchar ) column_encoded_bytes=0 ;
- Phoenix 4.8版本之前只要创建同名的Hbase表,会自动关联数据 - Phoenix 4.8版本以后,不推荐关联表的方式 - 推荐使用视图关联的方式来实现,如果你要使用关联表的方式,必须加上以下参数 ``` column_encoded_bytes=0 ; ``` - 如果关联已存在的表,Rowkey字段叫做ROW,使用时必须加上双引号
- select “ROW”,“C1”.USER_ID,“C1”.“PAYWAY” from ORDER_INFO;
知识点12:Phoenix的语法:DML:upsert
列名 | 数值 | 描述 |
Rowkey | 02602f66-adc7-40d4-8485-76b5632b5b53 | 行健,编码生成 |
USER_ID | 4944191 | 用户id |
OPERATION_DATE | 2020-04-25 12:09:16 | 操作时间 |
PAYWAY | 1 | 支付方式 |
PAY_MONEY | 4070 | 支付金额 |
STATUS | 已提交 | 提交状态 |
CATEGORY | 手机; | 分类 |
- 需求
- 基于order_info订单数据实现DML插入数据
- 分析
- Phoenix中插入更新的命令为:upsert
- 功能:insert + update
- MySQL:replace
- 如果存在就更新,如果不存在就插入
- 语法及示例
UPSERT INTO TEST VALUES('foo','bar',3); UPSERT INTO TEST(NAME,ID) VALUES('foo',123); UPSERT INTO TEST(ID, COUNTER) VALUES(123, 0) ON DUPLICATE KEY UPDATE COUNTER = COUNTER + 1; UPSERT INTO TEST(ID, MY_COL) VALUES(123, 0) ON DUPLICATE KEY IGNORE;
- 实现
- 插入一条数据
upsert into order_info values('z8f3ca6f-2f5c-44fd-9755-1792de183845','4944191','2020-04-25 12:09:16','1','4070','未提交','电脑');
- 更新USERID为123456
upsert into order_info("ROW","USER_ID") values('z8f3ca6f-2f5c-44fd-9755-1792de183845','123456');
- 总结
- 语法类似于insert语法
- 功能:insert + update
知识点13:Phoenix的语法:DML:delete
- 需求
- 基于order_info订单数据实现DML删除数据
- 分析
- Phoenix中插入更新的命令为:delete
- 语法及示例
DELETE FROM TEST; DELETE FROM TEST WHERE ID=123; DELETE FROM TEST WHERE NAME LIKE 'foo%';
- 实现
- 删除USER_ID为123456的rowkey数据
delete from order_info where USER_ID = '123456';
- 总结
- 与MySQL是一致的
知识点14:Phoenix的语法:DQL:select
- 需求
- 基于order_info订单数据实现DQL查询数据
- 分析
- Phoenix中插入更新的命令为:select
- 语法及示例
SELECT * FROM TEST LIMIT 1000; SELECT * FROM TEST LIMIT 1000 OFFSET 100; SELECT full_name FROM SALES_PERSON WHERE ranking >= 5.0 UNION ALL SELECT reviewer_name FROM CUSTOMER_REVIEW WHERE score >= 8.0
- 实现
- 查询支付方式为1的数据
select "ROW",payway,pay_money,category from order_info where payway = '1';
- 查询每种支付方式对应的用户人数,并且按照用户人数降序排序
- 分组:每、各个、不同
- 排序:用户人数
select payway, count(distinct user_id) as numb from order_info group by payway order by numb desc;
- 查询数据的第60行到66行
--以前的写法:limit M,N --M:开始位置 --N:显示的条数 --Phoenix的写法:limit N offset M select * from order_info limit 6 offset 60;//总共66行,显示最后6行
- 函数支持
- 总结
- 基本查询与MySQL也是一致的
- 写的时候注意数据类型以及大小写的问题即可
- 如果遇到SQL报错,检查语法是否支持
知识点15:Phoenix的使用:预分区
- 需求
- Hbase命令建表
create Ns;tbname,列族,预分区
- 创建表的时候,需要根据Rowkey来设计多个分区
- 分析
- Phoenix也提供了创建表时,指定分区范围的语法
CREATE TABLE IF NOT EXISTS "my_case_sensitive_table"( "id" char(10) not null primary key, "value" integer ) DATA_BLOCK_ENCODING='NONE',VERSIONS=5,MAX_FILESIZE=2000000 split on (?, ?, ?)
- 实现
- 创建数据表,四个分区
drop table if exists ORDER_DTL; create table if not exists ORDER_DTL( "id" varchar primary key, C1."status" varchar, C1."money" float, C1."pay_way" integer, C1."user_id" varchar, C1."operation_time" varchar, C1."category" varchar ) CONPRESSION='GZ' SPLIT ON ('3','5','7');
- 插入数据
UPSERT INTO "ORDER_DTL" VALUES('02602f66-adc7-40d4-8485-76b5632b5b53','已提交',4070,1,'4944191','2020-04-25 12:09:16','手机;'); UPSERT INTO "ORDER_DTL" VALUES('0968a418-f2bc-49b4-b9a9-2157cf214cfd','已完成',4350,1,'1625615','2020-04-25 12:09:37','家用电器;;电脑;'); UPSERT INTO "ORDER_DTL" VALUES('0e01edba-5e55-425e-837a-7efb91c56630','已提交',6370,3,'3919700','2020-04-25 12:09:39','男装;男鞋;'); UPSERT INTO "ORDER_DTL" VALUES('0f46d542-34cb-4ef4-b7fe-6dcfa5f14751','已付款',9380,1,'2993700','2020-04-25 12:09:46','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('1fb7c50f-9e26-4aa8-a140-a03d0de78729','已完成',6400,2,'5037058','2020-04-25 12:10:13','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('23275016-996b-420c-8edc-3e3b41de1aee','已付款',280,1,'3018827','2020-04-25 12:09:53','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('2375a7cf-c206-4ac0-8de4-863e7ffae27b','已完成',5600,1,'6489579','2020-04-25 12:08:55','食品;家用电器;'); UPSERT INTO "ORDER_DTL" VALUES('269fe10c-740b-4fdb-ad25-7939094073de','已提交',8340,2,'2948003','2020-04-25 12:09:26','男装;男鞋;'); UPSERT INTO "ORDER_DTL" VALUES('2849fa34-6513-44d6-8f66-97bccb3a31a1','已提交',7060,2,'2092774','2020-04-25 12:09:38','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('28b7e793-6d14-455b-91b3-0bd8b23b610c','已提交',640,3,'7152356','2020-04-25 12:09:49','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('2909b28a-5085-4f1d-b01e-a34fbaf6ce37','已提交',9390,3,'8237476','2020-04-25 12:10:08','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('2a01dfe5-f5dc-4140-b31b-a6ee27a6e51e','已提交',7490,2,'7813118','2020-04-25 12:09:05','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('2b86ab90-3180-4940-b624-c936a1e7568d','已付款',5360,2,'5301038','2020-04-25 12:08:50','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('2e19fbe8-7970-4d62-8e8f-d364afc2dd41','已付款',6490,0,'3141181','2020-04-25 12:09:22','食品;家用电器;'); UPSERT INTO "ORDER_DTL" VALUES('2fc28d36-dca0-49e8-bad0-42d0602bdb40','已付款',3820,1,'9054826','2020-04-25 12:10:04','家用电器;;电脑;'); UPSERT INTO "ORDER_DTL" VALUES('31477850-8b15-4f1b-9ec3-939f7dc47241','已提交',4650,2,'5837271','2020-04-25 12:08:52','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('39319322-2d80-41e7-a862-8b8858e63316','已提交',5000,1,'5686435','2020-04-25 12:08:51','家用电器;;电脑;'); UPSERT INTO "ORDER_DTL" VALUES('3d2254bd-c25a-404f-8e42-2faa4929a629','已完成',5000,1,'1274270','2020-04-25 12:08:43','男装;男鞋;'); UPSERT INTO "ORDER_DTL" VALUES('42f7fe21-55a3-416f-9535-baa222cc0098','已完成',3600,2,'2661641','2020-04-25 12:09:58','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('44231dbb-9e58-4f1a-8c83-be1aa814be83','已提交',3950,1,'3855371','2020-04-25 12:08:39','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('526e33d2-a095-4e19-b759-0017b13666ca','已完成',3280,0,'5553283','2020-04-25 12:09:01','食品;家用电器;'); UPSERT INTO "ORDER_DTL" VALUES('5a6932f4-b4a4-4a1a-b082-2475d13f9240','已提交',50,2,'1764961','2020-04-25 12:10:07','家用电器;;电脑;'); UPSERT INTO "ORDER_DTL" VALUES('5fc0093c-59a3-417b-a9ff-104b9789b530','已提交',6310,2,'1292805','2020-04-25 12:09:36','男装;男鞋;'); UPSERT INTO "ORDER_DTL" VALUES('605c6dd8-123b-4088-a047-e9f377fcd866','已完成',8980,2,'6202324','2020-04-25 12:09:54','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('613cfd50-55c7-44d2-bb67-995f72c488ea','已完成',6830,3,'6977236','2020-04-25 12:10:06','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('62246ac1-3dcb-4f2c-8943-800c9216c29f','已提交',8610,1,'5264116','2020-04-25 12:09:14','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('625c7fef-de87-428a-b581-a63c71059b14','已提交',5970,0,'8051757','2020-04-25 12:09:07','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('6d43c490-58ab-4e23-b399-dda862e06481','已提交',4570,0,'5514248','2020-04-25 12:09:34','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('70fa0ae0-6c02-4cfa-91a9-6ad929fe6b1b','已付款',4100,1,'8598963','2020-04-25 12:09:08','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('7170ce71-1fc0-4b6e-a339-67f525536dcd','已完成',9740,1,'4816392','2020-04-25 12:09:51','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('71961b06-290b-457d-bbe0-86acb013b0e3','已完成',6550,3,'2393699','2020-04-25 12:08:49','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('72dc148e-ce64-432d-b99f-61c389cb82cd','已提交',4090,1,'2536942','2020-04-25 12:10:12','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('7c0c1668-b783-413f-afc4-678a5a6d1033','已完成',3850,3,'6803936','2020-04-25 12:09:20','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('7fa02f7a-10df-4247-9935-94c8b7d4dbc0','已提交',1060,0,'6119810','2020-04-25 12:09:21','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('820c5e83-f2e0-42d4-b5f0-83802c75addc','已付款',9270,2,'5818454','2020-04-25 12:10:09','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('83ed55ec-a439-44e0-8fe0-acb7703fb691','已完成',8380,2,'6804703','2020-04-25 12:09:52','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('85287268-f139-4d59-8087-23fa6454de9d','已取消',9750,1,'4382852','2020-04-25 12:10:00','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('8d32669e-327a-4802-89f4-2e91303aee59','已提交',9390,1,'4182962','2020-04-25 12:09:57','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('8dadc2e4-63f1-490f-9182-793be64fed76','已付款',9350,1,'5937549','2020-04-25 12:09:02','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('94ad8ee0-8898-442c-8cb1-083a4b609616','已提交',4370,0,'4666456','2020-04-25 12:09:13','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('994cbb44-f0ee-45ff-a4f4-76c87bc2b972','已付款',3190,3,'3200759','2020-04-25 12:09:25','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('9ff3032c-8679-4247-9e6f-4caf2dc93aff','已提交',850,0,'8835231','2020-04-25 12:09:40','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('9ff3032c-8679-4247-9e6f-4caf2dc93aff','已付款',850,0,'8835231','2020-04-25 12:09:45','食品;家用电器;'); UPSERT INTO "ORDER_DTL" VALUES('a467ba42-f91e-48a0-865e-1703aaa45e0e','已提交',8040,0,'8206022','2020-04-25 12:09:50','家用电器;;电脑;'); UPSERT INTO "ORDER_DTL" VALUES('a5302f47-96d9-41b4-a14c-c7a508f59282','已付款',8570,2,'5319315','2020-04-25 12:08:44','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('a5b57bec-6235-45f4-bd7e-6deb5cd1e008','已提交',5700,3,'6486444','2020-04-25 12:09:27','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('ae5c3363-cf8f-48a9-9676-701a7b0a7ca5','已付款',7460,1,'2379296','2020-04-25 12:09:23','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('b1fb2399-7cf2-4af5-960a-a4d77f4803b8','已提交',2690,3,'6686018','2020-04-25 12:09:55','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('b21c7dbd-dabd-4610-94b9-d7039866a8eb','已提交',6310,2,'1552851','2020-04-25 12:09:15','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('b4bfd4b7-51f5-480e-9e23-8b1579e36248','已提交',4000,1,'3260372','2020-04-25 12:09:35','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('b63983cc-2b59-4992-84c6-9810526d0282','已提交',7370,3,'3107867','2020-04-25 12:08:45','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('bf60b752-1ccc-43bf-9bc3-b2aeccacc0ed','已提交',720,2,'5034117','2020-04-25 12:09:03','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('c808addc-8b8b-4d89-99b1-db2ed52e61b4','已提交',3630,1,'6435854','2020-04-25 12:09:10','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('cc9dbd20-cf9f-4097-ae8b-4e73db1e4ba1','已付款',5000,0,'2007322','2020-04-25 12:08:38','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('ccceaf57-a5ab-44df-834a-e7b32c63efc1','已提交',2660,2,'7928516','2020-04-25 12:09:42','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('d7be5c39-e07c-40e8-bf09-4922fbc6335c','已付款',8750,2,'1250995','2020-04-25 12:09:09','食品;家用电器;'); UPSERT INTO "ORDER_DTL" VALUES('dfe16df7-4a46-4b6f-9c6d-083ec215218e','已完成',410,0,'1923817','2020-04-25 12:09:56','家用电器;;电脑;'); UPSERT INTO "ORDER_DTL" VALUES('e1241ad4-c9c1-4c17-93b9-ef2c26e7f2b2','已付款',6760,0,'2457464','2020-04-25 12:08:54','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('e180a9f2-9f80-4b6d-99c8-452d6c037fc7','已完成',8120,2,'7645270','2020-04-25 12:09:32','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('e4418843-9ac0-47a7-bfd8-d61c4d296933','已付款',8170,2,'7695668','2020-04-25 12:09:11','家用电器;;电脑;'); UPSERT INTO "ORDER_DTL" VALUES('e8b3bb37-1019-4492-93c7-305177271a71','已完成',2560,2,'4405460','2020-04-25 12:10:05','男装;男鞋;'); UPSERT INTO "ORDER_DTL" VALUES('eb1a1a22-953a-42f1-b594-f5dfc8fb6262','已完成',2370,2,'8233485','2020-04-25 12:09:24','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('ecfd18f5-45f2-4dcd-9c47-f2ad9b216bd0','已付款',8070,3,'6387107','2020-04-25 12:09:04','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('f1226752-7be3-4702-a496-3ddba56f66ec','已付款',4410,3,'1981968','2020-04-25 12:10:10','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('f642b16b-eade-4169-9eeb-4d5f294ec594','已提交',4010,1,'6463215','2020-04-25 12:09:29','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('f8f3ca6f-2f5c-44fd-9755-1792de183845','已付款',5950,3,'4060214','2020-04-25 12:09:12','机票;文娱;');
- 查看分区请求
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9PGuMMNL-1616545523616)(20210323_分布式NoSQL列存储数据库Hbase(六).assets/image-20210323175607500.png)]
- 总结
- 实现效果与命令实现的效果一致
- 通过SQL建表语句实现
create table() split
知识点16:Phoenix的使用:加盐salt
- 需求
- Rowkey设计的时候为了避免连续,构建Rowkey的散列,如果rowkey设计是连续的,怎么解决?
- 分析
- 在Phoenix创建一张盐表,写入的数据会自动进行编码写入不同的分区中
CREATE TABLE table ( a_key VARCHAR PRIMARY KEY, a_col VARCHAR ) SALT_BUCKETS = 20;
- 实现
- 创建一张盐表,指定分区个数为10
drop table if exists ORDER_DTL; create table if not exists ORDER_DTL( "id" varchar primary key, C1."status" varchar, C1."money" float, C1."pay_way" integer, C1."user_id" varchar, C1."operation_time" varchar, C1."category" varchar ) CONPRESSION='GZ', SALT_BUCKETS=10;
- [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DWu5bCTo-1616545523618)(20210323_分布式NoSQL列存储数据库Hbase(六).assets/image-20210323180045755.png)]
- 写入数据
UPSERT INTO "ORDER_DTL" VALUES('02602f66-adc7-40d4-8485-76b5632b5b53','已提交',4070,1,'4944191','2020-04-25 12:09:16','手机;'); UPSERT INTO "ORDER_DTL" VALUES('0968a418-f2bc-49b4-b9a9-2157cf214cfd','已完成',4350,1,'1625615','2020-04-25 12:09:37','家用电器;;电脑;'); UPSERT INTO "ORDER_DTL" VALUES('0e01edba-5e55-425e-837a-7efb91c56630','已提交',6370,3,'3919700','2020-04-25 12:09:39','男装;男鞋;'); UPSERT INTO "ORDER_DTL" VALUES('0f46d542-34cb-4ef4-b7fe-6dcfa5f14751','已付款',9380,1,'2993700','2020-04-25 12:09:46','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('1fb7c50f-9e26-4aa8-a140-a03d0de78729','已完成',6400,2,'5037058','2020-04-25 12:10:13','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('23275016-996b-420c-8edc-3e3b41de1aee','已付款',280,1,'3018827','2020-04-25 12:09:53','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('2375a7cf-c206-4ac0-8de4-863e7ffae27b','已完成',5600,1,'6489579','2020-04-25 12:08:55','食品;家用电器;'); UPSERT INTO "ORDER_DTL" VALUES('269fe10c-740b-4fdb-ad25-7939094073de','已提交',8340,2,'2948003','2020-04-25 12:09:26','男装;男鞋;'); UPSERT INTO "ORDER_DTL" VALUES('2849fa34-6513-44d6-8f66-97bccb3a31a1','已提交',7060,2,'2092774','2020-04-25 12:09:38','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('28b7e793-6d14-455b-91b3-0bd8b23b610c','已提交',640,3,'7152356','2020-04-25 12:09:49','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('2909b28a-5085-4f1d-b01e-a34fbaf6ce37','已提交',9390,3,'8237476','2020-04-25 12:10:08','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('2a01dfe5-f5dc-4140-b31b-a6ee27a6e51e','已提交',7490,2,'7813118','2020-04-25 12:09:05','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('2b86ab90-3180-4940-b624-c936a1e7568d','已付款',5360,2,'5301038','2020-04-25 12:08:50','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('2e19fbe8-7970-4d62-8e8f-d364afc2dd41','已付款',6490,0,'3141181','2020-04-25 12:09:22','食品;家用电器;'); UPSERT INTO "ORDER_DTL" VALUES('2fc28d36-dca0-49e8-bad0-42d0602bdb40','已付款',3820,1,'9054826','2020-04-25 12:10:04','家用电器;;电脑;'); UPSERT INTO "ORDER_DTL" VALUES('31477850-8b15-4f1b-9ec3-939f7dc47241','已提交',4650,2,'5837271','2020-04-25 12:08:52','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('39319322-2d80-41e7-a862-8b8858e63316','已提交',5000,1,'5686435','2020-04-25 12:08:51','家用电器;;电脑;'); UPSERT INTO "ORDER_DTL" VALUES('3d2254bd-c25a-404f-8e42-2faa4929a629','已完成',5000,1,'1274270','2020-04-25 12:08:43','男装;男鞋;'); UPSERT INTO "ORDER_DTL" VALUES('42f7fe21-55a3-416f-9535-baa222cc0098','已完成',3600,2,'2661641','2020-04-25 12:09:58','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('44231dbb-9e58-4f1a-8c83-be1aa814be83','已提交',3950,1,'3855371','2020-04-25 12:08:39','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('526e33d2-a095-4e19-b759-0017b13666ca','已完成',3280,0,'5553283','2020-04-25 12:09:01','食品;家用电器;'); UPSERT INTO "ORDER_DTL" VALUES('5a6932f4-b4a4-4a1a-b082-2475d13f9240','已提交',50,2,'1764961','2020-04-25 12:10:07','家用电器;;电脑;'); UPSERT INTO "ORDER_DTL" VALUES('5fc0093c-59a3-417b-a9ff-104b9789b530','已提交',6310,2,'1292805','2020-04-25 12:09:36','男装;男鞋;'); UPSERT INTO "ORDER_DTL" VALUES('605c6dd8-123b-4088-a047-e9f377fcd866','已完成',8980,2,'6202324','2020-04-25 12:09:54','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('613cfd50-55c7-44d2-bb67-995f72c488ea','已完成',6830,3,'6977236','2020-04-25 12:10:06','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('62246ac1-3dcb-4f2c-8943-800c9216c29f','已提交',8610,1,'5264116','2020-04-25 12:09:14','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('625c7fef-de87-428a-b581-a63c71059b14','已提交',5970,0,'8051757','2020-04-25 12:09:07','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('6d43c490-58ab-4e23-b399-dda862e06481','已提交',4570,0,'5514248','2020-04-25 12:09:34','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('70fa0ae0-6c02-4cfa-91a9-6ad929fe6b1b','已付款',4100,1,'8598963','2020-04-25 12:09:08','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('7170ce71-1fc0-4b6e-a339-67f525536dcd','已完成',9740,1,'4816392','2020-04-25 12:09:51','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('71961b06-290b-457d-bbe0-86acb013b0e3','已完成',6550,3,'2393699','2020-04-25 12:08:49','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('72dc148e-ce64-432d-b99f-61c389cb82cd','已提交',4090,1,'2536942','2020-04-25 12:10:12','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('7c0c1668-b783-413f-afc4-678a5a6d1033','已完成',3850,3,'6803936','2020-04-25 12:09:20','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('7fa02f7a-10df-4247-9935-94c8b7d4dbc0','已提交',1060,0,'6119810','2020-04-25 12:09:21','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('820c5e83-f2e0-42d4-b5f0-83802c75addc','已付款',9270,2,'5818454','2020-04-25 12:10:09','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('83ed55ec-a439-44e0-8fe0-acb7703fb691','已完成',8380,2,'6804703','2020-04-25 12:09:52','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('85287268-f139-4d59-8087-23fa6454de9d','已取消',9750,1,'4382852','2020-04-25 12:10:00','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('8d32669e-327a-4802-89f4-2e91303aee59','已提交',9390,1,'4182962','2020-04-25 12:09:57','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('8dadc2e4-63f1-490f-9182-793be64fed76','已付款',9350,1,'5937549','2020-04-25 12:09:02','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('94ad8ee0-8898-442c-8cb1-083a4b609616','已提交',4370,0,'4666456','2020-04-25 12:09:13','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('994cbb44-f0ee-45ff-a4f4-76c87bc2b972','已付款',3190,3,'3200759','2020-04-25 12:09:25','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('9ff3032c-8679-4247-9e6f-4caf2dc93aff','已提交',850,0,'8835231','2020-04-25 12:09:40','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('9ff3032c-8679-4247-9e6f-4caf2dc93aff','已付款',850,0,'8835231','2020-04-25 12:09:45','食品;家用电器;'); UPSERT INTO "ORDER_DTL" VALUES('a467ba42-f91e-48a0-865e-1703aaa45e0e','已提交',8040,0,'8206022','2020-04-25 12:09:50','家用电器;;电脑;'); UPSERT INTO "ORDER_DTL" VALUES('a5302f47-96d9-41b4-a14c-c7a508f59282','已付款',8570,2,'5319315','2020-04-25 12:08:44','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('a5b57bec-6235-45f4-bd7e-6deb5cd1e008','已提交',5700,3,'6486444','2020-04-25 12:09:27','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('ae5c3363-cf8f-48a9-9676-701a7b0a7ca5','已付款',7460,1,'2379296','2020-04-25 12:09:23','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('b1fb2399-7cf2-4af5-960a-a4d77f4803b8','已提交',2690,3,'6686018','2020-04-25 12:09:55','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('b21c7dbd-dabd-4610-94b9-d7039866a8eb','已提交',6310,2,'1552851','2020-04-25 12:09:15','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('b4bfd4b7-51f5-480e-9e23-8b1579e36248','已提交',4000,1,'3260372','2020-04-25 12:09:35','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('b63983cc-2b59-4992-84c6-9810526d0282','已提交',7370,3,'3107867','2020-04-25 12:08:45','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('bf60b752-1ccc-43bf-9bc3-b2aeccacc0ed','已提交',720,2,'5034117','2020-04-25 12:09:03','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('c808addc-8b8b-4d89-99b1-db2ed52e61b4','已提交',3630,1,'6435854','2020-04-25 12:09:10','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('cc9dbd20-cf9f-4097-ae8b-4e73db1e4ba1','已付款',5000,0,'2007322','2020-04-25 12:08:38','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('ccceaf57-a5ab-44df-834a-e7b32c63efc1','已提交',2660,2,'7928516','2020-04-25 12:09:42','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('d7be5c39-e07c-40e8-bf09-4922fbc6335c','已付款',8750,2,'1250995','2020-04-25 12:09:09','食品;家用电器;'); UPSERT INTO "ORDER_DTL" VALUES('dfe16df7-4a46-4b6f-9c6d-083ec215218e','已完成',410,0,'1923817','2020-04-25 12:09:56','家用电器;;电脑;'); UPSERT INTO "ORDER_DTL" VALUES('e1241ad4-c9c1-4c17-93b9-ef2c26e7f2b2','已付款',6760,0,'2457464','2020-04-25 12:08:54','数码;女装;'); UPSERT INTO "ORDER_DTL" VALUES('e180a9f2-9f80-4b6d-99c8-452d6c037fc7','已完成',8120,2,'7645270','2020-04-25 12:09:32','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('e4418843-9ac0-47a7-bfd8-d61c4d296933','已付款',8170,2,'7695668','2020-04-25 12:09:11','家用电器;;电脑;'); UPSERT INTO "ORDER_DTL" VALUES('e8b3bb37-1019-4492-93c7-305177271a71','已完成',2560,2,'4405460','2020-04-25 12:10:05','男装;男鞋;'); UPSERT INTO "ORDER_DTL" VALUES('eb1a1a22-953a-42f1-b594-f5dfc8fb6262','已完成',2370,2,'8233485','2020-04-25 12:09:24','机票;文娱;'); UPSERT INTO "ORDER_DTL" VALUES('ecfd18f5-45f2-4dcd-9c47-f2ad9b216bd0','已付款',8070,3,'6387107','2020-04-25 12:09:04','酒店;旅游;'); UPSERT INTO "ORDER_DTL" VALUES('f1226752-7be3-4702-a496-3ddba56f66ec','已付款',4410,3,'1981968','2020-04-25 12:10:10','维修;手机;'); UPSERT INTO "ORDER_DTL" VALUES('f642b16b-eade-4169-9eeb-4d5f294ec594','已提交',4010,1,'6463215','2020-04-25 12:09:29','男鞋;汽车;'); UPSERT INTO "ORDER_DTL" VALUES('f8f3ca6f-2f5c-44fd-9755-1792de183845','已付款',5950,3,'4060214','2020-04-25 12:09:12','机票;文娱;');
- Phoenix中查看
select "id" from ORDER_DTL;
- Hbase中查看
scan 'ORDER_DTL'
- 总结
- 由Phoenix来实现自动编码,解决Rowkey的热点问题,不需要自己设计散列的Rowkey
知识点17:Phoenix的使用:视图
- 需求
- 直接关联Hbase中的表,会导致误删除,对数据的权限会有影响,容易出现问题,如何避免?
- 分析
- Phoenix中建议使用视图的方式来关联Hbase中已有的表
- 通过构建关联视图,可以解决大部分数据查询的数据,不影响数据
- 视图:理解为只读的表
- 实现
- 创建视图,关联Hbase中已经存在的表
create view if not exists "MOMO_CHAT"."MSG" ( "pk" varchar primary key, -- 指定ROWKEY映射到主键 "C1"."msg_time" varchar, "C1"."sender_nickyname" varchar, "C1"."sender_account" varchar, "C1"."sender_sex" varchar, "C1"."sender_ip" varchar, "C1"."sender_os" varchar, "C1"."sender_phone_type" varchar, "C1"."sender_network" varchar, "C1"."sender_gps" varchar, "C1"."receiver_nickyname" varchar, "C1"."receiver_ip" varchar, "C1"."receiver_account" varchar, "C1"."receiver_os" varchar, "C1"."receiver_phone_type" varchar, "C1"."receiver_network" varchar, "C1"."receiver_gps" varchar, "C1"."receiver_sex" varchar, "C1"."msg_type" varchar, "C1"."distance" varchar );
- 查询数据
select "pk", "C1"."msg_time", "C1"."sender_account", "C1"."receiver_account" from "MOMO_CHAT"."MSG" limit 10;
- 总结
- 工作中主要构建的都是视图
- MySQL:视图
- Hive:外部表
- Phoenix:视图
知识点18:Phoenix的使用:JDBC
- 需求
- 工作中实际使用SQL,会基于程序中使用JDBC的方式来提交SQL语句,在Phoenix中如何实现?
- 分析
- Phoenix支持使用JDBC的方式来提交SQL语句
- 例如:聊天分析案例中需求:查询条件为日期【年-月-日】 + 发送人ID + 接受人ID
select * from "MOMO_CHAT"."MSG" where substr("msg_time",0,10) = '2021-03-22' and "sender_account" = '17351912952' and "receiver_account" = '17742251415';
- 可以在代码中基于JDBC来提交SQL查询
- 实现
- 构建JDBC连接Phoenix
package cn.itcast.momo_chat.service.impl; import cn.itcast.momo_chat.entity.Msg; import cn.itcast.momo_chat.service.ChatMessageService; import org.apache.phoenix.jdbc.PhoenixDriver; import java.sql.*; import java.util.ArrayList; import java.util.List; /** * @ClassName PhoenixChatMessageService * @Description TODO JDBC连接Phoenix实现数据查询 * @Create By Frank */ public class PhoenixChatMessageService implements ChatMessageService { private Connection connection; public PhoenixChatMessageService() throws ClassNotFoundException, SQLException { try { //申明驱动类 Class.forName(PhoenixDriver.class.getName()); // System.out.println(PhoenixDriver.class.getName()); //构建连接 connection = DriverManager.getConnection("jdbc:phoenix:node1,node2,node3:2181"); } catch (ClassNotFoundException e) { throw new RuntimeException("加载Phoenix驱动失败!"); } catch (SQLException e) { throw new RuntimeException("获取Phoenix JDBC连接失败!"); } } @Override public List<Msg> getMessage(String date, String sender, String receiver) throws Exception { PreparedStatement ps = connection.prepareStatement( "SELECT * FROM MOMO_CHAT.MSG T WHERE substr(\"msg_time\", 0, 10) = ? " + "AND T.\"sender_account\" = ? " + "AND T.\"receiver_account\" = ? "); ps.setString(1, date); ps.setString(2, sender); ps.setString(3, receiver); ResultSet rs = ps.executeQuery(); List<Msg> msgList = new ArrayList<>(); while(rs.next()) { Msg msg = new Msg(); msg.setMsg_time(rs.getString("msg_time")); msg.setSender_nickyname(rs.getString("sender_nickyname")); msg.setSender_account(rs.getString("sender_account")); msg.setSender_sex(rs.getString("sender_sex")); msg.setSender_ip(rs.getString("sender_ip")); msg.setSender_os(rs.getString("sender_os")); msg.setSender_phone_type(rs.getString("sender_phone_type")); msg.setSender_network(rs.getString("sender_network")); msg.setSender_gps(rs.getString("sender_gps")); msg.setReceiver_nickyname(rs.getString("receiver_nickyname")); msg.setReceiver_ip(rs.getString("receiver_ip")); msg.setReceiver_account(rs.getString("receiver_account")); msg.setReceiver_os(rs.getString("receiver_os")); msg.setReceiver_phone_type(rs.getString("receiver_phone_type")); msg.setReceiver_network(rs.getString("receiver_network")); msg.setReceiver_gps(rs.getString("receiver_gps")); msg.setReceiver_sex(rs.getString("receiver_sex")); msg.setMsg_type(rs.getString("msg_type")); msg.setDistance(rs.getString("distance")); msgList.add(msg); } return msgList; } @Override public void close() { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { ChatMessageService chatMessageService = new PhoenixChatMessageService(); List<Msg> message = chatMessageService.getMessage("2021-03-22", "17351912952", "17742251415"); for (Msg msg : message) { System.out.println(msg); } chatMessageService.close(); } }
- 运行查看结果
- 总结
- Phoenix支持SQL
- 支持JDBC方式提交SQL语句实现数据处理