You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Dataform中配置BQ表每日增量更新仅拉取最新数据

问题原因分析

你修改后的代码在全量初始化(首次运行,非增量模式)分支直接使用了max(ts)但未指定来源表,SQL无法解析该字段对应的表,直接触发运行报错。

可行解决方案

调整SQL逻辑+配置定时调度即可实现每日自动增量同步,无需手动修改代码:

  • 全量初始化场景下明确时间戳的来源为原始表raw_data
  • 主查询新增时间筛选条件,仅同步大于checkpoint的新数据
  • 调度配置设置每日定时运行即可,无需修改代码逻辑

修正后的完整代码示例:

config {
  type: "incremental",
  bigquery: {
    partitionBy: "DATE(ts)",
    clusterBy: ["itemName"]
  }
}

pre_operations {
  declare event_timestamp_checkpoint default (
    ${when(incremental(),
    -- 增量模式:取目标表已同步的最大时间戳为起点
    `select max(ts) from ${self()}`,
    -- 全量初始化模式:取原始表最小时间戳,若首次只需同步昨日数据,可替换为 `select timestamp_sub(current_timestamp(), interval 1 day)`
    `select min(ts) from ${ref("raw_data")}`)}
  )
}

SELECT distinct 
  timestamp as ts,
  storeName,
  DATE(timestamp) as Date,
  itemId
  -- 其余业务字段
FROM ${ref("raw_data")}
-- 必加增量筛选条件,自动触发BigQuery分区裁剪降低查询成本
WHERE timestamp >= event_timestamp_checkpoint

可选优化项

如果你的raw_data存在数据延迟上传的情况,可以给增量模式的checkpoint增加缓冲时间,避免漏同步延迟数据,示例:

`select timestamp_sub(max(ts), interval 1 hour) from ${self()}`

内容的提问来源于stack exchange,提问作者Serge de Gosson de Varennes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 14:24:03