一. 数据导入

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. JIAVA知识点整理

    Java具有垃圾回收机制,程序退出之后,使用的所有内存全部都将被释放,如要保存数据你就要建立文件,因为当保存时是保存在运行内存中的. int 有返回值void 不需要返回值 1.判断语句if else ...

  2. 在Java中如何进行BASE64编码和解码

    在Java中如何进行BASE64编码和解码 //在Java中如何进行BASE64编码和解码 package me.xzh.study.sun.misc.BASE64; import sun.misc. ...

  3. 通过CXF,开发rest协议接口

    1. 引入cxf的jar包 pom文件里面直接增加依赖 < dependency> <groupId > junit</ groupId> <artifact ...

  4. ego network的概念

    转:http://greatpowerlaw.wordpress.com/2013/01/05/ego-network/ 所谓的ego network,它的节点是由唯一的一个中心节点(ego),以及这 ...

  5. SchemaSpy

    SchemaSpy https://github.com/schemaspy/schemaspy/releases https://sourceforge.net/projects/schemaspy ...

  6. Webpack笔记(三)——一款破产版脚手架的开发

    前些天一直在学习入门Webpack,后来尝试了自己搭建一下一个简单的React开发环境,后来就在想可不可以自己写一个简单的脚手架,以免每次搭建一个简单的开发环境都需要自己一个个的配置,这样很麻烦,使用 ...

  7. mongodb在windows平台安装和启动

    mongodb 官网:https://www.mongodb.com mongodb 官网下载: mongodb-win32-x86_64-2008plus-ssl-3.4.2-signed.msi ...

  8. Yii2.0随笔 路由

    1.去掉index.php,按照pathinfo模式访问 例:http://***.com/控制器/方法 (1)把web服务器的网站目录指向所在模块的web目录 (2)在main.php的 'comp ...

  9. 检测Android和IOS

    var u=navigator.userAgent; var isAndroid=u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; / ...

  10. PHP单链表的基本操作

    链表的实现 数据结构第一个就是链表了,链表分为两种有直接的数组形式的顺序链,这里不讨论,什么array_push(),array_pop(),函数基本能满足日常的需求,但报告老板,我就是想装个X 上代 ...