Hive DML(Data Manipulation Language)数据操作

Hive DML(Data Manipulation Language)数据操作

一、Load

Load 语句可将文件导入到 Hive 表中。

1)语法

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

关键字说明:
(1)local:表示从本地加载数据到 Hive 表;否则从 HDFS 加载数据到 Hive 表。
(2)overwrite:表示覆盖表中已有数据,否则表示追加。
(3)partition:表示上传到指定分区,若目标是分区表,需指定分区。

2)实操示例
(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 中
1、上传文件到 HDFS

[atguigu@hadoop102 ~]$ hadoop fs -put /opt/module/datas/student.txt /user/atguigu

2、加载 HDFS 上数据,导入完成后去 HDFS 上查看文件是否还存在

hive (default)> 
load data inpath '/user/atguigu/student.txt' 
into table student;

(3)加载数据覆盖表中已有的数据
1、上传文件到 HDFS

hive (default)> hdfs -put /opt/module/datas/student.txt /user/atguigu;

2、加载数据覆盖表中已有的数据

hive (default)> 
load data inpath '/user/atguigu/student.txt' 
overwrite into table student;

二、Insert

2.1、将查询结果插入表中

1)语法

INSERT (INTO | OVERWRITE) TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement;

关键字说明:
(1)INTO:将结果追加到目标表
(2)OVERWRITE:用结果覆盖原有数据

2)示例
(1)新建一张表

hive (default)> 
create table student1(
    id int, 
    name string
) 
row format delimited fields terminated by '\t';

(2)根据查询结果插入数据

hive (default)> insert overwrite table student1 
select 
    id, 
    name 
from student;

2.2、将给定Values插入表中

1)语法

INSERT (INTO | OVERWRITE) TABLE tablename [PARTITION (partcol1[=val1], partcol2[=val2] ...)] VALUES values_row [, values_row ...]

2)示例

hive (default)> insert into table  student1 values(1,'wangwu'),(2,'zhaoliu');

2.3、将查询结果写入目标路径

1)语法

INSERT OVERWRITE [LOCAL] DIRECTORY directory
  [ROW FORMAT row_format] [STORED AS file_format] select_statement;

2)示例

insert overwrite local directory '/opt/module/datas/student' ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.JsonSerDe'
select id,name from student;

三、Export&Import

Export 导出语句可将表的数据和元数据信息一并到处的 HDFS 路径,Import 可将 Export 导出的内容导入 Hive,表的数据和元数据信息都会恢复。Export 和 Import 可用于两个 Hive 实例之间的数据迁移

1)语法

--导出
EXPORT TABLE tablename TO 'export_target_path'
--导入
IMPORT [EXTERNAL] TABLE new_or_original_tablename FROM 'source_path' [LOCATION 'import_target_path']

2)示例

--导出
hive>
export table default.student to '/user/hive/warehouse/export/student';

--导入
hive>
import table student2 from '/user/hive/warehouse/export/student';

评论

暂无

添加新评论