Files
article/hive/hive表删除数据的几种方式.md
2025-12-18 09:50:26 +08:00

74 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## hive 删除数据
### 删除表
```sql
-- hive删除表
drop table table_name;
-- 永久性删除,不能恢复:
drop table table_name purge;
```
### 清空数据
```sql
-- hive删除表中数据
truncate table table_name;
-- hive按分区删除数据
alter table table_name drop partition (partition_name='分区名')
```
### 重写数据
与删除条件相反,如删除 date=0528 的数据
那么 重写数 的条件为 date!=0528
**分区表**
```
删除具体partition的部分数据
INSERT OVERWRITE TABLE table_name PARTITION(year='2021')
SELECT * FROM table_name WHERE year='2021' and xx;
```
**非分区表**
```
INSERT OVERWRITE TABLE table_name SELECT * FROM table_name WHERE xx;
```
**临时表**
```
-- 1. 创建临时表存储处理后的数据临时表可使用CREATE TEMPORARY TABLE会话结束自动删除
CREATE TEMPORARY TABLE tmp_my_table
AS
SELECT col1, col2, col3
FROM my_table
WHERE col3 > 100; -- 对原表数据进行过滤/转换
-- 2. 覆盖原表(清空原表并写入临时表的数据)
INSERT OVERWRITE TABLE my_table
SELECT * FROM tmp_my_table;
-- 3. 手动删除临时表若使用的是普通表而非TEMPORARY表
DROP TABLE IF EXISTS tmp_my_table;
```