1.4.3.6 Hive的DML操作
2020-04-10 本文已影响0人
寒暄_HX
总目录:https://www.jianshu.com/p/e406a9bc93a9
Hadoop - 子目录:https://www.jianshu.com/p/9428e443b7fd
什么是DML
DML--数据操纵语言,用来对数据库中的存储数据进行一些简单操作。
数据导入
语法
load data [local] inpath '文件绝对路径' [overwrite] into table student [partition (partcol1=val1,…)];
(1)load data:表示加载数据
(2)local:表示从本地加载数据到 hive 表;否则从 HDFS 加载数据到 hive 表
(3)inpath:表示加载数据的路径
(4)overwrite:表示覆盖表中已有数据,否则表示追加
(5)into table:表示加载到哪张表
(6)student:表示具体的表
(7)partition:表示上传到指定分区
数据追加
先创建一个表
create table student(id string, name string) row format delimited fields terminated by '\t';

追加本地文件
load data local inpath '/usr/hive_data/student.txt' into table student;


注:追加本地文件是拷贝,不会影响本地上的数据。
追加hdfs文件



注:追加hdfs文件是移动
数据覆盖
load data local inpath '/usr/hive_data/student.txt' overwrite into table student;


这一次导入的数据就会覆盖之前的数据。
数据插入
单条数据插入
insert into table student values(5,"zhaoqi");

基本模式插入
insert into table student select id,name from student where id=1;

将查询结果插入到表中。
创建表时导入数据
create table if not exists student5(
id int,name string)
row format delimited fields terminated by '\t'
location '/user/hive/warehouse/student5';

然后我们把符合这个表格式的文件上传到这个目录,就表示导入数据了。

然后查询一下。

Import导入数据
import table 要接受数据的表 from '要导入数据的绝对路径';
数据导出
Insert导出
insert overwrite local directory
'/usr/hive_data/student'
select * from student;


Hadoop命令导出
使用 hadoop dfs -get
下载文件
HIVE shell导出
hive -e 'select * from default.student;' > /usr/hive_data/student_hive;
Export 导出到 HDFS 上
export table default.student to '/user/hive/warehouse/export/student';
清除表中数据
truncate table student;
只能清除内部表数据,不能清除外部表。