【发布时间】:2019-02-01 06:34:25
【问题描述】:
我有一个用于在 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 脚本中。
那么,谁能建议我如何将数据从外部文件插入 Azure Cosmos DB。
【问题讨论】:
-
您能提供文件样本吗?是
.json文件吗? -
外部文件的驻留是否稳定?例如 Azure Blob 存储。
-
@Joy,它只是一个 .json 文件。
-
目前 .json 文件仅位于我的本地计算机中。
-
文件中的内容是什么?可以提供样品吗?
标签: azure powershell azure-cosmosdb