一、增删改查数据库
1)增
create database[if not exists] database_name
[comment 'beizhu']
[location 'hdfs_path'] //指定存储路径,默认为/user/hive/warehouse/database_name.db
[with dbproperties(property_name=property_value)]//一般都是创建时间
2)删
drop database databases_name [cascade]//加上cascade可以清空全部数据,而不加的话,只能删除空的数据库(即数据库中没有数据表)
二、增删改查数据表
1)增
create [external] table table_name
([columns id idtype])
[partitioned by(columns id idtype)]
[clustered by (columns id )]
[stored by (id(desc|asc)) into num_buckets buckets]
[row format delimited]
[stored as text_format]
[location 'hdfs_inpath']
[tblproperties property_name=proper_value ...];
[as sql]
hive中表分为两类,一类为外部表,一类为内部表(管理表)
一、内部表
内部表:在创建 表的语句中 不加 external ,创建的就是内部表
内部表一般都是作为案例来使用,或者说存储一些临时数据,内部表的数据不能被多个表共享(含义是:表中数据会随着表的删除而被删除)
二、外部表
外部表:外部表数据一般都是会被共享,或在多个阶段进行使用,当表被删除时,表中数据并不会被删除,
内部表和外部表的相互转换
在meatstore里面存有每个表的相关信息,当内部表想要转换为外部表时
通过
alter tabl table_name set tblproperties('EXTERNAL'='TRUE');
alter tabl table_name set tblproperties('EXTERNAL'='FALSE');
2)修改表
alter table table_name replace old_name new_name new_type
// alter table test id stu_id int; 替换列,可以多对一
alter table table_name add new_name new_type
//alter table test stu_id int; 可以同时增加多列
alter table table_name change old_name new_name new_type
//alter table test change id stu_id string