[英]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 可以跨机器分发数据,而无需分区之间的协调。 该系统被设计成不能在同一事务或操作中使用不同的分区。
您可以对该问题进行投票以推进该功能的实现。
相对而言,没有办法使用批量操作插入多个没有相同分区键的实体。
批处理操作的一些限制是
或者,您可以使用“TableOperation.Insert()”插入实体,它允许您插入具有相同分区键的实体。
在 2022 年,那里有一些已弃用的 nuget-packages。 据我研究,实际执行此操作的 nuget-package 将是Azure.Data.Tables
这里有一些很好的例子,如何通过这个 package 使用批处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.