ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
# 管理连接 Airflow需要知道如何连接到您的环境。 其他系统和服务的主机名,端口,登录名和密码等信息在UI的`Admin->Connection`部分中处理。 您将创作的管道代码将引用Connection对象的“conn_id”。 ![https://airflow.apache.org/_images/connections.png](https://box.kancloud.cn/5786ccc0ebfa016fb953a915eeb1c068_689x318.jpg) 可以使用UI或环境变量创建和管理连接。 有关更多信息,请参阅[Connenctions Concepts](https://apachecn.github.io/airflow-doc-zh/concepts.html)文档。 ## 使用UI创建连接 打开UI的`Admin->Connection`部分。 单击“ `Create`链接以创建新连接。 ![https://airflow.apache.org/_images/connection_create.png](https://box.kancloud.cn/0400e54422e0af91f468637d67f13726_516x316.jpg) 1. 使用所需的连接ID填写`Conn Id`字段。 建议您使用小写字符和单独的带下划线的单词。 2. 使用`Conn Type`字段选择连接类型。 3. 填写其余字段。 有关属于不同连接类型的字段的说明,请参阅连接类型。 4. 单击“ `Save`按钮以创建连接。 ## 使用UI编辑连接 打开UI的`Admin->Connection`部分。 单击连接列表中要编辑的连接旁边的铅笔图标。 ![https://airflow.apache.org/_images/connection_edit.png](https://box.kancloud.cn/e7876d188ee69467acaf54a59584d20d_516x238.jpg) 修改连接属性,然后单击“ `Save`按钮以保存更改。 ## 使用环境变量创建连接 可以使用环境变量创建气流管道中的连接。 环境变量需要具有`AIRFLOW_CONN_` for Airflow的前缀, `AIRFLOW_CONN_` URI格式的值才能正确使用连接。 在引用Airflow管道中的连接时, `conn_id`应该是没有前缀的变量的名称。 例如,如果`AIRFLOW_CONN_POSTGRES_MASTER`名为`postgres_master`则环境变量应命名为`AIRFLOW_CONN_POSTGRES_MASTER` (请注意,环境变量必须全部为大写)。 Airflow假定环境变量返回的值为URI格式(例如`postgres://user:password@localhost:5432/master`或`s3://accesskey:secretkey@S3` )。 ## 连接类型 ### Google云端平台 Google Cloud Platform连接类型支持[GCP集成](https://apachecn.github.io/airflow-doc-zh/integration.html) 。 #### 对GCP进行身份验证 有两种方法可以使用Airflow连接到GCP。 1. 使用[应用程序默认凭据](https://google-auth.readthedocs.io/en/latest/reference/google.auth.html) ,例如在Google Compute Engine上运行时通过元数据服务器。 2. 在磁盘上使用[服务帐户](https://cloud.google.com/docs/authentication/)密钥文件(JSON格式)。 #### 默认连接ID 默认情况下使用以下连接ID。 ``` bigquery_default ``` 由[`BigQueryHook`](https://apachecn.github.io/airflow-doc-zh/integration.html "airflow.contrib.hooks.bigquery_hook.BigQueryHook")钩子使用。 ``` google_cloud_datastore_default ``` 由[`DatastoreHook`](https://apachecn.github.io/airflow-doc-zh/integration.html "airflow.contrib.hooks.datastore_hook.DatastoreHook")钩子使用。 ``` google_cloud_default ``` 由[`GoogleCloudBaseHook`](https://apachecn.github.io/airflow-doc-zh/code.html "airflow.contrib.hooks.gcp_api_base_hook.GoogleCloudBaseHook") , [`DataFlowHook`](https://apachecn.github.io/airflow-doc-zh/integration.html "airflow.contrib.hooks.gcp_dataflow_hook.DataFlowHook") , [`DataProcHook`](https://apachecn.github.io/airflow-doc-zh/code.html "airflow.contrib.hooks.gcp_dataproc_hook.DataProcHook") , [`MLEngineHook`](https://apachecn.github.io/airflow-doc-zh/integration.html "airflow.contrib.hooks.gcp_mlengine_hook.MLEngineHook")和[`GoogleCloudStorageHook`](https://apachecn.github.io/airflow-doc-zh/integration.html "airflow.contrib.hooks.gcs_hook.GoogleCloudStorageHook")挂钩使用。 #### 配置连接 ``` Project Id (required) ``` 要连接的Google Cloud项目ID。 ``` Keyfile Path ``` 磁盘上[服务帐户](https://cloud.google.com/docs/authentication/)密钥文件(JSON格式)的路径。 如果使用应用程序默认凭据则不需要 ``` Keyfile JSON ``` 磁盘上的[服务帐户](https://cloud.google.com/docs/authentication/)密钥文件(JSON格式)的内容。 如果使用此方法进行身份验证,建议[保护您的连接](secure-connections.html) 。 如果使用应用程序默认凭据则不需要 ``` Scopes (comma separated) ``` 要通过身份验证的逗号分隔[Google云端范围](https://developers.google.com/identity/protocols/googlescopes)列表。 注意 使用应用程序默认凭据时,将忽略范围。 请参阅[AIRFLOW-2522](https://issues.apache.org/jira/browse/AIRFLOW-2522) 。