如何在Azure数据工厂中添加动态连接字符串

时间:2019-03-21 11:21:55

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

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

1 个答案:

答案 0 :(得分:0)

  

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

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

enter image description here

它支持Cosmos DB:

enter image description here

BTW,MS建议不要参数化密码或机密。而是将所有连接字符串存储在Azure Key Vault中,并参数化“秘密名称”。有关详细信息,请参阅此link


更新答案:

对于数据库名称,您当然可以对其进行参数化。

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

enter image description here

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

enter image description here


更新答案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的官方ADF management和python sdk。

enter image description here