建一个商城型网站,海拉尔做网站的公司,如何免费注册一个网站,毕业设计做系统跟做网站哪个容易1. hbase-phoenix的应用
1.1 概述#xff1a;
上面我们学会了hbase的操作和原理#xff0c;以及外部集成的mr的计算方式#xff0c;但是我们在使用hbase的时候#xff0c;有的时候我们要直接操作hbase做部分数据的查询和插入#xff0c;这种原生的方式操作在工作过程中还…1. hbase-phoenix的应用
1.1 概述
上面我们学会了hbase的操作和原理以及外部集成的mr的计算方式但是我们在使用hbase的时候有的时候我们要直接操作hbase做部分数据的查询和插入这种原生的方式操作在工作过程中还是比较常见的以上这些方式需要使用外部的框架进行协助处理其实hbase也对外提供了一个直接的操作方式接口插件Phoenix它和mr不一样是直接集成在hbase之中的通过一个工具使得hbase可以完全支持sql操作其实我们可以将Phoenix当成是一个sql插件一个可以写sql完成hbase操作的插件并且在hbase中通过regionserver直接执行还可以做sql的优化是hbase免费开源出来的一个插件。 安装过程及配置环境变量过程略。
1.2 创建测试表
在phoenix中创建测试表必须指定主键主键对应hbase的rowkey唯一且非空。
-- 表名不带双引号默认转成大写
create table phtest1(pk varchar not null primary key,col1 varchar,col2 varchar,col3 varchar
);
-- 表名带双引号不转大写
create table phtest2(pk varchar not null primary key,col1 varchar,col2 varchar,col3 varchar
);-- 查看表列表
!tables-- 查看表结构
!describe PHTEST1; 在hbase shell中查询(phoenix严格区分大小写所有小写在phoenix中都会被翻译为大写)。
规则就是如果表名没用双引号括起来小写全会被翻译为大写如果表用双引号括起来了那么小写的话就用小写大写就用大写。
1.3 插入/查询数据
0: jdbc:phoenix:hadoop106,hadoop107:2181 upsert into PHTEST1 values(x0001,1,2,3
. . . . . . . . . . . . . . . . . . . .) );
1 row affected (0.299 seconds)
0: jdbc:phoenix:hadoop106,hadoop107:2181 upsert into PHTEST1 values (x0001,1,22,3);
1 row affected (0.024 seconds)
0: jdbc:phoenix:hadoop106,hadoop107:2181 upsert into PHTEST1 values (x0002,1,2,3);
1 row affected (0.02 seconds)
0: jdbc:phoenix:hadoop106,hadoop107:2181 select * from PHTEST1;
-------------------------
| PK | COL1 | COL2 | COL3 |
-------------------------
| x0001 | 1 | 22 | 3 |
| x0002 | 1 | 2 | 3 |
-------------------------
2 rows selected (0.103 seconds)在hbase shell中查询 1.4 测试删除
插入多行删除其中某一行
-- 插入多行一次只能插入一行不能插入多行
upsert into PHTEST1 values (x0002,2,3,4);
upsert into PHTEST1 values (x0003,3,4,5);
upsert into PHTEST1 values (x0004,4,5,6);
-- 查询验证
select * from PHTEST1;
-- 删除一行
delete from PHTEST1 where col12;
-- 查询验证
select * from PHTEST1; 1.5 查询导入
-- 使用select查询结果集批量更新表
-- 创建一张临时表PHTEST2
create table PHTEST2(pk varchar not null primary key,col1 varchar, col2 varchar,col3 varchar
);
-- 临时表插入数据,比phtest1表多了x0005、x0006和x0002三行其中x0003、x0004与phtest1的一致
upsert into PHTEST2 values (x0001,newvalue,newvalue,newvalue);
upsert into PHTEST2 values (x0002,newvalue,newvalue,newvalue);
upsert into PHTEST2 values (x0003,3,4,5);
upsert into PHTEST2 values (x0004,4,5,6);
upsert into PHTEST2 values (x0005,newvalue,newvalue,newvalue);
upsert into PHTEST2 values (x0006,newvalue,newvalue,newvalue);-- 执行批量更新, 将PHTEST2表的数据覆盖到PHTEST1表
upsert into PHTEST1 select * from PHTEST2;
1.6 删除表
drop table PHTEST2;
1.7 数据导入
使用官方提供的数据样例phoenix数据导入只支持csv文件格式。
# 在客户端外
# 执行SQL文件
# 对标hive的-f test.sql ${hiveconf:batch_date}
# 创建sql文件 select * from PHTEST1
sqlline.py nn1:2181 /root/sql# 创建表
create table user(id varchar primary key,name varchar,age varchar);
# 创建csv文件 /root/user.csv
# 输入文件内容
# 1zhangsan,20
# 2,lisi,30psql.py -t USER nn1:2181 /root/user.csv
# 注意
# 1phoenix数据导入只支持后缀为.csv的文件, csv文件名称不需要和表名称一致文件名可以小写
# 2指定的表必须是大写小写就报错
1.8 在phoenix建表时指定列族
-- 用 列族名.字段名
create table cftest (pk varchar not null primary key,cf1.col1 varchar,cf2.col2 varchar);-- 查询时可以不用列族
select col1 from cftest
注如果建表时这些列未指定列族则会分配一个叫0的列族。 1.9 在phoenix建表时指定压缩格式
-- 在后面可指定压缩格式
create table comptest (pk varchar not null primary key,cf1.col1 varchar,cf2.col2 varchar) compressionsnappy;
1.10 在phoenix建表时预分region
-- 用 split on (x0001,x0002,x0003,x0004,x0005) 来进行预分region
-- 其中 on 里面的 是 splitkey
create table split_region_test (pk varchar not null primary key,cf1.col1 varchar,cf2.col2 varchar) compressionsnappy split on (x0001,x0002,x0003,x0004,x0005);
1. 11 phoenix与hbase表关联
1在hbase中创建带有命名空间的表并添加数据
create hainiu:relatetable_1,{NAME cf1,COMPRESSION snappy},{NAME cf2,COMPRESSION snappy}# 添加数据
put hainiu:relatetable_1,x0001,cf1:name,user1
put hainiu:relatetable_1,x0002,cf1:name,user2
put hainiu:relatetable_1,x0001,cf1:age,20
put hainiu:relatetable_1,x0002,cf1:age,21
put hainiu:relatetable_1,x0001,cf2:address,beijing
put hainiu:relatetable_1,x0002,cf2:address,shanghai
2在phoenix中创建schemaschema相当于命名空间
-- 先在phoenix中创建schema对应hbase的namespace
create schema if not exists hainiu;
执行报错
cannot create scheme because config phoenix.scheme.isNamespaceMappingEnabled for enabling name space mapping isnt enabled.schemaNamehainiu
在phoenix中创建schema报错解决方式在hbase的hbase-site.xml中添加phoenix.schema.isNamespaceMappingEnabledtrue和phoenix.schema.mapSystemTablesToNamespacetrue
在hbase和Phoenix的配置文件hbase-site.xml中都要增加这个配置
增加以上配置
重启hbase集群
stop-hbase.sh
start-hbase.sh
重新进入Phoenix 客户端
-- 退出客户端
!q
-- 进入客户端
sqlline.py nn1:2181-- 先在phoenix中创建schema对应hbase的namespace
create schema if not exists hainiu;
3创建带有命名空间的表
-- 在phoenix创建hainiu:relatetable的关联表
-- 其中 column_encoded_bytes0 是把字段名转成字符串而不是原来的byte数组
create table hainiu.relatetable_2(id varchar not null primary key,cf1.name varchar,cf1.age varchar,cf2.address varchar
) column_encoded_bytes0;-- 在phoenix中插入一条数据测试
upsert into hainiu.relatetable_2 (id,cf1.name,cf1.age,cf2.address) values (x0003,user3,22,guangzhou);select * from hainiu.relatetable_2;
select name from hainiu.relatetable_2;
select cf1.name from hainiu.relatetable_2;-- 没有给进行BYTES.tostring
create table hainiu.relatetable_3(id varchar not null primary key,cf1.name varchar,cf1.age varchar,cf2.address varchar
);
upsert into hainiu.relatetable_3 (id,cf1.name,cf1.age,cf2.address) values (x0003,user3,22,guangzhou);
建表语句中带有 column_encoded_bytes0 从hbase查询字段名能看得懂否则看不懂。
1. 12 phoenix建表时指定组合rowkey
-- 通过 CONSTRAINT pk primary key ( prefix,id ) 设定联合主键作为rowkey
-- 当prefix和id作为联合主键 只在hbase的rowkey中存在 column里没有
-- 建表语句
create table hainiu.combinationkey_table1 (prefix varchar not null,id varchar not null,col1 varchar,col2 varcharCONSTRAINT pk primary key ( prefix,id )
) column_encoded_bytes0, compressionsnappy split on (1,2,|);-- 插入数据
upsert into hainiu.combinationkey_table1 (prefix,id,col1,col2) values (1,001,user1,20);
upsert into hainiu.combinationkey_table1 (prefix,id,col1,col2) values (1,002,user2,21);-- 查看表结构
!describe hainiu.combinationkey_table
1. 13 phoenix实现动态列
-- 创建表
create table hainiu.dynamic_table1(pk varchar not null primary key,col1 varchar,col2 varchar
)column_encoded_bytes0;-- 插入数据
upsert into hainiu.dynamic_table1 (pk,col1,col2) values (x0001,user1,20);
upsert into hainiu.dynamic_table1 (pk,col1,col2) values (x0002,user1,21);
upsert into hainiu.dynamic_table1 (pk,col1,col2) values (x0003,user1,22);
upsert into hainiu.dynamic_table1 (pk,col1,col2) values (x0004,user1,23);-- 动态插入列
-- 动态插入 col3 和 col4 列
upsert into hainiu.dynamic_table1 (pk,col1,col2,col3 varchar,col4 varchar) values (x0005,user1,23,beijing,hainiu);
-- 动态插入 col4 和 col5 列
upsert into hainiu.dynamic_table1 (pk,col1,col2,col4 varchar,col5 varchar) values (x0006,user2,32,huawei,30K);-- 动态插入 col3、col4、col5 列
upsert into hainiu.dynamic_table1 (pk,col1,col2,col3 varchar,col4 varchar,col5 varchar) values (x0007,user3,33,shanghai,ali,22K);
-- 动态插入 col3、col4、col5、col6 列
upsert into hainiu.dynamic_table1 (pk,col1,col2,col3 varchar,col4 varchar,col5 varchar,col6 varchar) values (x0008,user4,35,shanghai,baidu,12K,false);-- phoenix中查询动态列
select * from hainiu.dynamic_table1(col3 varchar,col4 varchar);
select * from hainiu.dynamic_table1(col3 varchar,col4 varchar,col5 varchar) ;
select * from hainiu.dynamic_table1(col3 varchar,col4 varchar,col5 varchar,col6 varchar) ;
2. 索引
2.1 开启索引
配置hbase的hbase-site.xml
propertynamehbase.regionserver.wal.codec/namevalueorg.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec/value
/property
#分发到不同的机器#重启hbase集群
stop-hbase.sh
start-hbase.sh
# 删除Phoenix中的配置文件
hbase-site.xml
# 将hbase的配置文件给Phoenix数据准备
-- 创建测试表
create table hainiu.testindex(pk varchar not null primary key,col1 varchar
)column_encoded_bytes0;-- 插入数据
upsert into hainiu.testindex values (x1,1);
……
upsert into hainiu.testindex values (x20000,20000);
-- 编写脚本生成SQL文件
[rootworker-1 hdfs_test]# vim s1.sh
#! /bin/bashfor((i1;i20000;i))
doecho upsert into \hainiu\.\testindex\ values (x${i},${i}); testindex.sql
done-- 执行SQL文件导入表
sqlline.py nn1:2181 testindex.sql
索引开启前查询
-- 查看执行计划发现全表扫描
explain select * from hainiu.testindex1 where COL1 200;
-- 查询
select * from hainiu.testindex1 where COL1 200;
通过执行计划可以发现查询为FULL SCAN。全表扫描。
索引操作
-- 基于 COL1字段 创建索引 当创建完后索引里存的是已经排序好的COL1数据
-- local index 适用于写操作频繁的场景。索引数据和数据表的数据是存放在相同的服务器中的避免了在写操作的时候往不同服务器的索引表中写索引带来的额外开销
create local index myindex1 on hainiu.testindex (COL1);-- 查看执行计划发现不全表扫描
explain select * from hainiu.testindex where COL1 200;
select * from hainiu.testindex where COL1 200;-- 删除索引
drop index myindex on hainiu.testindex;