代码之家  ›  专栏  ›  技术社区  ›  Pradeep

如何使用PowerShell脚本将数据从外部文件插入Azure Cosmos DB?

  •  0
  • Pradeep  · 技术社区  · 7 年前

    我有一个用于在Azure Cosmos DB中创建数据库和集合的PowerShell脚本,我可以使用下面的PowerShell脚本在集合中插入一些虚拟记录。

        #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
    

    但我想从外部文件将记录插入Azure Cosmos DB,而不是将JSON数据直接放在PowerShell脚本中。

    1 回复  |  直到 7 年前
        1
  •  5
  •   Pradeep    7 年前

    将代码段添加到需要的位置,它将创建 .json

    $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"
    
    }
    

    我的 .json 文件:

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

    后果

    enter image description here

    enter image description here

    推荐文章