HIVE之 DDL 数据定义 & DML数据操作
DDL数据库定义
创建数据库
1)创建一个数据库,数据库在 HDFS 上的默认存储路径是/user/hive/warehouse/*.db。
hive (default)> create database db_hive;
2)避免要创建的数据库已经存在错误,增加 if not exists 判断。(标准写法)
hive (default)> create database if not exists db_hive;
3)创建一个数据库,指定数据库在 HDFS 上存放的位置
hive (default)> create database db_hive2 location '/db_hive2.db'
修改数据库
用户可以使用 ALTER DATABASE 命令为某个数据库的 DBPROPERTIES 设置键-值对属性值,
来描述这个数据库的属性信息。数据库的其他元数据信息都是不可更改的,包括数据库名和数据库所在的目录位置
hive (default)> alter database db_hive set dbproperties('createtime'='20170830');
查看结果
hive> desc database extended db_hive;
查询数据库
1)显示数据库
hive> show databases;
2)过滤显示查询的数据库
hive> show databases like 'db_hive*';
3)显示数据库信息
hive> desc database db_hive;
4)显示数据库详细信息,extended
hive> desc database extended db_hive;
删除数据库
1)删除空数据库
hive>drop database db_hive2;
2)如果删除的数据库不存在,最好采用 if exists 判断数据库是否存在
hive> drop database if exists db_hive2;
3)如果数据库不为空,可以采用 cascade 命令,强制删除
hive> drop database db_hive cascade;
创建表
1)建表语法
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
[CLUSTERED BY (col_name, col_name, ...)
[SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]
2)字段解释说明:
(1)CREATE TABLE 创建一个指定名字的表。如果相同名字的表已经存在,则抛出异常;
用户可以用 IF NOT EXISTS 选项来忽略这个异常。
(2)EXTERNAL 关键字可以让用户创建一个外部表,在建表的同时指定一个指向实际数据的路径(LOCATION),
Hive 创建内部表时,会将数据移动到数据仓库指向的路径;若创建外部表,仅记录数据所在的路径,
不对数据的位置做任何改变。在删除表的时候,内部表的元数据和数据会被一起删除,而外部表只删除元数据,不删除数据。
(3)COMMENT:为表和列添加注释。
(4)PARTITIONED BY 创建分区表
(5)CLUSTERED BY 创建分桶表
(6)SORTED BY 不常用
(7)ROW FORMAT DELIMITED [FIELDS TERMINATED BY char]
[COLLECTION ITEMS TERMINATED BY char]| SERDE serde_name
[WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)]
用户在建表的时候可以自定义 SerDe 或者使用自带的 SerDe。如果没有指定 ROW FORMAT 或者 ROW FORMAT DELIMITED,
将会使用自带的 SerDe。在建表的时候,用户还需要为表指定列,用户在指定表的列的同时也会指定自定义的 SerDe,
Hive 通过 SerDe确定表的具体的列的数据。
(8)STORED AS 指定存储文件类型常用的存储文件类型:SEQUENCEFILE(二进制序列文件)、TEXTFILE(文本)、
RCFILE(列式存储格式文件)如果文件数据是纯文本,可以使用 STORED AS TEXTFILE。如果数据需要压缩,
使用 STORED AS SEQUENCEFILE。
(9)LOCATION :指定表在 HDFS 上的存储位置。
(10)LIKE 允许用户复制现有的表结构,但是不复制数据。
管理表
1)理论
默认创建的表都是所谓的管理表,有时也被称为内部表。因为这种表,Hive 会(或多或少地)控制着数据的生命周期。
Hive 默认情况下会将这些表的数据存储在由配置项hive.metastore.warehouse.dir(例如,/user/hive/warehouse)
所定义的目录的子目录下。 当我们删除一个管理表时,Hive 也会删除这个表中数据。管理表不适合和其他工具共享数据。
2)操作
(1)普通创建表
create table if not exists student2(
id int, name string
)
row format delimited fields terminated by '\t'
stored as textfile
location '/user/hive/warehouse/student2';
(2)根据查询结果创建表(查询的结果会添加到新创建的表中)
create table if not exists student3
as select id, name from student;
(3)根据已经存在的表结构创建表
create table if not exists student4 like student;
(4)查询表的类型
hive (default)> desc formatted student2;
Table Type: MANAGED_TABLE
外部表
1)理论
因为表是外部表,所有 Hive 并非认为其完全拥有这份数据。删除该表并不会删除掉这份数据,不过描述表的元数据信息会被删除掉。
2)管理表和外部表的使用场景:
每天将收集到的网站日志定期流入 HDFS 文本文件。在外部表(原始日志表)的基础上做大量的统计分析,
用到的中间表、结果表使用内部表存储,数据通过 SELECT+INSERT进入内部表。
3)案例
分别创建部门和员工外部表,并向表中导入数据。
create external table if not exists default.dept(
deptno int,
dname string,
loc int
)
row format delimited fields terminated by '\t';
create external table if not exists default.emp(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int)
row format delimited fields terminated by '\t';
4)导入数据
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept;
hive (default)> load data local inpath '/opt/module/datas/emp.txt' into table default.emp;
5)查询表格式化数据
hive (default)> desc formatted dept;
Table Type: EXTERNAL_TABLE
分区表
(1)创建分区表
hive (default)> create table dept_partition(
deptno int, dname string, loc string
)
partitioned by (month string)
row format delimited fields terminated by '\t';
(2)加载数据到分区表中
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table
default.dept_partition partition(month='201709');
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table
default.dept_partition partition(month='201708');
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table
default.dept_partition partition(month='201707');
(3)查询分区表中数据
hive (default)> select * from dept_partition where month='201709';
hive (default)> select * from dept_partition where month='201709'
union
select * from dept_partition where month='201708'
union
select * from dept_partition where month='201707';
分区操作
(1)增加分区
hive (default)> alter table dept_partition add partition(month='201706') ;
hive (default)> alter table dept_partition
add partition(month='201705') partition(month='201704');
(2)删除分区
hive (default)> alter table dept_partition drop partition (month='201704');
hive (default)> alter table dept_partition
drop partition (month='201705'), partition (month='201706');
(3)查看分区个数
hive>show partitions dept_partition;
(4)查看分区表结构
hive>desc formatted dept_partition;
分区表注意事项
1)创建二级分区表
hive (default)> create table dept_partition2(
deptno int, dname string, loc string
)
partitioned by (month string, day string)
row format delimited fields terminated by '\t';
2)正常的加载数据
(1)加载数据到二级分区表中
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table
default.dept_partition2 partition(month='201709', day='13');
(2)查询分区数据
hive (default)> select * from dept_partition2 where month='201709' and day='13';
3)把数据直接上传到分区目录上,让分区表和数据产生关联的两种方式
(1)方式一:上传数据后修复
上传数据
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=12;
hive (default)> dfs -put /opt/module/datas/dept.txt
/user/hive/warehouse/dept_partition2/month=201709/day=12;
查询数据(查询不到刚上传的数据)
hive (default)> select * from dept_partition2 where month='201709' and day='12';
执行修复命令
hive>msck repair table dept_partition2;
再次查询数据
hive (default)> select * from dept_partition2 where month='201709' and day='12';
(2)方式二:上传数据后添加分区
上传数据
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=11;
hive (default)> dfs -put /opt/module/datas/dept.txt
/user/hive/warehouse/dept_partition2/month=201709/day=11;
执行添加分区
hive (default)> alter table dept_partition2 add partition(month='201709', day='11');
查询数据
hive (default)> select * from dept_partition2 where month='201709' and day='11';
(3)方式三:上传数据后 load 数据到分区
创建目录
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=10;
上传数据
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table
dept_partition2 partition(month='201709',day='10');
查询数据
hive (default)> select * from dept_partition2 where month='201709' and day='10';
修改表
(1)重命名表
ALTER TABLE table_name RENAME TO new_table_name
hive (default)> alter table dept_partition2 rename to dept_partition3;
(2)增加/ 修改/替换列信息
更新列:
ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name
column_type [COMMENT col_comment] [FIRST|AFTER column_name]
增加和替换列:
ALTER TABLE table_name ADD|REPLACE COLUMNS (col_name data_type
[COMMENT col_comment], ...)
注:ADD 是代表新增一字段,字段位置在所有列后面(partition 列前),REPLACE 则是表示替换表中所有字段。
实操案例:
(1)查询表结构
hive>desc dept_partition;
(2)添加列
hive (default)> alter table dept_partition add columns(deptdesc string);
(3)查询表结构
hive>desc dept_partition;
(4)更新列
hive (default)> alter table dept_partition change column deptdesc desc int;
(5)查询表结构
hive>desc dept_partition;
(6)替换列
hive (default)> alter table dept_partition replace columns
(deptno string, dname string, loc string);
(7)查询表结构
hive>desc dept_partition;
删除表
hive (default)> drop table dept_partition;
DML数据操作
数据导入
向表中装载数据(Load)
1)语法
hive>load data [local] inpath '/opt/module/datas/student.txt' [overwrite]
into table student [partition (partcol1=val1,…)];
参数说明
(1)load data:表示加载数据
(2)local:表示从本地加载数据到 hive 表;否则从 HDFS 加载数据到 hive 表
(3)inpath:表示加载数据的路径
(4)into table:表示加载到哪张表
(5)student:表示具体的表
(6)overwrite:表示覆盖表中已有数据,否则表示追加
(7)partition:表示上传到指定分区
2)实操案例
0)创建一张表
hive (default)> create table student(id string, name string)
row format delimited fields terminated by '\t';
(1)加载本地文件到 hive
hive (default)> load data local inpath '/opt/module/datas/student.txt'
into table default.student;
(2)加载 HDFS 文件到 hive 中
上传文件到 HDFS
hive (default)> dfs -put /opt/module/datas/student.txt /user/upuptop/hive;
加载 HDFS 上数据
hive (default)>load data inpath '/user/upuptop/hive/student.txt' into table default.student;
(3)加载数据覆盖表中已有的数据
上传文件到 HDFS
hive (default)> dfs -put /opt/module/datas/student.txt /user/upuptop/hive;
加载数据覆盖表中已有的数据
hive (default)>load data inpath '/user/upuptop/hive/student.txt' overwrite
into table default.student;
通过查询语句向表中插入数据(Insert)
1)创建一张分区表
hive (default)> create table student(id string, name string) partitioned by (month string)
row format delimited fields terminated by '\t';
2)基本插入数据
hive (default)> insert into table student partition(month='201709')
values('1004','wangwu');
3)基本模式插入(根据单张表查询结果)
hive (default)> insert overwrite table student partition(month='201708')
select id, name from student where month='201709';
4)多插入模式(根据多张表查询结果)
hive (default)> from student
insert overwrite table student partition(month='201707')
select id, name where month='201709'
insert overwrite table student partition(month='201706')
select id, name where month='201709';
查询语句中创建表并加载数据(As Select )
根据查询结果创建表(查询的结果会添加到新创建的表中)
create table if not exists student3 as select id, name from student;
创建表时通过 Location
1)创建表,并指定在 hdfs 上的位置
hive (default)> create table if not exists student5(
id int, name string
)
row format delimited fields terminated by '\t'
location '/user/hive/warehouse/student5';
2)上传数据到 hdfs 上
hive (default)> dfs -put /opt/module/datas/student.txt /user/hive/warehouse/student5;
3)查询数据
hive (default)> select * from student5;
Import 数据到指定 Hive
先用 export 导出后,再将数据导入!!!注意格式问题
hive (default)> import table student2 partition(month='201709') from '/user/hive/warehouse/export/student';
数据导出
Insert 导出
1)将查询的结果导出到本地
hive (default)> insert overwrite local directory '/opt/module/datas/export/student'
select * from student;
2)将查询的结果格式化导出到本地
hive (default)> insert overwrite local directory '/opt/module/datas/export/student1'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
COLLECTION ITEMS TERMINATED BY '\n'
select * from student;
3)将查询的结果导出到 HDFS 上(没有 local)
hive (default)> insert overwrite directory '/user/upuptop/hive/warehouse/student2'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
COLLECTION ITEMS TERMINATED BY '\n'
select * from student;
Hadoop 命令导出到本地
hive (default)> dfs -get
/user/hive/warehouse/student/month=201709/000000_0 /opt/module/datas/export/student3.txt;
Hive Shell 命令导出
基本语法:(hive -f/-e 执行语句或者脚本 > file)
$ bin/hive -e 'select * from default.student;' > /opt/module/datas/export/student4.txt;
Export 导出到 HDFS
hive (default)> export table default.student to '/user/hive/warehouse/export/student';
清空数据
注意:Truncate 只能删除管理表,不能删除外部表中数据
hive (default)> truncate table student;
本博客仅为博主学习总结,感谢各大网络平台的资料。蟹蟹!!
HIVE之 DDL 数据定义 & DML数据操作的更多相关文章
- sql 两大类 DDL数据定义语言 和DCL数据控制语言
SQL分为五大类: DDL:数据定义语言 DCL:数据控制语言 DML:数据的操纵语言 DTL:数据事务语言 DQL:数据查询语言. DDL (date definition lang ...
- MySQL高级查询与编程笔记 • 【第2章 数据定义和操作】
全部章节 >>>> 本章目录 2.1 数据定义语言和数据操作语言 2.1.1 设计"优乐网"数据库 2.1.2 数据定义语言 2.1.3 数据操作语言 ...
- 数据库基础之一--DDL(数据库定义语言),DCL(数据库控制语言)
Mysql是一个非常典型的C/S结构的应用模型,所以Mysql连接必须依赖于一个客户端或者驱动. 在linux中支持两种连接模式:TCP/IP模式和socket SQL语句的四部分: DDL:数据定义 ...
- Hive 官方手册翻译 -- Hive DDL(数据定义语言)
Hive DDL(数据定义语言) Confluence Administrator创建, Janaki Lahorani修改于 2018年9月19日 原文链接 https://cwiki.apache ...
- hive从入门到放弃(三)——DML数据操作
上一篇给大家介绍了 hive 的 DDL 数据定义语言,这篇来介绍一下 DML 数据操作语言. 没看过的可以点击跳转阅读: hive从入门到放弃(一)--初识hive hive从入门到放弃(二)--D ...
- Oracle language types(语言种类) 表的相关操作 DDL数据定义语言
数据定义语言 Data Definition Language Statements(DDL)数据操纵语言 Data Manipulation Language(DML) Statements事务控制 ...
- 大数据开发实战:Hive表DDL和DML
1.Hive 表 DDL 1.1.创建表 Hive中创建表的完整语法如下: CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [ (col_nam ...
- Hive(三)【DDL 数据定义】
目录 一.DDL数据定义 1.库的DDL 1.1创建数据库 1.2查询数据库 1.3查看数据库详情 1.4切换数据库 1.5修改数据库 1.6删除数据库 2.表的DDL 2.1创建表 2.2管理表(内 ...
- hive从入门到放弃(二)——DDL数据定义
前一篇文章,介绍了什么是 hive,以及 hive 的架构.数据类型,没看的可以点击阅读:hive从入门到放弃(一)--初识hive 今天讲一下 hive 的 DDL 数据定义 创建数据库 CREAT ...
随机推荐
- delphi7 stringgrid 点列头排序
最近在做stringgrid的项目, 下面delphi7 正常使用,均摘抄网路,但做过细微调整才能正常使用 首先排序的过程 procedure Quicksort(Grid: TStringGrid; ...
- QML学习【一】Basic Types
QML入门教程(1) QML是什么? QML是一种描述性的脚本语言,文件格式以.qml结尾.语法格式非常像CSS(参考后文具体例子),但又支持javacript形式的编程控制.它结合了QtDesi ...
- 你真的懂printf么?
自从你进入程序员的世界,就开始照着书本编写着各种helloworld,大笔一挥: printf("Hello World!\n"); 于是控制台神奇地出现了一行字符串,计算机一句温 ...
- nice-validator表单验证插件的简单使用
前言 前端表单校验是过滤无效数据.假数据.有毒数据的第一步,是数据安全的第一道关卡,虽然我们不能100%相信客户端提交的数据(真正的校验还得在服务端进行),但设置前端表单校验也是至关重要的,自己写逻辑 ...
- 【hibernate-validator+SpringMVC】后台参数校验框架
hibernate-validator+SpringMVC 简介:简单说,就是对Entity进行校验. 1.导包,没有很严谨的对应关系,所以我用了比较新的版本,支持更多的注解. <depende ...
- 【Linux杂记】screen命令
screen命令: 解决的问题:有些进程启动后,一旦断开ssh容易断掉,所以新建一个screen(可以理解成窗口)并行运行 守护进程的方式:linux守护进程的方式实现开机启动某些特定的程序 屏幕命令 ...
- django-haystack+whoosh+jieba实现中文全文搜索
先上效果图 附上个人网站:https://liyuankun.cn 安装依赖库 注意:这里我们不安装django-haystack,因为要添加中文分词的功能很麻烦,所以我直接集成了一个中文的djang ...
- 一路编程 -- Gruntfile.js
<一路编程> Steven Foote 第四章构建工具 中的 Gruntfile.js 文件的 JSHint 部分,如果按照书中所写,run grunt 的命令的时候会出错. 此处附上完 ...
- OpenCV常用数据结构和函数
点的表示:Point类 Point类数据结构表示二维坐标系下的点,即由其图像坐标x,y指定的2D点. 用法如下 Point point; point.x = 10; point.y = 8; 或者 P ...
- 深度探索c++对象模型 第二章
1,c++转换函数:显示转换和隐式转换. 隐式转换为程序员提供了很大的变量.比如整形提升,普通类型转换为类类型(operator int())都为程序带来无尽的方便.试想,如果没有整形提升,一个sho ...