Hive操作指南

24 篇文章 2 订阅
20 篇文章 1 订阅


一. 建外部表

create external  table test.test_external(
                id int comment '测试id' , 
                name string comment '测试名称'  )                           
comment '测试'
row format delimited fields terminated by '\t'
stored as textfile
location '/user/hive/warehouse/external';


二. 建表

create table test.test(
                id int comment '测试id' , 
                name string comment '测试名称'  )                           
comment '测试'
row format delimited fields terminated by '\t'
stored as textfile;

备注: hive常用存储文件类型有textfile、parquet、ocr等


三. 建分区表

create table test.test(
                id int comment '测试id' , 
                name string comment '测试名称'  )                           
comment '测试'
partitioned by (dt string comment '分区日期')
row format delimited fields terminated by '\t'
stored as textfile;


四. 建分区分桶表

create table test.test(
    id int comment '测试id' , 
    name string comment '测试名称'  )                           
comment '测试'
partitioned by (dt string comment '分区日期')
clustered by (id) into 5 buckets
row format delimited fields terminated by '\t'
stored as ocr;

五. 更改表名

ALTER TABLE test RENAME TO new_name;

六. 添加列

ALTER TABLE test ADD COLUMNS (col_spec[, col_spec ...])

备注:
 ALTER TABLE test ADD COLUMNS (col_spec[, col_spec ...]) CASCADE;// 加分区表字段需要加上CASCADE ,否则历史分区数据无法被重新写入数据,即便指定该新增列。


七. 更改列名相关

ALTER TABLE test CHANGE column_name new_name new_type;


八. 删除指定列age(原表共有id,name,age列)

ALTER TABLE test REPLACE COLUMNS (id bigint ,name string)

备注:
ALTER TABLE name DROP [COLUMN] column_name     //  DROP column_name为无效语法

九. 复制表结构
① 非分区表:

create table new_table as select * from exists_table where 1=0;

② 分区表:

create table text2 like test;

十. 复制表结构和数据
① 非分区表: 

create table new_table as select * from exists_table;

② 分区表:
方法一:

1.  create table test2 like test;

 注意: 如果使用as在分区表基础上,创建新表,那么得到的为非分区表,得到原表完整结构要用like。
2. hive cmd模式(将HDFS的数据文件复制一份到新表目录):   

 dfs -cp -f /user/hive/warehouse/test.db/test/* /user/hive/warehouse/test.db/test2/

3. hive cmd模式(修复元数据信息):   

MSCK REPAIR TABLE test2;

方法二:

1. create table test2 like test;

动态分区:

2. insert overwrite table test2 partition (dt) select id,name,dt from test;

十一. 删除表

drop table dbname.tname;

十二. 清空表数据
① 内部表(保留表结构):

truncate table dbname.tname;

② 外部表(保留表结构):
1. 进入hue hdfs指定分区删除
2. 用shell脚本删除

#!/bin/bash
temp=$(date +%Y-%m-%d)
temp2=$(date -d "-1 day" +%Y-%m-%d)
hdfs dfs -rm -r /user/hive/test_table/partition_date=${temp}

 

Hive创建内外部表    Hive 复制分区表和数据
Hive修改表1    Hive修改表2    Hive表删除&清空数据

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值