【问题标题】:How to add dynamic connection string in Azure Data Factory如何在 Azure 数据工厂中添加动态连接字符串
【发布时间】:2019-08-12 05:49:46
【问题描述】:

有人可以帮助我使用 azure 数据工厂实现与数据库的动态连接吗?

【问题讨论】:

  • 请添加更多详细信息,说明您正在尝试完成的工作、您尝试过的内容以及您正在尝试做的一些示例。

标签: azure azure-sql-database azure-cosmosdb azure-data-factory azure-databricks


【解决方案1】:

使用 azure 数据工厂实现与数据库的动态连接

基于此文档Parameterize linked services in Azure Data Factory,到目前为止,您现在可以参数化链接服务并在运行时传递动态值。

它支持 Cosmos DB:

顺便说一句,MS 建议不要参数化密码或机密。将所有连接字符串存储在 Azure Key Vault 中,并参数化 Secret Name。关于详细信息,请参阅此link


更新答案:

db name 当然可以参数化。

您可以在创建 cosmos db 链接服务时配置动态数据库名称。

单击动态内容并创建新参数。


更新答案 2:

请参考这个sdk function和我的工作代码如下:

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.datafactory import DataFactoryManagementClient
from azure.mgmt.datafactory.models import *

# Azure subscription ID
subscription_id = '***'

# This program creates this resource group. If it's an existing resource group, comment out the code that creates the resource group
rg_name = '***'

# The data factory name. It must be globally unique.
df_name = '***'

# Specify your Active Directory client ID, client secret, and tenant ID
credentials = ServicePrincipalCredentials(client_id='***',
                                          secret='***',
                                          tenant='***')
resource_client = ResourceManagementClient(credentials, subscription_id)
adf_client = DataFactoryManagementClient(credentials, subscription_id)

resource_client.resource_groups.get(rg_name)

# Create a data factory
df_resource = Factory(location='eastus')
df = adf_client.factories.get(rg_name, df_name, df_resource)
print(df)

ls_name = 'testlink1'
dbName = "<your db name>"

connection_string = 'AccountEndpoint=https://***.documents.azure.com:443/;AccountKey=***;Database='+dbName+';';

ls_cosmos_db = CosmosDbLinkedService(connection_string=connection_string)
ls = adf_client.linked_services.create_or_update(rg_name, df_name, ls_name, ls_cosmos_db)
print(ls)

更多细节,可以参考官方create linked service example code和python sdk for ADF management

【讨论】:

  • 嗨 Jay,我正在尝试参数化数据库名称。这样我在 ADF 中指定了相同的内容,我们应该能够根据参数连接到相应的数据库。
  • 感谢您提供帮助的回复。但是我可以将此参数传递给python脚本吗?
  • @Nomad18 嗨,Nomad18。我测试了上面的代码,请看更新答案2。希望对你有帮助。
猜你喜欢
  • 2019-11-17
  • 1970-01-01
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
  • 2022-01-12
  • 2020-12-17
  • 2019-11-30
  • 2019-12-24
相关资源
最近更新 更多