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

如何配置本地Apache Airflow实例对接BigQuery Emulator?

解决Airflow连接BigQuery Emulator的认证配置问题

核心配置思路

BigQuery Emulator无需真实GCP认证,需让Airflow的BigQuery操作符绕过默认GCP认证流程,直接指向模拟器地址并使用匿名凭证。

1. 创建专属Airflow连接

  • 在Airflow UI中创建Google Cloud类型连接(不要用HTTP类型)
  • 关键配置项:
    • Conn Id:自定义名称,比如bigquery_emulator
    • Host:填写BigQuery Emulator地址,默认是http://localhost:9050
    • Extra字段添加以下JSON:
      {
        "extra__google_cloud_platform__project": "你的模拟器项目ID",
        "extra__google_cloud_platform__credentials": "{}",
        "extra__google_cloud_platform__use_anonymous": true,
        "extra__google_cloud_platform__endpoint_url": "http://localhost:9050"
      }
      
    说明:use_anonymous设为true强制使用匿名凭证,endpoint_url指定模拟器地址,project需与模拟器中使用的项目ID一致。

2. 配置BigQueryExecuteQueryOperator

在DAG中使用上述连接,示例代码:

from airflow.providers.google.cloud.operators.bigquery import BigQueryExecuteQueryOperator
from datetime import datetime

with DAG(
    'bq_emulator_test_dag',
    schedule_interval=None,
    start_date=datetime(2023, 1, 1),
) as dag:
    create_raw_table = BigQueryExecuteQueryOperator(
        task_id='create_raw_table',
        sql="""
            CREATE TABLE IF NOT EXISTS `你的模拟器项目ID.dataset.raw_table` (
                id INT64,
                data STRING
            )
        """,
        gcp_conn_id='bigquery_emulator',
        use_legacy_sql=False,
    )

3. 环境变量补充(可选)

若仍存在认证问题,启动Airflow前设置环境变量强制客户端指向模拟器:

export BIGQUERY_EMULATOR_HOST=http://localhost:9050
export GOOGLE_CLOUD_PROJECT=你的模拟器项目ID

4. 版本适配检查

Airflow 2.3.4对应的apache-airflow-providers-google版本需≥3.0.0(该版本开始支持use_anonymous参数),版本过低则执行升级:

pip install --upgrade apache-airflow-providers-google>=3.0.0

关键注意事项

  • 不要设置GOOGLE_APPLICATION_CREDENTIALS环境变量,避免干扰匿名认证流程
  • 确保BigQuery Emulator服务正在本地运行,端口与连接配置一致

内容的提问来源于stack exchange,提问作者costello

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 06:10:30