Best approach to deploy tables into table storage

前端 未结 1 2000
情歌与酒
情歌与酒 2021-01-29 07:27

Will you let me know,what is the best way to do table storage deployment as my dev team is asking like that they have many tables of which each table is having thousands of entr

1条回答
  •  孤城傲影
    2021-01-29 08:17

    There is InsertOrReplace(ITableEntity) method in TableBatchOperation Class that can do batch operation with ExecuteBatch method, try it with this code:

             param(
                     [object[]]$fileObj
                    )
    
                $storageAccountName = "XXX"
    
                $tableName="XXX"
    
                # Get the storage key for the storage account
                $StorageAccountKey = "XXX"
    
                # Get a storage context
                $ctx = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
    
                foreach($fo in $fileObj){
                 Write-Host $fo.filepath
                 $csv = Import-CSV $fo.filepath
                  $cArray=$fo.Cols.split(",")
            $table = Get-AzureStorageTable -Name $fo.tableName -Context $ctx -ErrorAction Ignore
                  [Microsoft.WindowsAzure.Storage.Table.TableBatchOperation]$batchOperation = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.TableBatchOperation
    
    if($table)
    {
    Write-Host "table not null"
    }
    else
    {
    Write-Host "table is null"
    }
    if($table.CloudTable)
    {
    Write-Host "CloudTable not null"
    }
    else
    {
    Write-Host "CloudTable is null"
    }
    
    foreach($line in $csv)
                    {
    
                    Write-Host "$($line.partitionkey), $($line.rowKey)"
                    $entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $line.partitionkey, $line.rowKey 
                        foreach($c in $cArray){
                     Write-Host "$c,$($line.$c)"
                        $entity.Properties.Add($c,$line.$c)
                 $batchOperation.Insert($entity)
    
                        }       
                 }
    if($batchOperation)
    {
    Write-Host "batchOperation not null"
    }
    else
    {
    Write-Host "batchOperation is null"
    }
        $table.CloudTable.ExecuteBatch($batchOperation)
                }
    

    0 讨论(0)
提交回复
热议问题