我是使用天蓝色存储表的新手。我试图批量插入我的实体,但我发现你不能使用不同的分区键进行批量操作。
有什么方法可以让我想在表格中插入大约 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; } }
}
我的方法是在不同的类中:
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);
}