create table students(
id bigint,
name string,
age int,
gender string,
clazz string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
create external table students02(
id bigint,
name string,
age int,
gender string,
clazz string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
stored as rcfile
insert into table students select * from students02;覆盖原有的数据即hdfs上现在只有1000条数据,原本有2000条数据
insert overwrite table students select * from students02;
create table students03 as select * from students02;最常用的两种传输数据的两种方式
load data inpath 'hdfs路径' into table 表名; load data local inpath '本地路径' into table 表名;分区 创建分区表
create external table students(
id bigint,
name string,
age int,
gender string,
clazz string
)
PARTITIonED BY (year STRING, month STRING)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
通过load上传数据时会报一下错误
load data local inpath '/usr/lcoal/data/students_dt.txt' into table studnets04;FAILED: SemanticException [Error 10062]: Need to specify partition columns because the destination table is partitioned 原因:真正的分区表应该为 /user/hive/warehouse/shujia.db/students04/分区(目录)/数据 我们这里直接在students04下面存数据相当于中间跳了目录这一级所以会报错 所以我们通过hadoop的方式来上传数据
hadoop fs -put students_dt.txt /user/hive/warehouse/cj.db/students04并且通过alter增加分区字段来达到目录的效果
alter table students04 add partition(dt='20211125')以目录的形式再对数据做分割
//删除分区 //alter table students04 drop partition(dt='20211125')再通过load上传数据
load data local inpath '/usr/local/data/students_dt.txt' into table students04 partition(dt='20211125'); select * from students04;
// 使用where条件进行分区裁剪,避免了全表扫描,效率高 select count(*) from students_pt where dt='20211125';动态分区 比较下面两张表
create table students05(
id bigint,
name string,
age int,
gender string,
clazz string,
)
PARTITIonED BY (pt string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
create table students06(
id bigint,
name string,
age int,
gender string,
clazz string,
dt string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
开启动态分区有的时候我们原始表中的数据里面包含了 ‘‘日期字段 dt’’,我们需要根据dt中不同的日期,分为不同的分区,将原始表改造成分区表。
hive默认不开启动态分区
动态分区:根据数据中某几列的不同的取值 划分 不同的分区
# 表示开启动态分区 hive> set hive.exec.dynamic.partition=true; # 表示动态分区模式:strict(需要配合静态分区一起使用)、nostrict # strict: insert into table students_pt partition(dt='anhui',pt) select ......,pt from students; hive> set hive.exec.dynamic.partition.mode=nostrict; # 表示支持的最大的分区数量为1000,可以根据业务自己调整 hive> set hive.exec.max.dynamic.partitions.pernode=1000;使用动态分区插入数据
// 分区字段需要放在 select 的最后,如果有多个分区字段 同理,它是按位置匹配,不是按名字匹配 // students07表要跟06表字段一样并且必须要先存在 insert into table students07 partition(dt) select id,name,age,gender,clazz,dt from students06; // 比如下面这条语句会使用age作为分区字段,而不会使用student06中的dt作为分区字段 insert into table students07 partition(dt) select id,name,age,gender,dt,age from students06;多级分区
create table students_year_month
(
id bigint,
name string,
age int,
gender string,
clazz string,
year string,
month string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
create table students_year_month_pt
(
id bigint,
name string,
age int,
gender string,
clazz string
)
PARTITIonED BY(year string,month string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
insert into table students_year_month_pt partition(year,month) select id,name,age,gender,clazz,year,month from students_year_month;



