一、数据库操作
- 创建数据库
创建数据库myhive
create database if not exists myhive;
2.创建数据库并指定位置
create database myhive2 location '/myhive2';
- 选择操作某数据库
use myhive;
- 创建表
create table t_test(id int , name string);
在hive内,数据库与数据库中的表是以目录的形式存在的。
- 创建数据库键值对信息
数据库可以有一些描述性的键值对信息,在创建时添加:
create database foo with dbproperties ('owner'='itcast','date'='20210518');
- 查看数据库的键值对信息
describe database extended foo;
- 修改数据库的键值对信息
alter database foo set dbproperties ('itcast'='itheima');
- 查看数据库更多详细信息
desc database extended myhive2;
- 删除数据库
删除空的数据库
drop database myhive2;
强制删除数据库(包括数据库下的表)
drop database myhive cascade;
二、数据库表操作
1. 创建表的语法:
create [external] table [if not exists] table_name (
col_name data_type [comment '字段描述信息']
col_name data_type [comment '字段描述信息'])
[comment '表的描述信息']
[partitioned by (col_name data_type,...)]
[clustered by (col_name,col_name,...)]
[sorted by (col_name [asc|desc],...) into num_buckets buckets]
[row format row_format]
[storted as ...]
[location '指定表的路径']
2. 内部表的操作
- 建表:
use myhive;
create table stu(id int,name string);
insert into stu vaalues(1,"zhangsan"); #插入数据
select * from stu;
- 创建表并指定字段之间的分隔符
hive 表文件字段之间的分隔符
create table if not exists stu2(id int,name string) row format delimited fields terminated by '\t';
- 创建表并指定文件的存放路径
create table if not exists stu2(id int,name string) row format dalimited fields terminated by '\t' location '/user/stu2';
- 根据查询结果创建表
create table stu3 as select * from stu2; # 通过复制表结构和标内容创建新表
- 根据已经存在的表结构创建表
create table stu4 like stu;
- 查询表的详细信息
desc formatted stu2;
- 删除表
drop table stu4;
3. 外部表的操作
外部表的数据一般用于共享。删除hive表时,数据仍然存放在hdfs中,不会删掉。
- 创建外部表:
create external table teacher (t_id string, t_name string) row format delimited fields terminated by '\t';
- 加载数据(本地加载)
- 方法一(不建议使用)
hive> insert into stu values(1,'zhangzan');
- 方法二:
对文件做复制操作
load data local inpath '/export/servers/hivedatas/student.csv' into table student;
- 加载数据并覆盖已有数据
load data local inpath '/export/servers/hivedatas/student.csv' overwrite into table student;
- 从hdfs文件系统向表中加载数据(需要提前将数据上传到hdfs文件系统)
hive可以将hdfs文件映射成一张表。(依靠hive的元数据)
hdfs dfs -put teacher.csv /hivedatas/
load data inpath 'hivedatas/teacher.csv' into table teacher;
对文件做剪切操作
- 表的修复(建立表与数据文件之间的一个关系映射)
msck repair table score4;
4.分区表的操作
将大的hive表文件分切成一个个小的文件,存放在不同的文件夹中。
- 创建分区表语法
create table score(s_id string,c_id string,s_score int) partitioned by (month string) row format delimited field terminated by '\t';
- 创建一个表带多个分区(创建多级文件夹)
create table score2(s_id string,c_id string,s_score int) partitioned by (year string,month string,day string) row format delimited field terminated by '\t';
- 加载数据到分区表中
load data local inpath '/export/servers/hivedatas/score.csv' into table score partition (month='202105');
- 加载数据到多分区表中
load data local inpath '/export/servers/hivedatas/score.csv' into table score2 partition (year='2021'month='05',day='18');
- 多分区表联合查询(使用 union all)
select * from score where month = '2021' union all select * from score where month = '202105';
- 查看分区
show partitions score;
- 添加一个分区
alter table score add partition(month = '202105');
- 删除分区
alter table score drop partition(month = '202105');
5. 分桶表操作
分桶:将数据按照指定的字段划分到多个文件当中去(即MapReduce中的分区)。
可避免单个表文件数据过大。
查询时会将所有数据输出。
- 开启hive的分桶功能
set hive.enforce.bucketing=true;
- 设置reduce个数
set mapreduce.job.reduces=3;
- 创建分桶表
create table course (c_id string,c_name string,t_id string) clustered by(c_id) into 3 buckets row format delimited fields terminated by '\t';
- 创建普通表(做中间表)
create table course_common (c_id string,c_name string,t_id string) row format delimited fields terminated by '\t';
- 普通表中加载数据
load data local inpath '/export/servers/hivedatas/course.csv' into table course_common;
- 通过insert overwrite 给通表中加载数据
insert overwrite table course select * from course_common cluster by(c_id);
6.修改表结构
- 重命名
alter table old_table_name rename to new_table_name;
- 增加/修改列信息
- 查询表结构
desc score5;
- 添加列
alter table first add columns (mycol string,mysco int);
- 更新列
alter table first change column mycol mycolnew string;
- 删除表(略)