目录
DML(Data Manipulation Language)数据操作
Load
1)语法
2)实操案例
Insert
将查询结果插入表中
1)语法
2)案例
将给定Values插入表中
1)语法
2)案例
将查询结果写入目标路径
1)语法
2)案例
Export & Import
1)语法
2)案例
Load语句可将文件导入到Hive表中。
LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)];
关键字说明:
(0)创建一张表
hive (default)>
create table student(
id int,
name string
)
row format delimited fields terminated by '\t';
(1)加载本地文件到hive
hive (default)> load data local inpath '/opt/module/datas/student.txt' into table student;
(2)加载HDFS文件到hive中
①上传文件到HDFS
[lzl@hadoop12 ~]$ hadoop fs -put /opt/module/datas/student.txt /user/lzl
②加载HDFS上数据,导入完成后去HDFS上查看文件是否还存在
hive (default)>
load data inpath '/user/lzl/student.txt'
into table student;
(3)加载数据覆盖表中已有的数据
①上传文件到HDFS
hive (default)> dfs -put /opt/module/datas/student.txt /user/lzl;
②加载数据覆盖表中已有的数据
hive (default)>
load data inpath '/user/lzl/student.txt'
overwrite into table student;
INSERT (INTO | OVERWRITE) TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement;
关键字说明:
(1)新建一张表
hive (default)>
create table student1(
id int,
name string
)
row format delimited fields terminated by '\t';
(2)根据查询结果插入数据
hive (default)> insert overwrite table student3
select
id,
name
from student;
INSERT (INTO | OVERWRITE) TABLE tablename [PARTITION (partcol1[=val1], partcol2[=val2] ...)] VALUES values_row [, values_row ...]
hive (default)> insert into table student1 values(1,'wangwu'),(2,'zhaoliu');
INSERT OVERWRITE [LOCAL] DIRECTORY directory
[ROW FORMAT row_format] [STORED AS file_format] select_statement;
insert overwrite local directory '/opt/module/datas/student' ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.JsonSerDe'
select id,name from student;
Export导出语句可将表的数据和元数据信息一并导出到HDFS路径,Import可将Export导出的内容导入Hive,表的数据和元数据信息都会恢复。Export和Import可用于两个Hive实例之间的数据迁移。
导出
EXPORT TABLE tablename TO 'export_target_path'
导入
IMPORT [EXTERNAL] TABLE new_or_original_tablename FROM 'source_path' [LOCATION 'import_target_path']
导出
hive>
export table default.student to '/user/hive/warehouse/export/student';
导入
hive>
import table student2 from '/user/hive/warehouse/export/student';