Azure
Pushing Knowledge To CosmosDb From Azure Pipeline Utilizing PowerShell
On this article, we’re going to display how we are able to use PowerShell scripts and insert bulk knowledge from a file into cosmosDb leveraging CosmosDb PowerShell package deal
CosmosDb is a absolutely managed, nosql, non relational database which permits us to retailer knowledge durably with a number of ACID configurations. Microsoft ensures pace at any scale
Let’s get began and write the code.
#area param
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[string]$resourceGroup = "",
[Parameter(Mandatory = $false)]
[string]$database = "",
[Parameter(Mandatory = $false)]
[string]$cosmosAccount = "",
[Parameter(Mandatory = $false)]
[string]$filePath = "../file.json"
)
#endregion param
Operate UpdateCosmosDb
{
$Person = "Azure-userId"
$PWord = ConvertTo-SecureString -String "Password" -AsPlainText -Drive
$tenant = "Azure Tenant Id"
$subscription = "cAzure Subscription Id"
$Credential = New-Object -TypeName "System.Administration.Automation.PSCredential" -ArgumentList $Person,$PWord
Join-AzAccount -Credential $Credential -Tenant $tenant -Subscription $subscription #Login into your azure account
$module = Get-InstalledModule -Identify 'CosmosDB'
if($module -ne $null)
{
write-host "module CosmosDB avaiable"
}
else
{
write-host "module CosmosDB not avaiable obtain in progress"
Set up-Module -Identify CosmosDB -AllowClobber -force
}
$gadgets = Get-Content material -path $filePath -Encoding UTF8 -Uncooked | ConvertFrom-Json
$cosmosDbContext = New-CosmosDbContext -Account $cosmosAccount -Database $database -ResourceGroup $resourceGroup
foreach($merchandise in $gadgets)
{
$id = $merchandise.id
$merchandise | Add-Member -NotePropertyName id -NotePropertyValue $id #creates new property in josn
$doc = $merchandise | ConvertTo-Json #converts the thing into json
$question = "SELECT c.id FROM c WHERE c.id = '$id'"
write-host $question
$existingDocument = Get-CosmosDbDocument -Context $cosmosDbContext -CollectionId 'ContainerId' -Question $question
if($existingDocument -ne $null)
{
write-host "Take away $doc"
Take away-CosmosDbDocument -Context $cosmosDbContext -CollectionId 'ContainerId' -PartitionKey "$id" -Id "$id"
}
write-host "New $doc"
New-CosmosDbDocument -Context $cosmosDbContext -CollectionId 'ContainerId' -DocumentBody $doc -PartitionKey "$id"
}
}
UpdateCosmosDb
If you’re executing the above PowerShell in Azure pipeline with for example contributor position you then need not carry out Line No. 20 to 26
Preserve coding
Cheers