我有一个ARM模板,该模板可按如下方式获取cosmosDb的primaryMasterKey:
{ “属性”:{ “已启用”:是, “ siteConfig”:{ “ appSettings”:[ { “ name”:“ MongoDb:CnnDetails”, “ value”:“ [[listKeys(resourceId('Microsoft.DocumentDB / databaseAccounts',variables('cosmosdb_full')),'2015-04-08')。primaryMasterKey]” } }
如何修改它以获取实际的连接字符串?
我尝试了几件事:
'语言表达属性'primaryConnectionString'不存在,可用属性为'primaryMasterKey,secondaryMasterKey,primaryReadonlyMasterKey,secondaryReadonlyMasterKey'
'语言表达属性'primaryConnectionString'不存在,可用属性为'connectionStrings'
我去了https://docs.microsoft.com/en-us/rest/api/cosmos-db-resource-provider/databaseaccounts/listconnectionstrings#code-try-0进行尝试。 ListKeys返回这样的结构:
{ “ primaryMasterKey”:“ [已编辑]”, “ secondaryMasterKey”:“ [已编辑]”, “ primaryReadonlyMasterKey”:“ [已编辑]”, “ secondaryReadonlyMasterKey”:“ [已编辑]” }
所以我明白了为什么.primaryMasterKey起作用了。但是ListConnectionStrings返回:
{
"connectionStrings": [
{
"connectionString": "mongodb://[REDACTED]:10255/?ssl=true&replicaSet=globaldb",
"description": "Primary MongoDB Connection String"
},
{
"connectionString": "mongodb://[REDACTED]:10255/?ssl=true&replicaSet=globaldb",
"description": "Secondary MongoDB Connection String"
},
{
"connectionString": "mongodb://[REDACTED]:10255/?ssl=true&replicaSet=globaldb",
"description": "Primary Read-Only MongoDB Connection String"
},
{
"connectionString": "mongodb://[REDACTED]:10255/?ssl=true&replicaSet=globaldb",
"description": "Secondary Read-Only MongoDB Connection String"
}
]
}
不确定如何“索引”吗?
感谢任何线索。
答案 0 :(得分:10)
对于其他任何发现此问题并想要完整的ARM模板代码段的人,这是我使用过的并且正在起作用:
"connectionStrings": [
{
"name": "CosmosConnection",
"connectionString": "[listConnectionStrings(resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDbAccountName')), '2019-12-12').connectionStrings[0].connectionString]",
"type": 3
}
]
答案 1 :(得分:2)
像通常使用几乎任何语言一样
WITH CTE
AS (SELECT TOP (100000) t.*
FROM Ticket AS t
WHERE t.ExportDate IS NULL
AND EXISTS (SELECT *
FROM Series s
WHERE t.SeriesId = s.id
AND s.EndDate <= DATEADD(MONTH, -1, GETDATE()))
AND NOT EXISTS(SELECT *
FROM Payment p
WHERE t.id = p.TicketId)
ORDER BY t.id)
DELETE FROM CTE
索引从0开始。
您有一种更“原生”的方式:
ListConnectionStrings.connectionStrings[index].connectionString
但只有可用的功能是第一个和最后一个
答案 2 :(得分:0)
oatsoda 的答案是正确的,但只有当您与要为其获取连接字符串的 Cosmos DB 位于同一资源组中时,它才有效。如果您的 Cosmos DB 与您正在为以下代码段生成 ARM 模板的资源位于不同的资源组中,那么我用来为应用服务生成连接字符串并且正在运行。
"Cosmos": {
"value": "[listConnectionStrings(resourceId(parameters('cosmosResourceGroupName'),'Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDbName')), '2019-12-12').connectionStrings[0].connectionString]",
"type": "Custom"
}