HIve编程指南借--借鉴于 O`REILLY Hive 编程指南 人民邮电出版社
数据定义
库
描述信息
创建数据库并添加描述信息
create database test
comment "this is a test";
添加键值对描述信息
create database test
with dbproperties ('creator'='test db','date'='2021-4-23');
查看数据库的描述信息
describe database test;
或
describe database extended test;
表
查看表结构信息
describe extended my.user;
formatted 可以替代 extended 可提供更加可读和长的输出信息
向表添加字段以及描述
alter table bm_itv_order_statis_analyse_d add columns(other_price bigint,new_renew_price bigint);
alter table bm_itv_order_statis_analyse_d change column total_price total_price bigint COMMENT '总收入';
alter table bm_itv_order_statis_analyse_d change column new_renew_price new_renew_price bigint COMMENT '新增续订收入';
查看某列的信息
describe my.user;name;
管理表(外部表)
默认情况下hive会将这些表的数据存储由配置项hive.meatstore.warehouse.dir 所定义的目录的子目录下(/user/hive/warehouse)
当删除管理表时hive会删除表内数据
外部表
用于读取数据文件
读取 'data/stocks‘下 以 ’,‘ 分隔的数据
create external table if no exists user(
name string,
age int,
sex string)
row format delimited fields terminated by ','
locatilon 'data/stocks'
删除外部表不会删除原数据 但是描述表的元数据信息会被删除
对于管理表,可以对存在的一张表进行表结构的复制
create external table if not exists mydb.tmp_user
like mydb.user
location '/path/to/data'
分区表 (分区字段不能和表字段一样)
管理表
按照country和state来对数据进行分区
create table employees(
name string,
salary float,
address struck<street:string,city:string,state:string,zip:int)
partitioned by (country string,state string)
分区表改变了hive对数据存储的组织方式,如果在mydb内创建这个分区表,则会有一个对应的employees目录对应 /user/hive/warehouse/mydb.db/employees
查看表内所有分区
show partitions employees;
查看某个特定分区下的分区
show partitions employees partition (country=‘us’); 查位于country=‘us’ 下的所有分区
通过载入数据的方式在管理表创建分区
从本地目录$HOME}/c_employees 载入数据到employees表 hive会创建对应分区目录 /employees/country=us/state=ca $HOME}/c_employees这个目录下的数据将会被拷贝到上述分区的目录下
load data local inpath '${env:HOME}/c_employees'
into table employees
partition (country='us',state='ca');
外部分区表
create table if not exists log_messages(
hms int,
severity string,
sever fstring,
process_id int,
message string)
partitioned by (year int,month int,day int)
row format delimited fields terminated by '\t';
向分区表添加一个特定的分区
alter table log_message add partition(year=2021,month=3,day=2)
location '/data/log_message/2021/3/2';
修改表
alter table 只会修改元数据不会修改数据本身
修改表名
alter table user rename to user_test;
修改表属性(表属性可以修改但无法删除)
增加修改删除表分区
增加 alter table user add if not exists
partition (year=2021,month=2,day=2) location 'logs/2021/2/2'
partition (year=2021,month=2,day=3) location 'logs/2021/2/3'
对于管理表 添加分区,分区内的数据会同时和元数据信息一起删除
对于外部表,分区内数据不会删除
移动位置来修改分区路径
alter table user partition(year=2021,month=2,day=5)
set location '.logs