一. DDL数据定义

创建数据库

1)create database db_hive;

2)避免要创建的数据库已经存在错误,增加 if not exists 判断。

create database if not exists db_hive;

3)创建一个数据库,指定数据库在 HDFS 上存放的位置

create database db_hive2 location '/db_hive2.db';

二. 查询数据库

1.显示数据库

1)show databases;

2)模糊查询

show databases like 'db_hive*';

2.查看数据库详情

1)desc database db_hive;

2)显示详细信息

desc database extended db_hive;

3.切换数据库

use db_hive;

三. 修改数据库

1. 用户可以使用 ALTER DATABASE 命令为某个数据库的 DBPROPERTIES 设置键-值对属性值,来描述这个数据库的属性信息。数据库的其他元数据信息都是不可更改的,包括数据库名和数据库所在的目录位置。

alter database hive set dbproperties('createtime'='20170830');

2. 在 hive 中查看修改结果

desc database extended db_hive;

四. 删除数据库

1.删除空数据库

drop database db_hive2;

2.如果删除的数据库不存在,最好采用 if exists 判断数据库是否存在

drop database if exists db_hive2;

3.如果数据库不为空,可以采用 cascade 命令,强制删除

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]

字段解释说明:

(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]

[MAP KEYS TERMINATED BY char] [LINES 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确定表的具体的列的数据。SerDe 是 Serialize/Deserilize 的简称,目的是用于序列化和反序列化。

(8)STORED AS 指定存储文件类型

  常用的存储文件类型:SEQUENCEFILE(二进制序列文件)、TEXTFILE(文本)、RCFILE(列式存储格式文件)

  如果文件数据是纯文本,可以使用 STORED AS TEXTFILE。如果数据需要压缩,使用 STORED AS SEQUENCEFILE。

(9)LOCATION :指定表在 HDFS 上的存储位置。

(10)LIKE 允许用户复制现有的表结构,但是不复制数据。

2.管理表

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

3.外部表

1)理论

因为表是外部表,所以 Hive 并非认为其完全拥有这份数据。删除该表并不会删除掉这

份数据,不过描述表的元数据信息会被删除掉。

2)管理表和外部表的使用场景

每天将收集到的网站日志定期流入 HDFS 文本文件。在外部表(原始日志表)的基础上做大量的统计分析,用到的中间表、结果表使用内部表存储,数据通过 SELECT+INSERT进入内部表。

例:

create external table if not exists default.dept( deptno int, dname string, loc int ) row format delimited fields terminated by '\t';

查看表格式化数据:desc formatted dept;

4.管理表与外部表相互转换

(1)查询表的类型

hive (default)> desc formatted student2;

Table Type:             MANAGED_TABLE

(2)修改内部表

student2 为外部表 alter table student2 set tblproperties('EXTERNAL'='TRUE');

(3)查询表的类型

hive (default)> desc formatted student2;

Table Type:             EXTERNAL_TABLE

(4)修改外部表

student2 为内部表 alter table student2 set tblproperties('EXTERNAL'='FALSE');

(5)查询表的类型

hive (default)> desc formatted student2;

Table Type:             MANAGED_TABLE

注意:('EXTERNAL'='TRUE')和('EXTERNAL'='FALSE')为固定写法,区分大小写

六. 分区表

  分区表实际上就是对应一个 HDFS 文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。Hive 中的分区就是分目录,把一个大的数据集根据业务需要分割成小的数据集。在查询时通过 WHERE 子句中的表达式选择查询所需要的指定的分区,这样的查询效率会提高很多。

1.分区表基本操作

1)创建分区表语法:create table dept_partition( deptno int, dname string, loc string ) partitioned by (month string) row format delimited fields terminated by '\t';

2)加载数据到分区表中

load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201709');

load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201708');

load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201707’);

3)查询分区数据

select * from dept_partition where month='201709';

4)增加分区

alter table dept_partition add partition(month='201706') ;

alter table dept_partition add partition(month='201705') partition(month='201704');

5)删除分区

alter table dept_partition drop partition (month='201704');

6)查看分区表有多少分区   show partitions dept_partition;

7)查看分区表结构    desc formatted dept_partition;

2.注意事项

1) 创建二级分区

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)方式一:上传数据后修复  上传数据

dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=12;

dfs -put /opt/module/datas/dept.txt

/user/hive/warehouse/dept_partition2/month=201709/day=12;

查询数据(查询不到刚上传的数据)

select * from dept_partition2 where month='201709' and day='12';

执行修复命令

msck repair table dept_partition2;

再次查询数据

select * from dept_partition2 where month='201709' and day='12';

(2)方式二:上传数据后添加分区

上传数据

dfs -mkdir -p  /user/hive/warehouse/dept_partition2/month=201709/day=11;

dfs -put /opt/module/datas/dept.txt

/user/hive/warehouse/dept_partition2/month=201709/day=11;

执行添加分区

alter table dept_partition2 add partition(month='201709',  day='11');

查询数据

select * from dept_partition2 where month='201709' and day='11';

(3)方式三:创建文件夹后 load 数据到分区

创建目录

dfs -mkdir -p  /user/hive/warehouse/dept_partition2/month=201709/day=10;

上传数据

load data local inpath '/opt/module/datas/dept.txt' into table  dept_partition2 partition(month='201709',day='10');

查询数据

select * from dept_partition2 where month='201709' and day='10';

03hive_DDL数据定义的更多相关文章

  1. Oracle language types(语言种类) 表的相关操作 DDL数据定义语言

    数据定义语言 Data Definition Language Statements(DDL)数据操纵语言 Data Manipulation Language(DML) Statements事务控制 ...

  2. 《how to design programs》15章 相互引用的数据定义

    由结构体组成的表与结构体中的表. 在用追溯形式建立家家谱树时,我们通常从某个后代除法,依次处理它的父母,组父母等.而构建树时,我们会不断添加谁是谁的孩子,而不是写出谁是谁的父母,从而建立一颗后代家谱树 ...

  3. SQL语言学习-数据定义语言

    Sql语言至今已经有6个版本.SQL查询语言包括了所有对数据的操作命令,这些操作可分为四类:数据定义语言(DDL).数据操纵语言(DML).数据控制语言(DCL)和嵌入式SQL语言. 数据定义语言(D ...

  4. Python学习记录----数据定义

    摘要: 描述Python中数据定义格式,需要注意的东东. 一 数据声明 Python木有一般语言的具体数据类型,像char,int,string这些通通木有.这有点像javascript,但又不同,j ...

  5. 跟我一起读postgresql源码(七)——Executor(查询执行模块之——数据定义语句的执行)

    1.数据定义语句的执行 数据定义语句(也就是之前我提到的非可优化语句)是一类用于定义数据模式.函数等的功能性语句.不同于元组增删査改的操作,其处理方式是为每一种类型的描述语句调用相应的处理函数. 数据 ...

  6. DDL(数据定义语言)

    1.Oracle中常见的数据类型分类:(A) 1.number(x,y) 数字类型,x表示最大长度,y表示精度对应java中除char外所有基本数据类型(byte.short.int.long.flo ...

  7. oracle学习笔记(三) DCL 数据控制语言与 DDL 数据定义语言

    DCL 数据控制语言 Data control language 之前说过的授权和收权利语句 grant, revoke DDL 数据定义语言 Data define language create ...

  8. SQL DDL 数据定义语句

    前言 DDL(Data Definition Language)语句:数据定义语句,这些语句定义了不同的数据段.数据库.表.列.索引等数据库对象.常用的语句关键字主要包括 create.drop.al ...

  9. ODPS SQL <for 数据定义语言 DDL>

    数据定义语言:(DDL) 建表语句: CREATE TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_comment ...

随机推荐

  1. phpstorm实用技巧

    1.切换php代码版本提示 1.1找到External Libraries右键点击(configure PHP Include Paths) 1.2切换提示版本(在这里切换版本)确定即可 2.生成ge ...

  2. ASP.NET 模型验证2--验证部分属性

    在开发MVC时,模型验证非常常见,平常我们用的应该都是全验证 if(ModelState.IsValid){ //验证成功要做的事 .....} 但是有时候我们需要部分验证,比如修改用户信息时,因为更 ...

  3. Jmeter录制https协议不能跳转成功(证书导入)

    原文:  http://www.cnblogs.com/Lam7/p/7154120.html 录制脚本的时候,比如录制https协议的百度网站 https://www.baidu.com ,所有录制 ...

  4. 【SQL】基础概念

    1.. In order to find the rows where the value for a column is or is not NULL, you would use IS NULL  ...

  5. Outlook365(Oulook2016 或2013) 写邮件输入收件人时的推荐联系人如何清理?

    · 在Outlook365(Oulook2016 或2013) 中写邮件,输入收件人邮箱地址时,会出现“最近联系人”  “其他建议”等推荐的联系人,可以方便选择.如果里面有很多邮箱地址的已经无效的话, ...

  6. 如何在Word中排出漂亮的代码,去除回车符,去除拼写检查

    这位博主写到很到位,这里补充一下在VBA里用模块的部分. https://blog.csdn.net/code4101/article/details/41802715 1.放代码的方式是贴纯文本. ...

  7. 在sql server中如何检测一个字符串中是否包含另一个字符串

    select CHARINDEX('456','123456')   SQL语句使用CHARINDEX函数,来测试一个字符串中是否包含另一个字符串中的方法: 一.CHARINDEX函数介绍 1.函数功 ...

  8. C# ODP.Net oracle数据库操作 支持不安装客户端

    下载: http://download.oracle.com/otn/other/ole-oo4o/ODTwithODAC1110720.zip?AuthParam=1414811820_e61f2f ...

  9. python3练习100题——010

    第10天了,今天的题目跟009类似,都比较水,有时间的话再做一道- 链接:http://www.runoob.com/python/python-exercise-example10.html 题目: ...

  10. OpenCV图像载入、显示和输出到文件以及滑块的使用

    图像载入 imread()函数 Mat imread(const string& filename, int flags = 1); 第一个参数为文件名 第二个参数为载入标识 flags &g ...