Hive(6)-DML数据操作
一. 数据导入
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数据操作的更多相关文章
- HIVE之 DDL 数据定义 & DML数据操作
DDL数据库定义 创建数据库 1)创建一个数据库,数据库在 HDFS 上的默认存储路径是/user/hive/warehouse/*.db. hive (default)> create dat ...
- hive从入门到放弃(三)——DML数据操作
上一篇给大家介绍了 hive 的 DDL 数据定义语言,这篇来介绍一下 DML 数据操作语言. 没看过的可以点击跳转阅读: hive从入门到放弃(一)--初识hive hive从入门到放弃(二)--D ...
- Oracle基础(十) DML数据操作
一.DML数据操作语言 主要用于检索.插入和修改数据库信息.它是最常用的SQL命令,如INSERT(插入).UPDATE(更新).SELECT(选择).DELETE(删除). 1.INSERT插入语句 ...
- 6.1课堂笔记—DML(数据操作语言),DQL查询语句
一.DML(数据操作语言) InnoDB MyISAM 支持事务 不支持事务 不支持全文索引 支持全文索引 支持外键约束 不支持 命令查看默认存储引擎 show variables like '%st ...
- DML数据操作语言
DML数据操作语言 用来对数据库中表的数据记录进行更新.(增删改) 插入insert -- insert into 表(列名1,列名2,列名3...) values (值1,值2,值3...):向表中 ...
- Hive[5] HiveQL 数据操作
5.1 向管理表中装载数据 Hive 没有行级别的数据插入更新和删除操作,那么往表中装载数据的唯一途径就是使用一种“大量”的数据装载操作,或者通过其他方式仅仅将文件写入到正确的目录下: LOA ...
- DML数据操作语言之增加,删除,更新
1.数据的增加 数据的增加要用到insert语句 ,基本格式是: insert into <表名> (列名1,列名2,列名3,......) values (值1,值2,值3,..... ...
- Hive DDL DML SQL操作
工作中经常要用到的一些东西,一直没整理,用的多的记住了,用的不多的每次都是去查,所以记录一下. DDL(数据定义语言),那就包括建表,修改表结构等等了 建表:create hive table hiv ...
- DML数据操作语言之复杂查询
1.视图(View) 我们知道,在关系型数据库中,用来保存实际数据记录的是数据表.和表同等概念也是用来保存东西是:视图. 但是数据表是用来保存实际数据记录的,而视图是用来保存常用select语句的. ...
随机推荐
- lodash常用函数 - Array、Collection
lodash常用函数 - Array.Collection lodash版本 v3.10.1 1.Array.Collection pull 移除数组中满足条件的元素 var array = [1, ...
- 在 IDEA 中 配置 Maven
1.从 Maven官网下载 最新版的 Maven 2.设置Java相关环境变量 JAVA_HOME AVA_HOME=C:\jdk1.5.0_06 PATH ...
- cocos2d在IOS嵌入UM应用推荐
因为cocos2d默认建立的项目,没用使用导航界面,所以如果直接导航到应用推荐页面将无法返回. 所以我做了一些修改: AppController.mm中 用导航界面包装一下默认的viewControl ...
- Service Discovery in WCF 4.0 – Part 2 z
Service Discovery in WCF 4.0 – Part 2 In the previous post I discussed about the basic usage of WCF ...
- 【Leetcode】【Medium】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 解题: ...
- python接口自动化4-绕过验证码登录(cookie) (转载)
前言 有些登录的接口会有验证码:短信验证码,图形验证码等,这种登录的话验证码参数可以从后台获取的(或者查数据库最直接). 获取不到也没关系,可以通过添加cookie的方式绕过验证码. 一.抓登录coo ...
- Python学习---重点模块之json
注意:JSON不能转换类,不能转换函数 重点方法示例 json.dumps():实现文件写入,字符串转换[写入文件当然是JSON字符串楼] 实际上,json.dumps()只是帮我们做了一个字符串的转 ...
- windows生成硬链接
因工作电脑需要同时使用pl/sql和toad工具需要同时配置32位和64位oracle client如此增加了维护tnsnames.ora的复杂程度使用windows硬链接可以减少工作量,每次只修改源 ...
- spring core
https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#beans
- July 07th 2017 Week 27th Friday
Learn wisdom by the follies of others. 要从别人的愚行中学到智慧. How to become smart or what characters should a ...