再见以前说再见 大数据存储复习

创建一个数据库,数据库在HDFS上的默认存储路径是:
/user/hive/warehouse/*.db

.
创建数据库:
create database if not exists school;

.
过滤显示查询的数据库:
show databases like ‘db_hive*’;

.
显示数据库信息:
desc database school;

.
显示数据库详细信息,extended
desc database extended school;

.
修改数据库:
用户可以使用 ALTER DATABASE 命令为某个数据库的 DBPROPERTIES 设置键-值对属性值,来描述这个数据库的属性信息。数据库的很多元数据信息是不可更改的,包括数据库名和数据库所在的目录位置。

alter database school set dbproperties(‘createtime’=‘20200407’);

这是一个用于修改数据库属性的 SQL 语句,它将名为 “school” 的数据库的创建时间属性设置为 “20200407”。

.
如果数据库不为空,可以采用 cascade 命令,强制删除
drop database school cascade;

.
EXTERNAL 关键字可以让用户创建一个外部表

.
COMMENT:为表和列添加注释。

.
PARTITIONED BY 创建分区表

.
CLUSTERED BY 创建分桶表

.
LOCATION :指定表在HDFS上的存储位置。

.
普通创建管理表:

create table if not exists student2(
id int, name string
)
row format delimited fields terminated by '\t'
stored as textfile
location '/user/hive/warehouse/student2';

.
根据查询结果创建表(查询的结果会添加到新创建的表中):
create table if not exists student3 as select id, name from student2;

.
根据已经存在的表结构创建表:
create table if not exists student4 like student2;

.
查询表的类型:
desc formatted student2;

.
普通创建外部表:

create external table if not exists ext_stu(
id String,
name String,
age int
)
row format delimited fields terminated by ','
location '/usr/test_data/';

.
修改外部表student2为内部表:

alter table ext_stu set tblproperties(‘EXTERNAL’=‘FALSE’);

(‘EXTERNAL’=‘TRUE’)和(‘EXTERNAL’=‘FALSE’)为固定写法,区分大小写!

.
创建分区表语法:

create table stu_partition(
name String, Phone_NO String
)
partitioned by (class String)
row format delimited fields terminated by '\t';

.
加载数据到分区表中:

load data local inpath '/usr/test_data/dsj-1.txt' into table school.stu_partition partition(class='dsj-1');

.
多分区联合查询:

select * from stu_partition where class='dsj-1'
union
select * from stu_partition where class='dsj-2';

.
创建单个分区:

alter table stu_partition add partition(class=‘dsj-1’);

同时创建多个分区:

alter table stu_partition add partition(class=‘dsj-2’) partition(class=‘dsj-3’);

.
查看分区:
show partitions stu_partition;

.
删除分区:
alter table stu_partition drop partition(class=‘dsj-1’);
alter table stu_partition drop partition(class=‘dsj-2’),partition(class=‘dsj-3’);

.
查看分区表有多少分区:
show partitions stu_partition;

.
创建二级分区表:

create table stu_partition2(
name String, Phone_NO String
)
partitioned by (class String,year int)
row format delimited fields terminated by '\t';

.
二级分区加载数据示例:
加载数据到二级分区表中:

load data local inpath '/usr/test_data/dsj-1.txt' into table 
school.stu_partition2 partition(class='dsj-1',year='2018'); 

查询分区数据:

select * from school.stu_partition2 where class='dsj-1' 
and year='2018';

.
重命名表:
ALTER TABLE table_name RENAME TO new_table_name

.
修改列:

  • 增加列:alter table stu2 add columns (pid int) ;
  • 修改列:alter table stu2 change pid tab_id int ;
  • 替换列:alter table stu2 replace columns (id String, pname
    String,tab_id String); 会替换所有列,慎用!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

亖嘁

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值