HIVE学习系列——Hive操作

文章目录

Hive表介绍

Hive中表格共有4类,分别是内部表、外部表、分区表与桶表

  • 内部表:删除时会将表中数据与元数据一同删除
  • 外部表:删除时仅删除表与数据之间的引用,不会删除表中数据
  • 分区表:按照指定分区进行数据存储,映射至hdfs可以理解为在根目录下创建子文件夹
  • 分桶表

部分符号释义:
“[ ]”表示该部分内容可以出现,或者不出现
“( )”表示该部分内容必须出现
“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]
    
    [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

  • 是否可以不指定字段的创建表?
  • location关键字安装到了哪里?

基本句法-向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;

函数的使用

聚合函数
窗口函数
日期函数
数学运算函数
字符串处理函数
其他函数

Original: https://blog.csdn.net/jh_210/article/details/124409022
Author: KoenigseggH
Title: HIVE学习系列——Hive操作

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/818374/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球