Skip to content

Instantly share code, notes, and snippets.

@OksanaH
Last active December 4, 2020 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OksanaH/42a1fa831ca73465e1a4d4d7e053dfac to your computer and use it in GitHub Desktop.
Save OksanaH/42a1fa831ca73465e1a4d4d7e053dfac to your computer and use it in GitHub Desktop.
private static void CopyFast()
{
Stopwatch sw = new Stopwatch();
sw.Start();
Task[] tasks = new Task[totalSegments];
for (int segment = 0; segment < totalSegments; segment++)
{
int tmpSegment = segment;
tasks[segment] = Task.Run(() => ScanSegment(tmpSegment));
}
Task.WaitAll(tasks);
sw.Stop();
Console.WriteLine($"Copy fast - {sw.ElapsedMilliseconds} milliseconds elapsed");
Console.ReadLine();
}
private static async Task ScanSegment(int tmpSegment)
{
var request = new ScanRequest
{
TableName = sourceTableName,
Segment = tmpSegment,
TotalSegments = totalSegments,
};
var result = await client.ScanAsync(request);
for (var i = 0; i < result.Items.Count; i += 25)
{
var items = result.Items.Skip(i).Take(25).ToArray();
var req = new BatchWriteItemRequest
{
RequestItems = new Dictionary<string, List<WriteRequest>>
{
{
destinationTableNameFast,
items.Select(i => new WriteRequest(new PutRequest(i))).ToList()
}
}
};
await client.BatchWriteItemAsync(req);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment