HIVE SQL DDL&DML语句实例

创建新表(三种方式)

1.直接建表

drop table if exists student_score;
create table if not exists student_score(
	id string comment '记录id'
	,student_id string comment '学生学号'
	,subject_id string comment '科目id'
	,score string comment '成绩'
 )
partitioned by (university_name string comment '大学名称')--指定university_name字段作为分区字段
row format delimited fields terminated by '\u0001' --指定列分隔符
lines terminated by '\n'--指定行分隔符,hive默认使用\n作为行分隔符,所以此段可以不加
stored as parquet--指定hive文件的存储格式
location '存放地址'--指定存放地址
;

其中关于Hive的三种不同的文件存储格式可参考这篇文章https://blog.csdn.net/chenfeng_sky/article/details/107361081

2.create table as (直接使用查询结果插入到一张新表)

drop table if exists student_score_new;
create table student_score_new as 
(
select 
*
from student_score
)

通过执行show create table student_score_new,可以发现student_score_new并没有将student_score的分隔符、存储格式、分区字段、字段comment等表结构信息复制过来,只是复制了查询的字段和字段对应的值。

3.like (复制表结构)

create table student_score_structure_copy
like
student_score

student_score_structure_copy只是将student_score的表结构信息复制过来,但是没有复制student_score表的记录。所以student_score_structure_copy是一个空表。

向表中加载数据(三种方式)

1)直接向分区表中插入数据

--追加
insert into table student_score partition (university_name='hku')
values
('10000','20231123','math','a')
;
--覆盖
insert overwrite table student_score partition (university_name='hku')
values
('10000','20231123','math','a')
;

2)通过load方式加载数据

--追加
load data local inpath 'test_path/student_score.csv' into table student_score partition (university_name='hku');
--覆盖
load data local inpath 'test_path/student_score.csv' into table student_score partition (university_name='hku');

3)通过查询方式加载数据

--追加
insert into table student_score partition (university_name='hku') 
select id,student_id,subject_id,score
from student_score_backup
;
--覆盖
insert overwrite table student_score partition (university_name='hku') 
select id,student_id,subject_id,score
from student_score_backup
;

更改表名

alter table old_table_name rename to new_table_name;

更改字段

#将指定列数据类型更改为string
alter table table_name
change column_name_old column_name_new string comment '更改字段';

新增字段

alter table table_name add columns(new_column_name string comment '新增字段');

删除分区

alter table table_name drop partition(dt='2023-11-23');

添加分区

--单分区
alter table table_name add partition(dt='2023-11-23');
--多份分区
alter table table_name add partition(dt='2023-11-23') partition(university= 'hku');
--添加后可查看分区信息
show partitions table_name;

清空表数据

1)保留数据结构

 truncate table table_name;

2)不保留数据结构

 drop table table_name;

注意:truncate 和 drop:
如果 hdfs 开启了回收站,drop 删除的表数据是可以从回收站恢复的,表结构恢复不了,需要自己重新创建;truncate 清空的表是不进回收站的,所以无法恢复truncate清空的表
所以 truncate 一定慎用,一旦清空将无力回天

猜你喜欢

转载自blog.csdn.net/p1306252/article/details/123399229