Azure devops 在Azure Devops中将数据种子添加到Cosmos DB

Azure devops 在Azure Devops中将数据种子添加到Cosmos DB,azure-devops,azure-cosmosdb,azure-cosmosdb-mongoapi,Azure Devops,Azure Cosmosdb,Azure Cosmosdb Mongoapi,我有ARM模板通过管道创建Cosmos数据库、数据库和集合。由于有多个应用程序使用该数据库,我想为测试播种初始数据,我在devops中寻找Cosmos DB导入任务,发现了这个问题,但目前mongo API不受支持。它无法从json文件导入数据,我将其保存在存储帐户中 我的问题是,有没有其他方法可以通过像powershell/api这样的devops将json文件中的数据添加到cosmos DB中 我的问题是,有没有其他方法可以通过像powershell/api这样的devops将json文件中

我有ARM模板通过管道创建Cosmos数据库、数据库和集合。由于有多个应用程序使用该数据库,我想为测试播种初始数据,我在devops中寻找Cosmos DB导入任务,发现了这个问题,但目前mongo API不受支持。它无法从json文件导入数据,我将其保存在存储帐户中

我的问题是,有没有其他方法可以通过像powershell/api这样的devops将json文件中的数据添加到cosmos DB中

我的问题是,有没有其他方法可以通过像powershell/api这样的devops将json文件中的数据添加到cosmos DB中

答案是肯定的

我们可以尝试使用该任务执行以下powershell脚本:

param([string]$cosmosDbName
     ,[string]$resourceGroup
     ,[string]$databaseName
     ,[string[]]$collectionNames
     ,[string]$principalUser
     ,[string]$principalPassword
     ,[string]$principalTennant)

Write-Output "Loggin in with Service Principal $servicePrincipal"
az login --service-principal -u $principalUser -p $principalPassword -t $principalTennant

Write-Output "Check if database exists: $databaseName"
if ((az cosmosdb database exists -d $databaseName -n $cosmosDbName -g $resourceGroup) -ne "true")
{
  Write-Output "Creating database: $databaseName"
  az cosmosdb database create -d $databaseName -n $cosmosDbName -g $resourceGroup
}

foreach ($collectionName in $collectionNames)
{
  Write-Output "Check if collection exists: $collectionName"
  if ((az cosmosdb collection exists -c $collectionName -d $databaseName -n $cosmosDbName -g $resourceGroup) -ne "true")
  {
    Write-Output "Creating collection: $collectionName"
    az cosmosdb collection create -c $collectionName -d $databaseName -n $cosmosDbName -g $resourceGroup
  }
}

Write-Output "List Collections"
az cosmosdb collection list -d $databaseName -n $cosmosDbName -g $resourceGroup
然后按脚本参数后的三个点并添加PowerShell脚本中定义的参数(将所有参数放入变量中):

您可以查看此以了解更多详细信息

我的问题是,有没有其他方法可以通过像powershell/api这样的devops将json文件中的数据添加到cosmos DB中

答案是肯定的

我们可以尝试使用该任务执行以下powershell脚本:

param([string]$cosmosDbName
     ,[string]$resourceGroup
     ,[string]$databaseName
     ,[string[]]$collectionNames
     ,[string]$principalUser
     ,[string]$principalPassword
     ,[string]$principalTennant)

Write-Output "Loggin in with Service Principal $servicePrincipal"
az login --service-principal -u $principalUser -p $principalPassword -t $principalTennant

Write-Output "Check if database exists: $databaseName"
if ((az cosmosdb database exists -d $databaseName -n $cosmosDbName -g $resourceGroup) -ne "true")
{
  Write-Output "Creating database: $databaseName"
  az cosmosdb database create -d $databaseName -n $cosmosDbName -g $resourceGroup
}

foreach ($collectionName in $collectionNames)
{
  Write-Output "Check if collection exists: $collectionName"
  if ((az cosmosdb collection exists -c $collectionName -d $databaseName -n $cosmosDbName -g $resourceGroup) -ne "true")
  {
    Write-Output "Creating collection: $collectionName"
    az cosmosdb collection create -c $collectionName -d $databaseName -n $cosmosDbName -g $resourceGroup
  }
}

Write-Output "List Collections"
az cosmosdb collection list -d $databaseName -n $cosmosDbName -g $resourceGroup
然后按脚本参数后的三个点并添加PowerShell脚本中定义的参数(将所有参数放入变量中):


您可以查看更多详细信息。

我遇到了相同的场景,我需要在源代码管理中维护配置文档,并在各种Cosmos实例发生更改时更新它们。我最后要做的是编写一个python脚本来读取目录结构,每个需要更新的集合对应一个文件夹,然后读取文件夹中的每个json文件并将其插入Cosmos。在那里,我运行了python脚本,作为Azure DevOps中多阶段管道的一部分

这是我的概念验证代码的链接


这是一个到管道Python任务的链接

我遇到了相同的场景,我需要在源代码管理中维护配置文档,并在各种Cosmos实例发生更改时更新它们。我最后要做的是编写一个python脚本来读取目录结构,每个需要更新的集合对应一个文件夹,然后读取文件夹中的每个json文件并将其插入Cosmos。在那里,我运行了python脚本,作为Azure DevOps中多阶段管道的一部分

这是我的概念验证代码的链接


这是管道Python任务的链接

谢谢,但本文档介绍了如何通过powershell创建数据库,目前ARM模板支持powershell。我正在寻找的东西,可以添加到集合的内容。例如,假设我有testdata.json,我想将数据添加/导入到集合中,这样测试用例就不会失败。谢谢,但本文档介绍了如何通过powershell创建数据库,目前ARM模板支持powershell。我正在寻找的东西,可以添加到集合的内容。例如,假设我有testdata.json,我想将数据添加/导入到集合中,这样测试用例就不会失败。