1.4.3.6 Hive的DML操作

总目录:https://blog.csdn.net/qq_41106844/article/details/105553392

Hadoop - 子目录:https://blog.csdn.net/qq_41106844/article/details/105553369

什么是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';

 
20155953-3fd5994085ab7ff3.png
创建表

追加本地文件

load data local inpath '/usr/hive_data/student.txt' into table student;

 
20155953-9597bcc9f2430254.png
追加
 
20155953-df53a632156e0e9e.png
查询

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

追加hdfs文件

 
20155953-72567fc3e2f1b152.png
上传到HDFS上
 
20155953-79001c6720e25f69.png
追加hdfs文件
 
20155953-5779e7d08ff55b86.png
查询

注:追加hdfs文件是移动

数据覆盖

load data local inpath '/usr/hive_data/student.txt' overwrite into table student;

 
20155953-eefb9b0a73d7fe0f.png
覆盖
 
20155953-c244c59e3c7ca05a.png
查询


这一次导入的数据就会覆盖之前的数据。

数据插入

单条数据插入

insert into table student values(5,"zhaoqi");

 
20155953-de6ebfb21aee1cfe.png
插入

基本模式插入

insert into table student select id,name from student where id=1;

 
20155953-6051401aebc260ab.png
查询插入

将查询结果插入到表中。

创建表时导入数据

create table if not exists student5(
id int,name string)
row format delimited fields terminated by '\t'
location '/user/hive/warehouse/student5';
 
20155953-2e024890cd5864da.png
设置location

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

 
20155953-7fd43f2a3dc76835.png
上传

然后查询一下。

 
20155953-4f8c73e7aecf664a.png
查询

Import导入数据

import table 要接受数据的表 from '要导入数据的绝对路径';

数据导出

Insert导出

insert overwrite local directory
'/usr/hive_data/student'
select * from student;
 
20155953-cffce2f2a8b40e36.png
导出
 
20155953-7e2c45f71ac62ec2.png
查看本地

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;

只能清除内部表数据,不能清除外部表。

发布了242 篇原创文章 · 获赞 60 · 访问量 2170

猜你喜欢

转载自blog.csdn.net/qq_41106844/article/details/105553332