簡體   English   中英

Azure存儲表中的批量插入

[英]Batch Insert in Azure storage table

我是使用 azure 存儲表的新手。 我試圖批量插入我的實體,但我發現你不能使用不同的分區鍵進行批量操作。

有什么方法可以讓我想在表格中插入大約 10,000 - 20,000 個文件詳細信息。

這是我到目前為止所嘗試的:

public class Manifest:TableEntity
{
    private string name;
    private string extension;
    private string filePath;
    private string relativePath;
    private string mD5HashCode;
    private string lastModifiedDate;

    public void AssignRowKey()
    {
        this.RowKey = relativePath.ToString();
    }
    public void AssignPartitionKey()
    {
        this.PartitionKey = mD5HashCode;
    }
    public string Name { get { return name; } set { name = value; } }
    public string Extension { get { return extension; } set { extension = value; } }
    public string FilePath { get { return filePath; } set { filePath = value; } }
    public string RelativePath { get { return relativePath; } set { relativePath = value; } }
    public string MD5HashCode { get { return mD5HashCode; } set { mD5HashCode = value; } }
    public string LastModifiedDate { get { return lastModifiedDate; } set { lastModifiedDate = value; } }

}

我的方法是在不同的 class 中:

static async Task BatchInsert(CloudTable table, IEnumerable<FileDetails> files)
    {
        int rowOffset = 0;

        var tasks = new List<Task>();

        while (rowOffset < files.Count())
        {
            // next batch
            var rows = files.Skip(rowOffset).Take(100).ToList();

            rowOffset += rows.Count;                

            var task = Task.Factory.StartNew(() =>
            {                  

                var batch = new TableBatchOperation();

                foreach (var row in rows)
                {
                    Manifest manifestEntity = new Manifest
                    {
                        Name = row.Name,
                        Extension = row.Extension,
                        FilePath = row.FilePath,
                        RelativePath = row.RelativePath.Replace('\\', '+'),
                        MD5HashCode = row.Md5HashCode,
                        LastModifiedDate = row.LastModifiedDate.ToString()
                    };
                    manifestEntity.AssignPartitionKey();                        
                    manifestEntity.AssignRowKey();
                    batch.InsertOrReplace(manifestEntity);
                }

                // submit
                table.ExecuteBatch(batch);

            });

            tasks.Add(task);
        }

         await Task.WhenAll(tasks);
}

如果要使用批處理操作,批處理中的實體必須具有相同的 PartitionKey 不幸的是,別無選擇,只能將它們單獨保存在您的情況下。

分區鍵甚至存在的原因是 Azure 可以跨機器分發數據,而無需分區之間的協調。 該系統被設計成不能在同一事務或操作中使用不同的分區。

您可以對該問題進行投票以推進該功能的實現。

相對而言,沒有辦法使用批量操作插入多個沒有相同分區鍵的實體。

批處理操作的一些限制是

  • 單個批處理操作中的所有實體必須具有相同的分區鍵。
  • 單個批處理操作只能包含 100 個實體。

或者,您可以使用“TableOperation.Insert()”插入實體,它允許您插入具有相同分區鍵的實體。

在 2022 年,那里有一些已棄用的 nuget-packages。 據我研究,實際執行此操作的 nuget-package 將是Azure.Data.Tables

這里有一些很好的例子,如何通過這個 package 使用批處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM