Hive DDL

Hive 库操作

创建数据库

$ create database hive_db;
$ create table hive_db.test(id int);
指定位置
$ create database hive_db2 location '/hive_db2.db';
判断不存在
$ create database if not exists hive_db;

删除数据库

$ drop database hive_db2;
判断不存在
$ drop database if exists db_hive2;
数据库不为空,级联(cascade)删除。
$ drop database hive_db cascade;

修改数据库信息

$ alter database hive_db set dbproperties("CTtime"="2020-03-26");
$ desc database extended hive_db;

数据库信息

$ desc database hive_db;
$ desc database extended hive_db;

Hive 表操作

内部表
又称管理表。

  • Hive会控制数据的生命周期。
  • 内部表数据存储在hive.metastore.warehouse.dir(例如 /user/hive/warehouse)所定义的目录下。
  • 删除内部表也会删除表中数据。

外部表

  • 删除该表并不会删掉这份数据,只删除掉元数据。

分区表

  • Hive中的分区就是分目录。将大数据集分割成小数据集。
  • 查询时通过WHERE选择指定分区。

桶表

  • 分区表物理上是文件夹字段+数据文件字段,而桶表就是讲数据文件分割。

创建表

1. hive> create table student(id int, name string);
2. hive> create table emp(id int,name string)  row format delimited fields terminated by '\t';
3. hive> create table test(
                name string,
                friends array,
                children map,
                address struct
            )
            row format delimited
            fields terminated by ','
            collection items terminated by '_'
            map keys terminated by ':'
            lines terminated by '\n';
4. hive> create table test1 as select * from test;
5. create external table .....
6. location '路径',指定数据位置。

7. 创建分区表,数据文件中没有分区列,分区列是插入数据时确定的。
  1 baozi1
  2 baozi2
  3 baozi3
hive> create table stu_partition(id int, name string)
    partitioned by(month string)
    row format delimited fields terminated by ' ';
hive> load data local inpath '/home/user000/data/student.txt' into table stu_partition partition(month="202001");
hive> load data local inpath '/home/user000/data/student.txt' into table stu_partition partition(month="202002");

8. 创建桶表,桶表不会新增列,而是对原有列进行分桶。
  1001 ss1
  1002 ss2
  1003 ss3
  1004 ss4
  1005 ss5
  1006 ss6
  1007 ss7
  1008 ss8
  1009 ss9
  1010 ss10
  1011 ss11
  1012 ss12
  1013 ss13
  1014 ss14
  1015 ss15
  1016 ss16
hive> create table stu_bucket(id int, name string)
    clustered by(id)  into 4 buckets
    row format delimited fields terminated by ' ';
使用load data不会分桶,使用insert。
hive> set hive.enforce.bucketing = true;
hive> set mapreduce.job.reduces=-1;
hive> create table stu(id int, name string)
    row format delimited fields terminated by ' ';
hive> load data local inpath '/home/user000/data/student.txt' into table stu;
hive> insert into table stu_bucket select * from stu;
hive> select * from stu_bucket;

删除表

语法:drop table 表;
示例:hive> drop table test1;

修改表

内部表与外部表相互转换

语法:alter table 表 set tblproperties('EXTERNAL'='TRUE/FALSE');
示例:hive> alter table test set tblproperties('EXTERNAL'='FALSE');

分区表-添加分区

语法:alter table 表 add partition(列=值) [partition(列=值)  ... ];
例如:hive> alter table stu_partition add partition(month="202003");
     hive> alter table stu_partition add partition(month="202004") partition(month="202005");

分区表-删除分区

语法:alter table 表 drop partition(列=值) [, partition(列=值) , ... ];
例如:hive> alter table stu_partition drop partition(month="202003");
     hive> alter table stu_partition drop partition(month="202004") , partition(month="202005");

分区表-修复分区
手动创建分区文件夹并上传数据,但是Hive缺少对应元数据。

语法:
  msck repair table 表;
  alter table 表 add partition(分区文件夹);
  load data local inpath  ... ... partition(分区文件夹)
示例:略。

重命名

语法:alter table 表 rename to 新表名
示例:略。

更新列

语法:alter table 表 change  [COLUMN]  旧列 新列名 列类型 [COMMENT 描述] [FIRST|AFTER 列名]
示例:hive> alter table student change column id stu_id string;

添加列

语法:alter table 表 add columns (列名 类型 [COMMENT 描述] , ...)
示例:略。

替换列

替换所有列,抛弃原本所有的列,保留替换的列。
语法:alter table 表 replace columns (列名 类型 [COMMENT 描述] , ...)
示例:略。

你可能感兴趣的:(Hive DDL)