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\';
继续阅读与本文标签相同的文章
上一篇 :
保时捷将与SAP共同开发数字化转型解决方案
下一篇 :
vue2.0项目文件夹介绍
-
现在三四线城市自主创业什么项目好?可以试试共享设备
2026-05-15栏目: 教程
-
网站资源干货分享:在线工具、图片视频素材、办公应有尽有!
2026-05-15栏目: 教程
-
人工智能肾病预测系统上线,知晓5年内终末期肾病概率
2026-05-15栏目: 教程
-
不期而至的感谢信
2026-05-15栏目: 教程
-
一张图看懂基木鱼平台
2026-05-15栏目: 教程
