hive的ddl操作
库:
修改数据库不支持
创建数据库:create database mading;
切换数据库:use mading;
查看数据库列表:show databases;
//模糊查询:show databases like 'test*';
查看正在使用的数据库:show current_database();
查看数据库的详细信息:desc database mading;
删除数据库:drop database mading;//只能删除空数据库
drop database cascade mading;// 可以删除非空数据库
表:
1.
external 外部表 ,删除时只删除元数据,保留原始数据,建表指定表的存储目录
managed 内部表 ,默认,删除时和元数据一起,建表用默认路径可以
2.
分区:为了提高查询性能,对庞大的表进行切分,把经常查询的字段作为分区字段,把数据划分成不同的数据目录,这样就避免的全表扫描,
分桶:1可以提升抽样效率,2可以提升join性能。相对于分区表颗粒度更细,按照分桶字段划分为不同的文件,指定分桶字段和分桶个数,分桶字段.hashCode&分桶个数
3.
fields terminated by 指定列分隔符
lines terminated by 指定行分隔符
items terminated by 指定集合中元素的分隔符
查看表的描述信息
desc 表名
desc extended 表名
desc formatted 表名
查看表的列表信息
show tables;
show tables in database_1;//查看数据库中的表
show tables like '';//模糊查询
修改表
1.修改表名:
alter table 表名 rename to 新表名
2.修改表字段
alter table 表名 change 字段 新字段 类型;
3.增加表字段
alter table 表名 add columns (score int);
4.删除表字段
alter table 表名 delete score;
5.替换全表的所有字段
alter table 表名 replace columns(xxx string);
6. 清空表数据,但是会保留表结构
truncate table stu;
7.
加载数据到表
load data inpath '/stuin' into table stu;
建表案例
950003,刘梓晨,女,17,IS
1.建内部表
create table if not exsits stu(id int,name string,sex string,age int comment '16-24',department string) comment 'mystudent' row format delimited fields terminated by ',' stored as textfile location '/user/stu';
2.建外部表
create external table if not exsits stu(id int,name string,sex string,age int comment '16-24',department string) comment 'mystudent' row format delimited fields terminated by ',' stored as textfile location '/user/stu';
3.建一个分区表
分区字段一定不能是建表语句中的字段
create table stu(id int,name string,sex string,department string) partitioned by(age int) row format delimited fields terminated by ',';
添加分区的语法:
alter table stu add if not exsits partition(age=18);
修改分区的存储路径
alter table stu partition (age =17) set location '';
删除分区
alter table stu drop if exsits partition (age =17);
4.建一个分桶表
create table stu(id int,name string,sex string,department string) partitioned by(age int) clustered by (name) sorted by (age desc) into 8 buckets row format delimited fields terminated by ',';
5.ctas 建表语句
create table as select ... from ...
6.表复制
like关键字
只会复制表结构,不会复制表数据
create table stu1 like stu;
其他辅助命令
show functions;//查看hive函数列表