在 Airflow 2.8 使用官方 provider 运行 `Checkpoint`
解读
国内大多数公司把 Airflow 当“定时器”用,真正用到 Great Expectations(GX)官方 provider 做数据质量 Checkpoint 的场景并不多。面试官问这一条,核心想看三件事:
- 你是否知道 Airflow 2.8 的 provider 体系已经内置了
GreatExpectationsOperator,而不再推荐自己写PythonOperator去调ge_checkpoint_run; - 你是否能在 国内网络隔离环境 下把 GX 的
datasource、expectation_suite、checkpoint三件套一次性跑通; - 你是否理解 Checkpoint 失败时的重试、告警、SLA 与下游任务熔断 如何与 Airflow 原生机制打通。
答得太浅(“我 pip install great-expectations 再 bash 一下”)会被直接打断;答得太偏(“我把 GX 跑在 K8sPodOperator 里”)又会被质疑“何必用官方 provider”。因此,必须给出一条 可落地的、符合国内数据仓库现状 的端到端路径。
知识点
- Airflow 2.8 provider 版本对齐:
apache-airflow-providers-great-expectations==0.2.3仅兼容great-expectations>=0.17.0,<0.18.0,需锁定镜像源,否则国内 pip 会拉成 1.0+ 导致 API 漂移。 - Checkpoint 三要素:
datasource:国内常用 Hive + JDBC 或 OSS/S3 外部表,需在great_expectations.yml里把connection_string写成hive+jdbc://格式,并提前把hive-jdbc-standalone.jar放到$AIRFLOW_HOME/data/hive_jdbc目录;expectation_suite:命名规范建议dwh.{layer}.{table}_suite,方便 Airflow 用{dag_id}.{task_id}变量自动拼接;checkpoint:必须显式指定run_name_template="%Y%m%d_%H%M%S_airflow_{{ ts_nodash }}",否则并发补数会出现 GX 冲突锁。
- 官方 operator 关键参数:
data_context_root_dir:指向 GX 项目目录,推荐放在 DAG 同级 git 子目录include/gx,CI 打包时一起发;checkpoint_name与fail_task_on_validation_failure=True:国内数仓习惯把“数据质量”当软告警,但面试时必须强调 硬熔断,即 Checkpoint 不过下游 ETL 直接raise AirflowFailException;return_json_dict=True:把run_results写回 XCom,方便下一个任务做 行级别异常采样 推钉钉/飞书。
- 国内网络坑:
- GX 默认会去
https://docs.greatexpectations.io拉expectation元数据,需在great_expectations.yml里加anonymous_usage_statistics: false并设置GE_USAGE_STATS_URL=""; - 若用 Redshift/AnalyticDB PostgreSQL 作数据源,需在
requirements.txt里显式加psycopg2-binary==2.9.9,否则编译报错。
- GX 默认会去
- 权限与并发:
- GX 会在
$GE_HOME/uncommitted目录写临时validations,务必给 airflow 用户 目录写权限; - Airflow 2.8 的
max_active_runs_per_dag=1与 GX 的validation_operator默认多线程冲突,需在checkpoint.yml里把validation_operator_name改成action_list_operator并关闭多线程。
- GX 会在
答案
-
环境准备
a) 私有 Nexus 源上传锁定包:apache-airflow-providers-great-expectations==0.2.3、great-expectations==0.17.23。
b) 在 Airflow 镜像里内置hive-jdbc-standalone.jar与ge_redshift.jar,并设置ENV GE_HOME=/opt/airflow/gx。 -
GX 项目初始化(CI 阶段完成,禁止线上手动 init)
great_expectations --v3-api init --no-view
great_expectations --v3-api datasource new \
--name dwh_hive \
--class_name SqlAlchemyDatasource \
--connection_string hive+jdbc://hive@emr-header-1:10000/dwh
great_expectations --v3-api suite new --datasource dwh_hive --table dwd.orders --suite dwd.orders_suite
great_expectations --v3-api checkpoint new dwd.orders_checkpoint
修改 checkpoint.yml:
run_name_template: "%Y%m%d_%H%M%S_airflow_{{ ts_nodash }}"
validations:
- batch_request:
datasource_name: dwh_hive
data_connector_name: default_inferred_data_connector_name
data_asset_name: dwd.orders
expectation_suite_name: dwd.orders_suite
action_list:
- name: store_validation_result
action:
class_name: StoreValidationResultAction
- name: store_evaluation_params
action:
class_name: StoreEvaluationParametersAction
- DAG 代码(Airflow 2.8,Python 3.9)
from datetime import timedelta
from airflow import DAG
from airflow.providers.great_expectations.operators.great_expectations import GreatExpectationsOperator
from airflow.utils.trigger_rule import TriggerRule
from airflow.exceptions import AirflowFailException
with DAG(
dag_id="dwd_orders_quality",
start_date=pendulum.datetime(2024, 1, 1, tz="Asia/Shanghai"),
schedule_interval="0 3 * * *",
catchup=False,
max_active_runs=1,
tags=["dwd", "quality"],
default_args={
"owner": "data",
"retries": 2,
"retry_delay": timedelta(minutes=5),
"sla": timedelta(hours=2),
},
) as dag:
checkpoint = GreatExpectationsOperator(
task_id="gx_checkpoint_orders",
data_context_root_dir="/opt/airflow/dags/repo/include/gx",
checkpoint_name="dwd.orders_checkpoint",
fail_task_on_validation_failure=True,
return_json_dict=True,
)
# 下游任务仅当 Checkpoint 成功才执行
dwd_orders_load = BashOperator(
task_id="dwd_orders_load",
bash_command="bash /opt/airflow/scripts/dwd_orders_load.sh {{ ds }}",
trigger_rule=TriggerRule.ALL_SUCCESS,
)
# 如果 Checkpoint 失败,立即告警并熔断
alert_quality = PythonOperator(
task_id="alert_quality",
python_callable=_send_dingtalk,
trigger_rule=TriggerRule.ONE_FAILED,
op_kwargs={"msg": "GX Checkpoint 失败,已阻断下游"},
)
checkpoint >> [dwd_orders_load, alert_quality]
- 运行与验证
- 手动
airflow dags test dwd_orders_quality gx_checkpoint_orders 2024-01-01可在 EMR 开发机 离线调试; - 上线后通过 Airflow UI → Browse → XCom 查看
validation_result统计,expectation_success_rate<1 即自动重跑; - 补数时用
airflow dags backfill --rerun-failed-tasks,GX 的run_name带ts_nodash可保证 幂等写入。
- 手动
拓展思考
- 多数据源混合 Checkpoint:国内常把 Hive 离线表 与 ClickHouse 实时表 做 row_count 交叉校验,可在同一个 DAG 里起两个
GreatExpectationsOperator,分别连 Hive 与 ClickHouse,再用PythonOperator对比XCom返回的observed_value,差值超过 5% 即raise AirflowFailException。 - SLA 与成本平衡:GX 全表扫描费用高,可在
checkpoint.yml里加sampling:limit 1000万或按_dt partition采样,同时把expectation_suite拆成 核心字段 与 非核心字段 两套,用 Airflow 2.8 的 Dataset 感知调度,仅当上游分区Dataset变更才触发核心字段校验,非核心字段每日抽样一次,节省 30% EMR 费用。 - 与数据目录集成:把 GX 的
validation_result回写 阿里云 DataWorks 数据地图 或 Hive MSCK,实现 质量评分 在数据资产门户可视化;回写接口可放在GreatExpectationsOperator的on_success_callback里,用 Airflow 2.8 的 Listener 机制解耦,避免污染主 DAG 逻辑。