| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- -- 作者:tianyu.chu
- -- 日期:2026-07-17
- -- 工单:(无)
- -- 目的:拼团全量最新态日常 merge —— full dt=${dt} = 昨日全量中今天没变的 + 今日增量
- -- 状态:[草案]
- -- 备注:sched=T,${dt}=业务日 T-1、${pdt}=T-2(即前一天的 full)。
- --
- -- **反连接而非窗口函数**:今天的全量 = 「昨日全量 LEFT ANTI JOIN 今日增量 id」+「今日增量」。
- -- 右侧只有当日变更的 id(千级),Spark 会 broadcast,全量表只扫不 shuffle、也不排序。
- -- 若改用 ROW_NUMBER() OVER (PARTITION BY id) 套在 union 上,等于每天把全量按 id 全shuffle+排序一遍,
- -- 增量才几千行却要搬动全表,merge 的意义就没了。
- -- 正确性:ODS inc 单个 dt 分区内一个 id 只有一行(已按 (id, ods_dt) 去重),且 inc dt=${dt}
- -- 意味着该记录最后变更就在当天、必然比昨日全量那版新 —— 故「变了的取 inc、没变的留昨日」
- -- 与「取 max(update_time)」等价。
- -- 注:LEFT ANTI JOIN 是 Spark SQL 语法(Hive 2.1 不支持),本 SQL 走 spark-sql-starter,无碍。
- --
- -- **静态分区写入**(PARTITION(dt='${dt}'))是硬要求:本 SQL 读 full dt=${pdt} 又写 full,
- -- 若用动态分区(PARTITION(dt)),输出路径=表根目录、与读的分区路径重叠,Spark 会报
- -- "Cannot overwrite a path that is also being read from"。静态分区的输出是具体分区目录,不重叠。
- -- 起点由 manual/backfill/20260717_..._ful_d_seed.sql 一次性 seed;断链需重新 seed。
- -- 历史多版本在 inc 表(拉链底座),本表只保当前态。lock 是保留字,SELECT 里必须带反引号。
- --
- -- **分区保留 2 天**(只留 ${dt} 与 ${pdt}):文末 ALTER 精确 drop ${rdt}(=${dt}-2,即 T-3),
- -- 每天正好掉一个。用精确分区值而非 dt < 'x'——Spark SQL 的 DROP PARTITION 只接受 col=value,
- -- 比较式是 Hive 语法、Spark 解析不了。DS 需额外传参 rdt=$[yyyyMMdd-3];手动跑加 -p rdt=<T-3>。
- -- 代价:漏跑一天则那天本该掉的分区不会被补 drop,需手工清;断链重新 seed 即可。
- INSERT OVERWRITE TABLE ods.ods_trd_card_group_info_ful_d PARTITION (dt='${dt}')
- -- 昨日全量里今天没变的
- SELECT
- id, merchant_id, appid, name, code,
- status, specs, type, random_type, total_price,
- copies, unit_price, sold_copies, release_time, cycle,
- show_applet, title, msg, remark, create_time,
- update_by, update_time, order_quota_min, order_quota_max, user_quota_max,
- start_time, marketing_info, reviewmsg, `lock`, commission_rate,
- year, sport, manufacturer, sets, act,
- config, info_config, total_num, banner_end_time, add_banner,
- finished_time, display_name, group_sets_no, close_payment_time, confirm_send_time,
- close_payment_status, open_card, close_payment_record, group_full_time, live_create_time,
- live_start_time, live_end_time, report_start_time, report_end_time, report_review_num,
- report_review_first_time, report_review_end_time, review_hold_time, review_approval_time, review_num,
- config_json, free_flag, mer_name, change_type, act_price,
- act_config_json, real_sold_num, weight, hot_type, team_first,
- prop1, prop2, prop3, point_rate, point_max,
- point_min, list_id, list_code, mix_copies, sub_type,
- act_point_type, payment_method, payment_total_price, payment_commission, payment_finished_price,
- payment_remain_price, payment_online_price, exclusive, has_bg, merchant_sort,
- del_flg, del_time, review_account, act_id, sold_end_time,
- panini_list_id, hot_type_config, goods_type, report_flag, use_coupon,
- user_level, custom, gift_card_id, group_show_name, min_card_num,
- act_type, waring_type, compensation_status, point_type, first_act_config,
- gift_config, version, extra_prop, use_member_discount, merchant_open,
- is_deleted
- FROM (SELECT * FROM ods.ods_trd_card_group_info_ful_d WHERE dt = '${pdt}') f
- LEFT ANTI JOIN (
- SELECT id FROM ods.ods_trd_card_group_info_inc_d WHERE dt = '${dt}'
- ) i ON f.id = i.id
- UNION ALL
- -- 今日变了的(含今日新建)
- SELECT
- id, merchant_id, appid, name, code,
- status, specs, type, random_type, total_price,
- copies, unit_price, sold_copies, release_time, cycle,
- show_applet, title, msg, remark, create_time,
- update_by, update_time, order_quota_min, order_quota_max, user_quota_max,
- start_time, marketing_info, reviewmsg, `lock`, commission_rate,
- year, sport, manufacturer, sets, act,
- config, info_config, total_num, banner_end_time, add_banner,
- finished_time, display_name, group_sets_no, close_payment_time, confirm_send_time,
- close_payment_status, open_card, close_payment_record, group_full_time, live_create_time,
- live_start_time, live_end_time, report_start_time, report_end_time, report_review_num,
- report_review_first_time, report_review_end_time, review_hold_time, review_approval_time, review_num,
- config_json, free_flag, mer_name, change_type, act_price,
- act_config_json, real_sold_num, weight, hot_type, team_first,
- prop1, prop2, prop3, point_rate, point_max,
- point_min, list_id, list_code, mix_copies, sub_type,
- act_point_type, payment_method, payment_total_price, payment_commission, payment_finished_price,
- payment_remain_price, payment_online_price, exclusive, has_bg, merchant_sort,
- del_flg, del_time, review_account, act_id, sold_end_time,
- panini_list_id, hot_type_config, goods_type, report_flag, use_coupon,
- user_level, custom, gift_card_id, group_show_name, min_card_num,
- act_type, waring_type, compensation_status, point_type, first_act_config,
- gift_config, version, extra_prop, use_member_discount, merchant_open,
- is_deleted
- FROM ods.ods_trd_card_group_info_inc_d
- WHERE dt = '${dt}';
- -- 保留 2 天:drop 掉 ${dt}-2(T-3)这一个分区
- ALTER TABLE ods.ods_trd_card_group_info_ful_d DROP IF EXISTS PARTITION (dt='${rdt}');
|