创建数据库与创建数据库表
创建数据库操作
创建数据库
create database if not exists myhive;
use myhive;
创建数据库并指定hdfs存储位置
create database myhive2 location ‘/myhive’;
修改数据库
数据库的元数据信息是不可更改的,包括数据库名称以及数据库所在位置
alter database myhive2 set dbproperties(‘createtiome’=‘20201119’);
查看数据基本信息
desc database myhive2;
查看数据更多详细信息
desc database extended myhive2;
删除数据库
删除一个空数据库,如果有数据库下面有数据表,那么就会报错
drop database myhive2;
强制删除数据库,包含数据库下面的表(谨慎谨慎再谨慎)
drop database myhive cascade;
创建数据库表的操作
管理表
创建表并指定字段之间的分隔符
create table if not exists stu(id int,name string) row format delimited fields terminated by ‘\t’
insert into stu2 values (1,“zhangsan”);
insert into stu2 values (2,“lisi”);
insert into stu2 values (3,“wangwu”);
根据查询结果创建表
create table stu1 as select * from stu;
根据已经存在的表结构创建表
create table stu3 like stu2;
查询表的类型
desc formatted stu3;
若使用drop [database] if exists [database Name]语句删除数据库,若数据库中有表,则无法删除;如果想强制删除,则可以使用语句:drop database dblab cascade;
外部表
内部表和外部表的区别:
内部表在删除表时会将表与数据一起删除,外部表在删除表时,数据仍然存放在hdfs中,不会删掉。
创建表
create
external
table 表名 (字段1 类型1,字段2 类型2) row format delimited fields terminated by ‘分隔符’;
从本地文件系统向表中加载数据
load data local inpath ‘数据绝对路径’ into table 表名;
加载数据并覆盖已有的数据
load data local inpath ‘数据绝对路径’
overwrite
into table 表名;
从hdfs文件系统向表中加载数据(需要提前将数据上传到hdfs文件系统,其实就是一个移动文件的操作)
load data inpath ‘数据绝对路径’ into table 表名;
如果删除表,hdfs的数据仍然存在,并且重新创建表之后,表中就直接存在数据
分区表
创建分区表语法
create table 表名 (字段1 类型,字段2 类型)
partitioned by(year string)
row format delimited fields terminated by ‘分割符’;
创建一个表带多个分区
create table 表名 (字段1 类型,字段2 类型)
partitioned by(year string,month string,day string)
row format delimited fields terminated by ‘分割符’;
加载数据到分区表中
load data local inpath ‘数据路径’ into table 表名
partition(year='2020')
;
加载数据到一个多个分区表中
load data local inpath ‘数据路径’ into table 表名
partition(year='2020',month='11',day='20')
;
多分区联合查询使用union all 来实现
select * from 表1 where day=‘20’ union all select * from 表2 where day=‘19’;
查看分区
show partitions 表名;
添加多个分区
alter table 表名 add partition(month=‘202006’)
partition (month=‘202007’)
删除分区
alter table 表名 drop partition(month=‘202006’);
修复数据
注意:修复数据前需要在hdfs上将数据放在对应目录下
自动修复
msck repair table 表名;
手动修复
alter table 表名 add partition(month=‘202006’)
分桶表
开启hive的桶表功能
set hive.enforce.bucketing=true;
设置reduce的个数
set mapreduce.job.reduces=3
创建桶表
create table 表名 (字段,类型)
clustered by(字段) into 3 buckets
row format delimited fields terminated by ‘\t’;
==注意==:桶表的数据加载,只能通过insert overwrite。hdfs dfs -put文件或者通过load data无法加载
创建普通表
create table 表名(字段 类型) row format delimited fields terminated by ‘\t’;
普通表中加载数据
load data local inpath ‘数据路径’ into table 表名;
通过insert overwrite 给桶表中加载数据
insert overwrite table 桶表名select * from 普通表名cluster by(字段);
修改表
表重命名
alter table 表名
rename to
新表名;
增加/修改列信息
(1)查询表结构
desc 表名;
(2)添加列
alter table 表名add columns (字段1 类型, 字段2 类型)
;
(3)查询表结构
desc 表名;
(4)更新列
alter table 表名change column 字段1 字段2 int
;
(5)查询表结构
desc 表名;
删除表
drop table 表名;
hive 表中加载数据
导入数据
直接向分区表中插入数据
insert into table 表名 partition(month=‘202006’) values(数据)
通过查询插入数据
通过load方式加载数据 load data local inpath ‘数据路径’ overwrite into table 表名
partition(month=‘201806’);通过查询方式加载数据 create table 表名1 like 表名2;
insert overwrite table 表名1 partition(month = ‘202006’) select 字段,字段,字段
from 表名2;
多插入模式
from 表名
insert overwrite table 表名1 partition(month=‘201806’) select s_id,c_id
insert overwrite table 表名2 partition(month = ‘201806’) select c_id,s_score;
查询语句中创建表并加载数据(as select)
create table 表名1 as select * from 表名2 ;
创建表时通过location指定加载数据路径
create external table 表名 (s_id string,c_id string,s_score int) row format delimited fields terminated by ‘\t’ location ‘hdfs上的数据路径’;
import 导入 hive表数据(内部表操作)
import table 表名 from ‘数据路径’;
导出数据
将查询的结果格式化导出到本地
insert overwrite
local
directory ‘路径’ row format delimited fields terminated by ‘\t’ select * from student;
清空表数据
只能清空管理表,也就是内部表
truncate table 表名;
恢复表数据
在删除外部表后,创建以前的表结构
恢复数据:msck repair table 表名;