我在 json 文件中建立了两个连接
aws_default
和 google_cloud_default
,如下所示:
{
"aws_default": {
"conn_type": "s3",
"host": null,
"login": "sample_login",
"password": "sample_secret_key",
"schema": null,
"port": null,
"extra": null
},
"google_cloud_default": {
"conn_type": "google_cloud_platform",
"project_id": "sample-proj-id123",
"keyfile_path": null,
"keyfile_json": {sample_json},
"scopes": "sample_scope",
"number_of_retries": 5,
}
}
我有一个在 Docker 中容器化的本地气流服务器。我想做的是从此文件导入连接,这样我就不需要在 Airflow UI 中定义连接。
我有一个entrypoint.sh 文件,每次构建气流图像时都会运行。
我已将这一行
airflow connections import connections.json
包含在该 shell 文件中。
在我的 docker-compse.yaml 文件中,我添加了一个绑定的卷,如下所示:
- type: bind
source: ${HOME}/connections.json
target: /usr/local/airflow/connections.json
但是,当我在本地运行 DAG(其中包括连接到这些连接的钩子)时,我收到错误:即
The conn_id `google_cloud_default` isn't defined
所以我不太确定如何继续。我正在阅读有关 Airflow 本地文件系统机密后端的内容here。
它提到了这个代码块来建立文件路径:
[secrets]
backend = airflow.secrets.local_filesystem.LocalFilesystemBackend
backend_kwargs = {"variables_file_path": "/files/var.json", "connections_file_path": "/files/conn.json"}
但是,当我检查我的airflow.cfg时,我找不到这个代码块。我应该将其添加到airflow.cfg 中吗?
是的,您应该在airflow.cfg 中添加一个[secrets] 部分。不要忘记将文件复制到 docker-compose.yaml 中的容器中:
volumes:
- ${AIRFLOW_PROJ_DIR:-.}/dags:/opt/airflow/dags
- ${AIRFLOW_PROJ_DIR:-.}/logs:/opt/airflow/logs
- ${AIRFLOW_PROJ_DIR:-.}/config:/opt/airflow/config
- ${AIRFLOW_PROJ_DIR:-.}/plugins:/opt/airflow/plugins
- ${AIRFLOW_PROJ_DIR:-.}/plugins:/opt/airflow/files
- ${AIRFLOW_PROJ_DIR:-.}/airflow.cfg:/opt/airflow/airflow.cfg
请注意,连接不会出现在 GUI 中,但您可以从代码中使用它们。