Hive-表和库相关操作

表和库相关操作

--------------------------------库-----------------------------------------------

--创建库
create database 
if not exists practice
location "/hive/warehouse/practice";
--#创建库(赋予库属性)
create database 
if not exists demo
location '/user/demodb'
with dbproperties('name'='yire','age'='22');

--删除库,default库不能删除
drop database practice;
--删除库(库中有表 - cascade:级联删除)
drop database 库名 cascade;

--修改库的属性
alter database 库名 set dbproperties('key'='value');

--查看创建库的语句
show create database 库名;
--#查看库信息
desc database practice;
desc database extended practice;
--查看当前库
select current_database()

--------------------------------表-----------------------------------------------

--创建表(最简单)
create table if not exists person(
id int,
name string
)
row format delimited fields terminated by '\t';

--创建表(复杂字段类型,定义怎么分割,并且是外部表)
create table test(
name string,
friends array<string>,
children map<string,int>,
address struct<street:string,
city:string,
email:int>) 
row format delimited fields terminated by ','
collection items terminated by '_'
map keys terminated by '_'
lines terminated by '\n';

--创建外部表(external定义为外部表,comment 为字段描述,tblproperties为表的属性)
create external table if not exists person(
id int comment 'this is id',
name string comment 'this is name'
)
comment 'this is table'
row format delimited fields terminated by ' '
location '/demo'
tblproperties('version'='5.1');

--查看创建表的语句
show create table 表名;
--查看表信息(字段,字段类型,字段的描述信息)
desc 表名;
--查看表的详细信息
desc formatted 表名;

--内部表和外部表的转换:'EXTERNAL'='TRUE'必须为大写
--EXTERNAL的值为TRUE-外部表/FALSE-内部表
alter table 表名 set tblproperties('EXTERNAL'='TRUE');
--修改表名称
alter table 表名 rename to 新表名
--修改字段的名称或类型
--注意:字段的类型和原来的类型是否可以隐式转换如果不能则会失败
alter table person change column id newId int COMMENT 'this is id';
--添加和替换列的字段
ALTER TABLE table_name ADD|REPLACE COLUMNS .......
--注意(REPLACE):
--	1.替换的字段的类型和原来的类型是否可以隐式转换如果不能则会失败
--	2.替换的时候是从第一个字段开始依次替换。如果替换的字段少于被替换的字段
--		那么原字段将缺失。

--删除表
drop table dept;
--清空表,只能清空内部表
truncate table 表名

--------------------------------分区表-----------------------------------------------

--创建
create table  if not exists pt(
id int
)
partitioned by (day string) --partitioned by (day string,hour string) 二级分区
location '/yire/partition'
--导入
load data local inpath '/home/fang/yire/1' into table pt partition(day='01');
--查看分区表有多少分区
show partitions dept_partition;
--创建分区(多个分区之间不能有逗号)
alter table dept_partition add partition(day='20200405') partition(day='20200406');
--同时删除多个分区(分区之间必须有逗号)
alter table dept_partition drop partition (day='20200404'), partition(day='20200405');
--修复数据 (方式二为alter 创建分区,方式三为load data 进表)
msck repair table pt

--从别的表插入数据
insert into table pt 
select deptno, dname, loc from dept;

--------------------------------分桶表-----------------------------------------------

--创建
create table if not exists bt(
id int,name string
)
clustered by (id)
into 4 buckets
row format delimited fields terminated by '\t'
location '/yire/cluster'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿-瑞瑞

打赏不准超过你的一半工资哦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值