DDL数据定义

1 创建数据库

1.创建命令

CREATE DATABASE [IF NOT EXISTS] database_name

[COMMENT database_comment]

[LOCATION hdfs_path];

注:Impala不支持WITH DBPROPERTIE…语法

2. 错误演示

DDL数据定义

2查询数据库

2.1显示数据库

DDL数据定义

2.2删除数据库

[hadoop103:21000] > drop database hive_db;

[hadoop103:21000] > drop database hive_db cascade;

注:

Impala不支持alter database语法

当数据库被 USE 语句选中时,无法删除

3创建表

3.1 管理表

DDL数据定义

3.2 外部表

[hadoop103:21000] > create external table stu_external(

                  > id int,

                  > name string)

                  > row format delimited fields terminated by ‘\t’ ;

4分区表

4.1 创建分区表

[hadoop103:21000] > create table stu_par(id int, name string)

                  > partitioned by (month string)

                  > row format delimited

                  > fields terminated by ‘\t’;

4.2 向表中导入数据

[hadoop103:21000] > alter table stu_par add partition (month=’201810′);

[hadoop103:21000] > load data inpath ‘/student.txt’ into table stu_par partition(month=’201810′);

[hadoop103:21000] > insert into table stu_par partition (month = ‘201811’)

                  > select * from student;

注意:

如果分区没有,load data导入数据时,不能自动创建分区。

4.3 查询分区表中的数据

[hadoop103:21000] > select * from stu_par where month = ‘201811’;

4.4 增加多个分区

[hadoop103:21000] > alter table stu_par add partition (month=’201812′) partition (month=’201813′);

4.5 删除分区

[hadoop103:21000] >  alter table stu_par drop partition (month=’201812′);

4.5查看分区

[hadoop103:21000] > show partitions stu_par;

大数据培训