Hive表介绍
Hive中表格共有4类,分别是内部表、外部表、分区表与桶表
部分符号释义: “[ ]”表示该部分内容可以出现,或者不出现 “( )”表示该部分内容必须出现 “A|B”表示该部分内容中A或者B只能出现一个
基本句法-创建新表:
create [external] table [if not exist] table_name
[(
col_name data_type [comment col_coment],
...
)]
[comment table_comment]
[partitioned by
(col_name data_type [comment col_comment]),
...
]
[clustered by (col_name, ...)
[sorted by (col_name [asc|desc], ...)]
into num_buckets buckets
]
[row format delimited
[fields terminated by char]
[collection items terminated by char]
[map keys terminated by char]
[lines terminated by char]
]|[row format serde serde_name
[with serdeproperties(
property_name = property_value,
...
)]
]
[stored as file_format]
[location hdfs_path]
lifecycle 7;
Demo运行(以实际使用中的常用句法为编写规范):
create table if not exists base_table(
car_id string comment 'id of car',
car_price double comment 'price of car',
max_speed double comment 'max speed of car',
origin string comment 'manufacturers'
)
comment 'info of car'
partitioned by(
ds string comment 'store part'
)
lifecycle 7
;
create external table if not exists gta_table(
car_id string comment 'id of gta car',
max_speed double comment 'max speed of gta car'
)
comment 'GTA car'
partitioned by(
ds string comment 'store part'
)
lifecycle 7
;
Q&A
基本句法-向table添加数据
insert into table_name
[partition (
col_name = col_val,
...
)]
select
col_name,
...
from table2_name
;
insert overwrite table_name
[partition (
col_name = col_val,
...
)]
select
col_name,
...
from table2_name
;
load data [local] inpath file_path [overwrite] into table table_name
[partition (
col_name=col_val,
...
)]
;
Demo运行(承接创建的表)
load data local inpath './cars1.xlsx' into table base_table
partition (
ds='volkswagen'
);
load data local inpath './cars2.xlsx' into table base_table
partition (
ds='audi'
);
load data local inpath './cars3.xlsx' into table base_table
partition (
ds='byd'
);
insert overwrite gta_table partition (ds='vlokswagen')
select
car_id,
max_speed
from base_table
where ds='vlokswagen';
Q&A
基本句法-删除table中数据&删除table
- hive删除不支持
Delete from table_name where ... - hive中
truncate 不支持外部表删除
alter table table_name drop partition(col_name='col_value')
insert overwrite table table_name partition(col_name='col_value')
select col1, col2, ... from table_name
where col_name='col_value' and col2 is not null;
truncate table table_name;
insert overwrite table table_name select * from table_name where 条件;
drop table [if exists] table_name;
drop table [if exists] table_name purge;
Demo运行(承接添加数据后的表)
insert overwrite table gta_table partition(ds='volkswagen')
select car_id, max_speed from gta_table
where ds='volkswagen' and max_speed >= 100;
基本句法-修改table信息
alter table table_name rename to table_name_new;
alter table table_name add columns(
col_name data_type [comment col_coment],
...
);
alter table table_name drop columns col_name;
alter table table_name change col_name new_colname new_type;
alter table table_name replace columns(
col_name data_type [comment col_coment],
...
);
Demo运行
alter table gta_table rename to gta_car;
Q&A
基本句法-查找table中数据
select [all|distinct] col1, col2, ...
from table_name
[where where_condition]
[group by col_name [having have_condition]]
[order by col_name]
[limit num];
Demo
select distinct car_id
from gta_car
limit 2;
基本句法-查看table信息、分区信息
Demo
show partitions gta_car;
show create table gta_car;
desc formatted gta_car;
desc gta_car;
desc extended gta_car;
函数的使用
聚合函数 窗口函数 日期函数 数学运算函数 字符串处理函数 其他函数
|