一. 数据导入

1. 语法

load data [local] inpath 'path' [overwrite] into table table_name [partition (partcol1=val1,…)];

1). load data:表示加载数据

2). local:表示从本地加载数据到hive表;否则从HDFS加载数据到hive表

3). inpath:表示加载数据的路径

4). overwrite:表示覆盖表中已有数据,否则表示追加

5). into table:表示加载到哪张表

6). partition:表示上传到指定分区

2. 实操

1). 加载本地文件到hive

-- 创建一张表
create table student(id string, name string) row format delimited fields terminated by '\t'; -- 加载本地文件
load data local inpath '/opt/module/datas/student.txt' into table default.student;

2). 加载HDFS文件到hive中

#上传文件
dfs -put /opt/module/datas/student.txt /user/nty/hive;
-- 加载HDFS上数据
load data inpath '/user/nty/hive/student.txt' into table default.student;

3). 加载数据覆盖表中已有的数据

#上传文件
dfs -put /opt/module/datas/student.txt /user/nty/hive;
-- 加载数据覆盖表中已有的数据
load data inpath '/user/nty/hive/student.txt' overwrite into table default.student;

3. 通过查询语句向表中插入数据(Insert)

1). 创建一张分区表

create table student(id int, name string) partitioned by (month string) row format delimited fields terminated by '\t';

2). 基本插入数据

insert into table  student partition(month='') values(1,'wangwu'),(2,’zhaoliu’);

3). 基本模式插入(根据单张表查询结果)

insert overwrite table student partition(month='')
select id, name from student where month='';

insert into:以追加数据的方式插入到表或分区,原有数据不会删除

insert overwrite:会覆盖表或分区中已存在的数据

4).多表(多分区)插入模式(根据多张表查询结果)

from student
insert overwrite table student partition(month='')
select id, name where month=''
insert overwrite table student partition(month='')
select id, name where month='';

4. 查询语句中创建表并加载数据(As Select)

-- 根据查询结果创建表(查询的结果会添加到新创建的表中)
create table if not exists student3
as select id, name from student;

5. 创建表时通过Location指定加载数据路径

1). 上传数据到hdfs上

 dfs -mkdir /student;

 dfs -put /opt/module/datas/student.txt /student;

2). 创建表,并指定在hdfs上的位置

create external table if not exists student5(
id int, name string
)
row format delimited fields terminated by '\t'
location '/student;

6.Import数据到指定表中

import table student2 partition(month='') from
'/user/hive/warehouse/export/student';

注意:先用export导出后,再将数据导入。

二. 数据导出

1.Insert 导出

-- 将查询的结果导出到本地
insert overwrite local directory '/opt/module/datas/export/student'
select * from student; -- 将查询的结果格式化导出到本地
insert overwrite local directory '/opt/module/datas/export/student1'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select * from student; -- 将查询的结果导出到HDFS上(没有local)
insert overwrite directory '/user/nty/student2'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select * from student;

2. Hadoop命令导出到本地

dfs -get /user/hive/warehouse/student/month=/000000_0 /opt/module/datas/export/student3.txt;

3. Hive Shell 命令导出

bin/hive -e 'select * from default.student;' > /opt/module/datas/export/student4.txt;

4. Export导出到HDFS上

export table default.student to '/user/hive/warehouse/export/student';

三. 清除数据(Truncate)

truncate table student;

Truncate只能删除管理表,不能删除外部表中数据

Hive(6)-DML数据操作的更多相关文章

  1. HIVE之 DDL 数据定义 & DML数据操作

    DDL数据库定义 创建数据库 1)创建一个数据库,数据库在 HDFS 上的默认存储路径是/user/hive/warehouse/*.db. hive (default)> create dat ...

  2. hive从入门到放弃(三)——DML数据操作

    上一篇给大家介绍了 hive 的 DDL 数据定义语言,这篇来介绍一下 DML 数据操作语言. 没看过的可以点击跳转阅读: hive从入门到放弃(一)--初识hive hive从入门到放弃(二)--D ...

  3. Oracle基础(十) DML数据操作

    一.DML数据操作语言 主要用于检索.插入和修改数据库信息.它是最常用的SQL命令,如INSERT(插入).UPDATE(更新).SELECT(选择).DELETE(删除). 1.INSERT插入语句 ...

  4. 6.1课堂笔记—DML(数据操作语言),DQL查询语句

    一.DML(数据操作语言) InnoDB MyISAM 支持事务 不支持事务 不支持全文索引 支持全文索引 支持外键约束 不支持 命令查看默认存储引擎 show variables like '%st ...

  5. DML数据操作语言

    DML数据操作语言 用来对数据库中表的数据记录进行更新.(增删改) 插入insert -- insert into 表(列名1,列名2,列名3...) values (值1,值2,值3...):向表中 ...

  6. Hive[5] HiveQL 数据操作

    5.1 向管理表中装载数据   Hive 没有行级别的数据插入更新和删除操作,那么往表中装载数据的唯一途径就是使用一种“大量”的数据装载操作,或者通过其他方式仅仅将文件写入到正确的目录下:   LOA ...

  7. DML数据操作语言之增加,删除,更新

    1.数据的增加 数据的增加要用到insert语句  ,基本格式是: insert into <表名> (列名1,列名2,列名3,......) values (值1,值2,值3,..... ...

  8. Hive DDL DML SQL操作

    工作中经常要用到的一些东西,一直没整理,用的多的记住了,用的不多的每次都是去查,所以记录一下. DDL(数据定义语言),那就包括建表,修改表结构等等了 建表:create hive table hiv ...

  9. DML数据操作语言之复杂查询

    1.视图(View) 我们知道,在关系型数据库中,用来保存实际数据记录的是数据表.和表同等概念也是用来保存东西是:视图. 但是数据表是用来保存实际数据记录的,而视图是用来保存常用select语句的. ...

随机推荐

  1. ubuntu下root不能复制 abc用户下的软连接文件

    使用root用户去复制,提示权限不足: 那么这个软连接是个什么东西??

  2. 基于bootstrap的单选(radio)或者多选(checkbox)的选择框组

    完成的效果如下图所示: html代码如下: <!-- 两行组 --> <div class="box"> <ul class="list-g ...

  3. lvm 相关

    求教:/home分区和/root分区的关系 lvm扩容试验 [复制链接] lvm快速使用http://imysql.cn/2008_05_05_quick_startup_lvm Linux LVM学 ...

  4. skype for business server 2015 报错“不可用:试图检查架构状态时发生故障,请确保能够访问Active Direcotry”

    报错“不可用:试图检查架构状态时发生故障,请确保能够访问Active Direcotry” 遇到错误后就上网查询了下,有的人说用下面方法解决了 用域的administrator 登入就可以了(之前是用 ...

  5. js中公有方法、特权方法、静态方法

    1.公有属性和公有方法 1 2 3 4 5 6 7 8 9 function User(name,age){   this.name = name;//公有属性   this.age = age; } ...

  6. 一直在用的一个javascript网站

    http://www.dottoro.com/ 很不错,例子丰富,解释详细,全面:非常好的参考资料站.

  7. Micro

    Micro 架构与设计   Micro 架构与设计 翻译自 Micro architecture & design patterns for microservices 注: 原文作者即 Mi ...

  8. 可变对象(immutable)和不可变对象(mutable)

    可变对象(immutable)和不可变对象(mutable) 这个是之前一直忽略的一个知识点,比方说说起String为什么是一个不可变对象,只知道因为它是被final修饰的所以不可变,而没有抓住不可变 ...

  9. IBM websphere MQ远程队列的简单配置

    原理: 1.远程队列分发送方和接收方 2.接收方配置: 接收方配置要先拿到对方的发送通道配置,接收方的队列名称必须和远程发送方的队列名称一致,告诉远程发送方,你的地址,队列管理器名称等信息,在通道中建 ...

  10. 对HandlerExecutionChain类的理解分析

    HandlerExecutionChain类比较简单,好理解. ==================================================================== ...