本文主要介绍insert overwrite命令修改指定分区内的数据
说明:
insert overwrite
:不支持指定插入列,只能使用insert into
。
命令格式
insert {into|overwrite} table <table_name> [partition (<pt_spec>)] [(<col_name> [,<col_name> ...)]
<select_statement>
from <from_statement>
[zorder by <zcol_name> [, <zcol_name> ...]];
- table_name:必填。需要插入数据的目标表名称。
- pt_spec:可选。需要插入数据的分区信息,不允许使用函数等表达式,只能是常量。格式为
(partition_col1 = partition_col_value1, partition_col2 = partiton_col_value2, ...)
。 - col_name:可选。需要插入数据的目标表的列名称。
insert overwrite
不支持指定[(<col_name> [,<col_name> ...)]
。 - select_statement:必填。
select
子句,从源表中查询需要插入目标表的数据。说明- 源表与目标表的对应关系依赖于
select
子句中列的顺序,而不是表与表之间列名的对应关系。 - 如果目标表是静态分区,向某个分区插入数据时,分区列不允许出现在
select
子句中。
- 源表与目标表的对应关系依赖于
- from_statement:必填。
from
子句,表示数据来源。例如,源表名称。 - zorder by <zcol_name> [, <zcol_name> ...]:可选。向表或分区写入数据时,支持根据指定的一列或多列(select_statement对应表中的列),把排序列数据相近的行排列在一起,提升查询时的过滤性能,在一定程度上降低存储成本。需要注意的是,
order by x, y
会严格地按照先x后y的顺序对数据进行排序,zorder by x, y
会把相近的<x, y>尽量排列在一起。当SQL查询语句的过滤条件中包含排序列时,order by
后的数据仅对包含x的表达式有较好的过滤效果,zorder by
后的数据对包含x或同时包含x、y的表达式均有较好的过滤效果,列压缩比例更高。
示例1:更新dm_bdp_ops.dm_rtc_server_log_alarm_rate_rel_mf 表中的 inc_month = '202101'分区的value值;
insert overwrite table dm_bdp_ops.dm_rtc_server_log_alarm_rate_rel_mf partition (inc_month = '202101') values('3184.0','174','0.0546','386','0.1212') ;
示例2:从其他两张表拿出数据写入 dm_bdp_ops.dm_rtc_server_log_alarm_rate_rel_mf
insert
overwrite table dm_bdp_ops.dm_rtc_server_log_alarm_rate_rel_mf partition (inc_month = '$[time(yyyyMM,-1M)]')
select
task_total_qty,
task_fail_all,
kafka_overstore_num_all,
round((task_fail_all / task_total_qty), 4) as task_alarm_rate,
round((kafka_overstore_num_all / task_total_qty), 4) as kafka_overstore_rate
from
(
select
sum(
task_num_1 + task_num_0 + task_num_3 + task_num_6
) as task_total_qty
from
dm_bdp_ops.dm_rtc_task_statistics_sum_mf
where
task_type = '15'
and inc_month = '$[time(yyyyMM,-1M)]'
) tb,
(
select
count(distinct task_id) as task_fail_all
from
dm_bdp_ops.dm_rtc_server_fail_log_mi
where alarm_type = '1'
and inc_month = '$[time(yyyyMM,-1M)]'
) t2,
(
select
count(distinct task_id) as kafka_overstore_num_all
from
dm_bdp_ops.dm_rtc_server_fail_log_mi
where alarm_type = '2'
and inc_month = '$[time(yyyyMM,-1M)]'
) t3;