你当前正在访问 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="XXXXX" credentials.password="XXXXX"
创建 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="XXXXX" credentials.password="XXXXX"
创建 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: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX # add access key id
secret_access_key: XxXxXxXXXXXXXxXxXxxXxxXXXXXXXXxXxxXXxXXXXXXXxxxXxXXxXXXXXxXXxXXXxXxXxxxXXxXXxXXXXXxXxxXX # 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: john password: pass
连接到公共源(无凭据):
name: test_ws_conn_python_no_cred type: python_feed target: https://test-feed.com3
在 CLI 中创建 Azure 机器学习连接:
az ml connection create --file connection.yaml
Azure 容器注册表
使用以下 YAML 文件之一创建与 Azure 容器注册表的连接。 请务必更新适当的值:
使用 Microsoft Entra ID 身份验证进行连接:
name: test_ws_conn_cr_managed type: container_registry target: https://test-feed.com credentials: type: managed_identity client_id: client_id resource_id: resource_id
使用用户名和密码进行连接:
name: test_ws_conn_cr_user_pass type: container_registry target: https://test-feed.com2 credentials: type: username_password username: contoso password: pass
在 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://XXXXXXXXX.core.windows.net/mycontainer"
wps_connection = WorkspaceConnection(
name=name,
type="apikey",
target=target,
credentials=ApiKeyConfiguration(key="XXXXXXXXX"),
)
ml_client.connections.create_or_update(workspace_connection=wps_connection)
相关内容
如果使用数据连接(Snowflake DB、Amazon S3 或 Azure SQL DB),请参阅以下文章,了解详细信息: