Hive-HQL数据定义DDL

介绍前的几点说明:

1、Hive不支持行级别的操作,也不支持事务;

2、Hive中的数据库本质上仅仅是HDFS的一个目录,数据库中的表将会以这个数据库目录子目录的形式存储;

3、如果用户没有指定数据库,Hive将会使用默认的数据库default;

4、数据库的目录位于属性hive.metastore.warehouse.dir所指定的顶层目录之后;

5、数据库的文件目录名是以.db结尾的;

6、Hive的master-server依据hadoop配置文件中fs.default.name作为master-server的服务器名和端口号;

7、默认情况下,Hive不允许删除一个包含表的数据库,如果需要级联删除,需加关键字cascade;

8、Hive中关键字table和schema是含义相同;

9、Hive会自动为表增加两个属性:last_modified_by和last_modified_time;

A、如果不想使用默认表路径,为了避免产生混淆,建议使用外部表;

B、相对于外部表而言是管理表,Hive会控制着管理表的数据的生命周期;Hive删除管理表会同时删除其数据,如果和其他工作共享数据,则使用外部表,创建一个表指向外部数据,但是并不需要对其具有所有权,删除表时并不会删除数据;

C、防止用户对分区表误操作,没有添加分区过滤器(where谓词)从而触发一个巨大的MapReduce任务,可以将Hive设置为'strict'模式:set hive.mapred.mode=strict;

D、用户不小心删除的数据会移到用户根目录的.Trash目录下,不过需要配置fs.trash.interval=1440,1440是回收站检查点的时间间隔,单位是分钟;

注:博客中关键字用的是小写,实际中大家最好都使用大写,以作区别;

创建一个数据库:

    create database test_db comment 'test datebase';

    create database if not exists test_db comment 'test database';

    create database test_db location '/hive/db/directory';

    create database test_db with dbproperties ('creator' = 'user', 'date' = '20151010');

查看数据库:

    show databases;

    show databases like 'h.*';

查看具体数据库:

    describe database test_db;

    describe database extended test_db;

切换数据库:

    use test_db;

删除数据库:

    drop database if exists test_db;

    drop database if exists test_db cascade;

Hive中不允许更改数据库的元数据信息,唯一可改变的就是添加数据库的dbproperties值:

    alter database test_db set dbproperties ('key' = 'value');

在Hive cli提示符中显示db名:

    set hive.cli.print.current.db=true;

显示数据库中的表:

    show tables;

    show tables in test_db;

    show tables 'test.*';

    注:in test_db和使用正则表达式功能暂不支持同时使用;

创建表:

    create table [if not exists] [test_db.]test_table (

        stringfield string [comment 'string field'],

        floatfield float [comment 'float field'],

        arrayfield array<string> [comment 'array field'],

        mapfield map<string, float> [comment 'map field'],

        structfield struct<field1:string, field2:int> [comment 'struct field']

    )

    [comment 'description of the table']

    [tblproperties ('key1' = 'value1', 'key2' = 'value2')]

    [row format delimited

    fields terminated by '\001'

    collection items terminated by '\002'

    map keys terminated by '\003']

    [lines terminated by '\n']

    [stored as textfile]

    [location '/hive/table/directory'];

    create table [if not exists] [test_db.]test_table like [test_db.]other_table [location '/hive/table/directory'];

v0.10.0版本查看表tblproperties:

    show tblproperties test_table;

查看表详细信息:

    desc test_table;

    describe extended test_table;

    describe formatted test_table;

只查看表中某一列的信息:

    describe test_db.test_table.column;

创建外部表:

    create external table if not exists test_table (

        name string

    )

    location '/hive/external/table/directory';

    create external table test_table like other_tablelocation '/hive/external/table/directory';

创建分区表:

    create table test_table (

        name string

    )

    partitioned by (pt_key1 string, pt_key2 int);

    分区改变了Hive对数据的存储方式,如果没有分区存储路径为:hdfs://master-server/.../hive_workdir/hive_warehouse/test_db.db/test_table

    使用分区后路径为:.../test_db.db/test_table/pt_key1=value1/pt_key2=value2

    分区字段与普通字段一样,可以直接查询,但是在where中只用分区字段可以提供查询性能(分区字段的用途!);

查看表中的分区:

    show partitions test_table;

    show partitions test_table partition (pt_key1='value');

    describe extended test_table;

load数据到分区表:

    load data local inpath '${env:HOME}/file' into table test_table partition (pt_key1 = 'value1', pt_key2 = 'value2');

可以用alter table的方式为一个分区表添加分区:

    alter table test_table add partition (pt_key1=value1, pt_key2=value2) location 'hdfs://master-server/data/value1/value2';

    注意该路径,可以不使用Hive分区表的路径命名习惯;使用该方式类似于外部表,表删除,数据也不会跟着删除;

    alter同时可以更改分区对应的目录地址:

    alter table test_table partition (pt_key1=value1, pt_key2=value2) set location 'hdfs://new/path';

    describe extended test_table会显示所有的分区键,同时只能显示默认的存储路径,如果需要查看上述方式创建的特定分区所在的路径只能通过以下命令:

    describe extended test-table(pt_key1=value1, pt_key2=value2);

自定义表的存储格式:

    Hive的默认存储格式是文本文件格式textfile,用户还可以使用stored as语句设置其他Hive所支持的内置文件格式,包括sequencefile和rcfile,这两种格式都是使用二进制编码和压缩(可选)来优化磁盘空间使用以及IO带宽性能的;对于记录是如何编码成文件的,是通过InputFormat对象控制的,例如TextInputFormat;记录的解析是由序列化/反序列化器SerDe控制的,比如LazySimpleSerDe;Hive还有OutputFormat控制将查询结果输出到文件中或者控制台,比如HiveIgnoreKeyTextOutputFormat。

    通俗的讲,Hive使用一个InputFormat对象将输入流分割成记录,然后使用一个OutputFormat对象来将记录格式化为输出流,在使用一个SerDe在读数据时将记录解析成列,在写数据时将列编码成记录。这三个对象用户都可以进行自定义:

    create table test_table (

        name string

    )

    partitioned by (pt_key string)

    row format serde 'com.your.own.ExampleSerDe'

    with serdeproperties ('schema.url' = 'http://www.example.com')    -- 传递配置信息给SerDe

    stored as

    inputformat 'com.your.own.ExampleInputFormat'

    outputformat 'com.your.own.ExampleOutputFormat';

删除表:

    drop table if exists test_table;

重命名表:

    alter table test_table rename to new_table;

增加、修改和删除分区:

    alter table test_table add partition ... location ... partition ... location ...

    alter table test_table partition(...) set location ...

    alter table test_table drop if exists partition(...);

修改列信息:

    alter table test_table change column old_field new_field string comment '' after other_field (first);

    alter table test_table add columns (new_field1 string comment '', new_field2 int comment '');

重新定义表数据列:

    alter table test_table replace columns (

        new_field1 string comment '',

        new_field2 int comment ''

    );

用户只能增加表属性或者修改已存在属性,并不能删除属性:

    alter table test_table set tbproperties ('new_prop' = 'new value');

修改表存储属性:

    alter table test_table partition(...) set fileformat sequencefile;

其他一些有用的修改表语句(只支持分区表):

    alter table test_table archive partition (...);

    将指定分区内的文件打包成一个Hadoop压缩HAR包,这样仅仅可以降低文件系统中的文件数,减轻NameNode的压力,而不会减少任何的存储空间;

    alter table test_table unarchive partition (...);

    alter table test_table partition(...) enable no_drop; 防止分区被查询

    alter table test_table partition(...) enable offline; 防止分区被查询


Hive支持DDL操作大概就这些,日常工作基本够用了...


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值