hive中的Load data 和 insert的区别

文章目录

    • 一、Load data
    • 二、insert
    • 三、IMPORT/EXPORT

一、Load data

语法为

LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename
[PARTITION (partcol1=val1, partcol2=val2 ...)]

local关键字:
有,表示从本地文件系统中导入
没有,表示从HDFS文件系统中导入,这里是文件的移动,理解为剪切

overwrite关键字
有,表示执行数据覆盖操作,原有数据会被全部覆盖(如果是分区表,则覆盖指定分区)
没有,表示执行数据追加操作,原有数据不会被覆盖

使用load data 装载数据是一种粗暴的方法,他并不会检查表的字段,如果文件中的字段多了,会自动舍去,少了,会使用NULL填充,所以,如果不是对表结构和文件数据非常熟悉的情况下,不建议使用load data

二、insert

语法为

INSERT OVERWRITE/INTO TABLE tablename1 
[PARTITION (partcol1=val1, partcol2=val2 ...)] 
select fileds,... from tb_other;

overwrite 关键字 表示覆盖
into 关键字 表示追加

insert插入数据(导入)

insert into employee select * from ctas_employee; -- 通过查询语句插入
-- 多插入
from ctas_employee      --高性能:只需扫描一次输入数据
insert overwrite table employee select *
insert overwrite table employee_internal select *;
-- 插入到分区
from ctas_patitioned 
insert overwrite table employee PARTITION (year, month)--典型的ETL模式
select *,'2018','09';
-- 通过指定列插入(insert into可以省略table关键字)
insert into employee(name) select 'John' from test limit 1;--指定列有利于 data schema changes
-- 通过指定值插入
insert into employee(name) value('Judy'),('John');

insert插入文件(导出)

-- 从同一数据源插入本地文件,hdfs文件,表
from ctas_employee
insert overwrite local directory '/tmp/out1'  select *
insert overwrite directory '/tmp/out1' select *
insert overwrite table employee_internal select *;
-- 以指定格式插入数据
insert overwrite directory '/tmp/out3'
row format delimited fields terminated by ','
select * from ctas_employee;

三、IMPORT/EXPORT

EXPORT导出数据

EXPORT TABLE employee TO '/tmp/output3';
EXPORT TABLE employee_partitioned partition (year=2014, month=11) TO 

使用IMPORT导入数据

IMPORT TABLE employee FROM '/tmp/output3';
IMPORT TABLE employee_partitioned partition (year=2014, month=11) FROM '/tmp/output5';

你可能感兴趣的:(hive,mysql,大数据,java,数据库)