你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
创建连接(预览)
适用范围:Azure CLI ml 扩展 v2(最新版)Python SDK azure-ai-ml v2(最新版)
本文介绍如何连接到 Azure 外部的数据源,使该数据可供 Azure 机器学习服务使用。 Azure 连接充当密钥保管库代理,而与连接的交互是与 Azure 密钥保管库的直接交互。 Azure 机器学习连接将用户名和密码数据资源作为机密安全地存储在密钥保管库中。 密钥保管库 RBAC 控制对这些数据资源的访问。 为了可以使用此数据,Azure 支持连接到以下外部源:
- Snowflake DB
- Amazon S3
- Azure SQL DB
重要
此功能目前处于公开预览状态。 此预览版在提供时没有附带服务级别协议,我们不建议将其用于生产工作负荷。 某些功能可能不受支持或者受限。
有关详细信息,请参阅 Microsoft Azure 预览版补充使用条款。
先决条件
Azure 订阅。 如果没有 Azure 订阅,请在开始操作前先创建一个免费帐户。 试用免费版或付费版 Azure 机器学习。
Azure 机器学习工作区。
重要
Azure 机器学习连接将在连接创建期间传递的凭据安全地存储在工作区 Azure Key Vault 中。 连接从密钥保管库存储位置引用凭据以供后续使用。 将凭据存储到密钥保管库后,你就无需直接处理它们。 可以选择将凭据存储在 YAML 文件中。 CLI 命令或 SDK 可能会替代它们。 建议避免将凭据存储在 YAML 文件中,因为安全漏洞可能会导致凭据泄露。
注意
要成功导入数据,请验证是否已安装适用于 SDK 的最新 azure-ai-ml 包(版本 1.5.0 或更高版本)以及 ml 扩展(版本 2.15.1 或更高版本)。
如果有较旧的 SDK 包或 CLI 扩展,请删除旧包,并使用选项卡部分中显示的代码安装新包。 按照如下所示的 SDK 和 CLI 说明进行操作:
代码版本
az extension remove -n ml
az extension add -n ml --yes
az extension show -n ml #(the version value needs to be 2.15.1 or later)
创建 Snowflake DB 连接
此 YAML 文件创建 Snowflake DB 连接。 请务必更新适当的值:
# my_snowflakedb_connection.yaml
$schema: http://azureml/sdk-2-0/Connection.json
type: snowflake
name: my-sf-db-connection # add your datastore name here
target: jdbc:snowflake://<myaccount>.snowflakecomputing.com/?db=<mydb>&warehouse=<mywarehouse>&role=<myrole>
# add the Snowflake account, database, warehouse name and role name here. If no role name provided it will default to PUBLIC
credentials:
type: username_password
username: <username> # add the Snowflake database user name here or leave this blank and type in CLI command line
password: <password> # add the Snowflake database password here or leave this blank and type in CLI command line
在 CLI 中创建 Azure 机器学习连接:
选项 1:使用 YAML 文件中的用户名和密码
az ml connection create --file my_snowflakedb_connection.yaml
选项 2:在命令行中替代用户名和密码
az ml connection create --file my_snowflakedb_connection.yaml --set credentials.
username="<username>" credentials.
password="<password>"
创建使用 OAuth 的 Snowflake DB 连接
本部分中的信息介绍了如何创建使用 OAuth 进行身份验证的 Snowflake DB 连接。
重要
在按照本部分中的步骤操作之前,必须先配置 Azure 以代表客户端颁发 OAuth 令牌。 此配置创建一个服务主体,这是 OAuth 连接所必需的。 若要创建连接,你需要以下信息:
- 客户端 ID:服务主体的 ID
- 客户端密码:服务主体的机密
- 租户 ID:Microsoft Entra ID 租户的 ID
此 YAML 文件创建使用 OAuth 的 Snowflake DB 连接。 请务必更新适当的值:
# my_snowflakedb_connection.yaml
name: snowflake_service_principal_connection
type: snowflake
# Add the Snowflake account, database, warehouse name, and role name here. If no role name is provided, it will default to PUBLIC.
target: jdbc:snowflake://<myaccount>.snowflakecomputing.com/?db=<mydb>&warehouse=<mywarehouse>&scope=<scopeForServicePrincipal>
credentials:
type: service_principal
client_id: <client-id> # The service principal's client id
client_secret: <client-secret> # The service principal's client secret
tenant_id: <tenant-id> # The Microsoft Entra ID tenant id
在 CLI 中创建 Azure 机器学习连接:
az ml connection create --file my_snowflakedb_connection.yaml
还可以在命令行中替代 YAML 文件中的信息:
az ml connection create --file my_snowflakedb_connection.yaml --set credentials.client_id="my-client-id" credentials.client_secret="my-client-secret" credentials.tenant_id="my-tenant-id"
创建 Azure SQL DB 连接
此 YAML 脚本创建 Azure SQL DB 连接。 请务必更新适当的值:
# my_sqldb_connection.yaml
$schema: http://azureml/sdk-2-0/Connection.json
type: azure_sql_db
name: my-sqldb-connection
target: Server=tcp:<myservername>,<port>;Database=<mydatabase>;Trusted_Connection=False;Encrypt=True;Connection Timeout=30
# add the sql servername, port addresss and database
credentials:
type: sql_auth
username: <username> # add the sql database user name here or leave this blank and type in CLI command line
password: <password> # add the sql database password here or leave this blank and type in CLI command line
在 CLI 中创建 Azure 机器学习连接:
选项 1:使用 YAML 文件中的用户名/密码
az ml connection create --file my_sqldb_connection.yaml
选项 2:替代 YAML 文件中的用户名和密码
az ml connection create --file my_sqldb_connection.yaml --set credentials.
username="<username>" credentials.
password="<password>"
创建 Amazon S3 连接
使用以下 YAML 文件创建 Amazon S3 连接。 请务必更新适当的值:
# my_s3_connection.yaml
$schema: http://azureml/sdk-2-0/Connection.json
type: s3
name: my_s3_connection
target: <mybucket> # add the s3 bucket details
credentials:
type: access_key
access_key_id: bbbbbbbb-1c1c-2d2d-3e3e-444444444444 # add access key id
secret_access_key: H4iJ5kL6mN7oP8qR9sT0uV1wX2yZ3a # add access key secret
在 CLI 中创建 Azure 机器学习连接:
az ml connection create --file my_s3_connection.yaml
非数据连接
你可以使用这些连接类型连接到 Git:
- Python 源
- Azure 容器注册表
- 使用 API 密钥的连接
这些连接不是数据连接,而是用于连接到外部服务,以便在代码中使用。
Git
使用以下 YAML 文件之一创建 Git 连接。 请务必更新适当的值:
使用个人访问令牌进行连接 (PAT):
#Connection.yml name: test_ws_conn_git_pat type: git target: https://github.com/contoso/contosorepo credentials: type: pat pat: dummy_pat
连接到公共存储库(无凭据):
#Connection.yml name: git_no_cred_conn type: git target: https://https://github.com/contoso/contosorepo
在 CLI 中创建 Azure 机器学习连接:
az ml connection create --file connection.yaml
Python 源
使用以下其中一个 YAML 文件创建与 Python 源的连接。 请务必更新适当的值:
使用个人访问令牌进行连接 (PAT):
#Connection.yml name: test_ws_conn_python_pat type: python_feed target: https://test-feed.com credentials: type: pat pat: dummy_pat
使用用户名和密码进行连接:
name: test_ws_conn_python_user_pass type: python_feed target: https://test-feed.com credentials: type: username_password username: <username> password: <password>
连接到公共源(无凭据):
name: test_ws_conn_python_no_cred type: python_feed target: https://test-feed.com3
在 CLI 中创建 Azure 机器学习连接:
az ml connection create --file connection.yaml
容器注册表
使用以下其中一个 YAML 文件创建与 Azure 容器注册表的连接。 请务必更新适当的值:
使用用户名和密码进行连接:
name: test_ws_conn_cr_user_pass type: container_registry target: https://test-feed.com2 credentials: type: username_password username: <username> password: <password>
在 CLI 中创建 Azure 机器学习连接:
az ml connection create --file connection.yaml
API 密钥
以下示例将会创建 API 密钥连接:
from azure.ai.ml.entities import WorkspaceConnection
from azure.ai.ml.entities import UsernamePasswordConfiguration, ApiKeyConfiguration
name = "my_api_key"
target = "https://L6mN7oP8q.core.windows.net/mycontainer"
wps_connection = WorkspaceConnection(
name=name,
type="apikey",
target=target,
credentials=ApiKeyConfiguration(key="9sT0uV1wX"),
)
ml_client.connections.create_or_update(workspace_connection=wps_connection)
通用容器注册表
使用 GenericContainerRegistry 工作区连接,可以为映像生成指定外部注册表,例如 Nexus 或 Artifactory。 环境映像从指定的注册表推送,先前的缓存会被忽略。
使用以下 YAML 文件创建连接。 请务必更新适当的值:
#myenv.yml
$schema: https://azuremlschemas.azureedge.net/latest/environment.schema.json
name: docker-image-plus-conda-example
image: mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu20.04
type: python_feed
conda_file: conda_dep.yml
description: Environment created from a Docker image plus Conda environment
#conda_dep.yml
name: project_environment
dependencies:
- python=3.10
- pip:
- azureml-defaults
channels:
- anaconda
- conda-forge
#connection.yml
name: ws_conn_generic_container_registry
type: container_registry
target: https://test-registry.com
credentials:
type: username_password
username: <username>
password: <password>
#hello_world_job.yml
$schema: https://azuremlschemas.azureedge.net/latest/commandJob.schema.json
command: echo "hello world"
environment: azureml:<env name>@latest
使用凭据从 YAML 文件创建连接:
az ml connection create --file connection.yaml --credentials username=<username> password=<password> --resource-group my-resource-group --workspace-name my-workspace
创建环境
az ml environment create --name my-env --version 1 --file my_env.yml --conda-file conda_dep.yml --image mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu20.04 --resource-group my-resource-group --workspace-name my-workspace
可以验证是否已成功创建环境
az ml environment show --name my-env --version 1 --resource-group my-resource-group --workspace-name my-workspace
相关内容
如果使用数据连接(Snowflake DB、Amazon S3 或 Azure SQL DB),请参阅以下文章,了解详细信息: