HIVE SQL DDL&DML statement examples

Create a new table (three ways)

1. Create a table directly

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 '存放地址'--指定存放地址
;

For information on Hive’s three different file storage formats, please refer to this articlehttps://blog.csdn.net/chenfeng_sky/article/details/107361081

2.create table as (directly insert into a new table using query results)

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

By executingshow create table student_score_new, you can find that student_score_new does not copy the table structure information such as student_score's separator, storage format, partition field, field comment, etc., but only copies the queried fields and The value corresponding to the field.

3.like (copy table structure)

create table student_score_structure_copy
like
student_score

student_score_structure_copy only copies the table structure information of student_score, but does not copy the records of the student_score table. So student_score_structure_copy is an empty table.

Load data into the table (three ways)

1) Insert data directly into the partition table

--追加
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 data through load method

--追加
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) Load data through query

--追加
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
;

Change table name

alter table old_table_name rename to new_table_name;

Change field

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

Add new field

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

delete partition

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

Add partition

--单分区
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;

Clear table data

1) Preserve data structure

 truncate table table_name;

2) Does not retain data structure

 drop table table_name;

Note: truncate and drop:
If the recycle bin is enabled in HDFS, the table data deleted by drop can be recovered from the recycle bin, but the table structure cannot be restored and needs to be re-created by yourself; The table emptied by truncate will not be entered into the recycle bin, so the table emptied by truncate cannot be restored
Therefore, truncate must be used with caution. Once it is emptied, it will be irreversible

Guess you like

Origin blog.csdn.net/p1306252/article/details/123399229
Recommended