HiveSQL DML动态分区

2.6动态分区模式(让分区成为变量)
脚本模板

INSERT OVERWRITE TABLE tablename
PARTITION (partcol1[=var1], partcol2[=var2] ...) select_statement FROM from_statement

设置非严格模式

set hive.exec.dynamic.partition.mode=nonstric;

样例
有新表 student_outer,即新来的外部学生表,有若干学生数据,与student 表结构完全相同。
将 student_outer 表的多个分区数据,一次性插入到 student 表中。
数据准备
创建新表 student_outer 与之前的 student 结构完全一致

CREATE	TABLE student_outer( 
id string comment ' 学 号 ', 
username string comment '姓名', 
classid int comment '班级id',
classname string comment '班级名称') comment '学生信息主表'
partitioned by (come_date string comment '按入学年份分区') 
ROW FORMAT DELIMITED 
FIELDS TERMINATED BY '\t' 
LINES TERMINATED BY '\n'
STORED AS textfile;

装载数据

LOAD DATA LOCAL INPATH './student.txt' 
OVERWRITE INTO TABLE student_outer PARTITION (come_date=20171120);
LOAD DATA LOCAL INPATH './student.txt' 
OVERWRITE INTO TABLE student_outer PARTITION (come_date=20171121);
LOAD DATA LOCAL INPATH './student.txt' 
OVERWRITE INTO TABLE student_outer PARTITION (come_date=20171122);

查看分区列表

show partitions student_outer;

将 student_outer 表所有数据插入到 student 表

//先**清空**之前的student 表,方便查看效果
truncate table student;
insert overwrite table student partition(come_date) 
select id,username,classid,classname,come_date from student_outer

2.7将查询结果写入 hdfs 目录
脚本模版

NSERT OVERWRITE [LOCAL] DIRECTORY directory1 SELECT ... FROM ...

样例-默认

数据写入文件系统时进行文本序列化,且每列用^A 来区分,\n 换行
insert overwrite directory  "/tmp/output2/" 
select * from student 
where come_date='20170905';

样例-指定输分隔符

insert overwrite directory "/tmp/output2/" 
row format delimited
fields terminated by '\t'
select * from student where come_date='20170905';

你可能感兴趣的:(hive)