2

I have a PowerShell script for creating database and collection inside Azure Cosmos DB and I am able to insert some dummy records inside collection by using the below PowerShell script.

    #region Parameters

$clientId= "< Replace with your clientId >"
$clientSecret= "< Replae with you clientsecret >"
$subscriptionName= "< Replace with your subscription name>"
$tenantId= "< Replace with your tenant Id>"
$resourceGroupName= "Demo"
$connectionString='< Replace wtih Cosmos DB Connection String >'
$cosmosDBAccounts= @('demo-account-01')
#$accountName='demo-account-01'
$databaseName='demo-db-01'
$collectionName='demo-collection-01'
$partitionkey= 'demo'

#endregion

#region Login into Azure using Interactive Mode or Service Principal details

# sign in
Write-Host "Logging in...";

#Connect-AzAccount 
$securePassword = $clientSecret | ConvertTo-SecureString -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $clientId, $securePassword
Connect-AzAccount -Credential $cred -ServicePrincipal -TenantId $tenantId

#Set the current azure subscription
Select-AzSubscription  -subscription $subscriptionName

#endregion

#region Create Collection and insert some data into it

foreach($cosmosDBAccount in $cosmosDBAccounts){

    $cosmosDbContext = New-CosmosDbContext -Account $cosmosDbAccount -Database $databaseName -ResourceGroup $resourceGroupName    
    New-CosmosDbDatabase -Context $cosmosDbContext -Id $databaseName
    New-CosmosDbCollection -Context $cosmosDbContext -Id $collectionName -PartitionKey $partitionkey -OfferThroughput 2500 -Database $databaseName

0..9 | Foreach-Object {

$document = @"
{
         "id": "$([Guid]::NewGuid().ToString())",
         "name": "pradeep",         
         "demo": "XYZ"  
}
"@

New-CosmosDbDocument -Context $cosmosDbContext -CollectionId $collectionName -DocumentBody $document -PartitionKey "XYZ"

}
}

#endregion

But I want to insert the records into Azure Cosmos DB from external file instead of putting the JSON data directly inside PowerShell script.

So, can anyone suggest me how to insert the data into Azure Cosmos DB from external file.

5
  • Could you provide the file sample? Is it a .json file? Commented Feb 1, 2019 at 6:42
  • Does the residence of the external files stable? Such as Azure Blob Storage. Commented Feb 1, 2019 at 6:51
  • @Joy, It is a .json file only. Commented Feb 1, 2019 at 7:07
  • Right now the .json file is located in my local machine only. Commented Feb 1, 2019 at 7:08
  • What the content in the file? Could you provide a sample? Commented Feb 1, 2019 at 7:12

1 Answer 1

6

Add the code snippet to where you need, it will create the documents of the .json file in the collection.

$json = Get-Content 'C:\Users\joyw\Desktop\cosmos.json' | Out-String | ConvertFrom-Json
$cosmosDbContext = New-CosmosDbContext -Account $cosmosDbAccount -Database $databaseName -ResourceGroup $resourceGroupName

foreach($item in $json){
    $document = $item | ConvertTo-Json | Out-String
    New-CosmosDbDocument -Context $cosmosDbContext -CollectionId $collectionName -DocumentBody $document -PartitionKey "XYZ"

}

My .json file:

[{
    "id": "1",
    "name": "pradeep",
    "demo": "XYZ"
}, {
    "id": "2",
    "name": "pradeep12",
    "demo": "XYZ"
}, {
    "id": "3",
    "name": "pradeep123",
    "demo": "XYZ"
}, {
    "id": "4",
    "name": "pradeep1234",
    "demo": "XYZ"
}]

Result:

enter image description here

Check in the portal:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.