hive 平时总结

hive 的平时总结

1. hive sql 中如何设置变量

1
2
3
4
5
6
-- 设置变量
set hivevar:test=2;
select ${hivevar:test};

-- 配置文件中的变量
select ${hiveconf:hive.variable.sustitute};

2. hive 常用建表语句

  1. 使用create table 语句建表

    1
    2
    3
    4
    5
    6
    7
    8
    create table db.tableName (
    colName1 type comment '字段1注释',
    colName2 type comment '字段2注释',
    ...
    )
    comment '标注释'
    partitioned by (dt sting comment '格式yyyyMMdd')
    row format delimited fields terminated by ','
  2. 通过一张表的某些字段抽取出来,建表

    1
    2
    3
    create table db.table_name1 
    as
    select col_name1, col_name2 from db.table_name2
  3. 复制表结构

    1
    create table db.table_name1 like db.table_name2

3. 查询结果导出到hdfs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-- 子查询结果导出到 hdfs 的 "/user/username" 下
insert overwrite directory "/user/username"
row format delimited fields terminated by ","
select
col_name1,
col_name2,
col_name3,
...
from
db.table_name
where
dt='20220202'
[and ...]
group by ...
order by ...

4. 修改表属性–删除表分区

1
2
3
4
5
-- 删除列
alter table db.table_name drop partition_name;

-- 删除某个分区(假如dt为分区字段)
alter table db.table_name drop partition(dt='20220202'[,...])

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!