-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathBatchGetItem.cs
74 lines (63 loc) · 3.29 KB
/
BatchGetItem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
namespace DotnetSamples.WorkingWithItems
{
public class BatchGetItem
{
public static async Task ServiceClientExampleAsync()
{
var client = new AmazonDynamoDBClient(RegionEndpoint.USWest2);
var request = new BatchGetItemRequest
{
RequestItems = new Dictionary<string, KeysAndAttributes> {
{
//Table to read
"RetailDatabase",
//Definition of the keys to retrieve
new KeysAndAttributes
{
Keys = new List<Dictionary<string, AttributeValue>>
{
//List of dictionaries that define the keys of the items to retrieve
new Dictionary<string, AttributeValue> {
{ "pk", new AttributeValue { S = "[email protected]" }},
{ "sk", new AttributeValue { S = "metadata" }}
},
new Dictionary<string, AttributeValue> {
{ "pk", new AttributeValue { S = "[email protected]" }},
{ "sk", new AttributeValue { S = "metadata" }}
},
new Dictionary<string, AttributeValue> {
{ "pk", new AttributeValue { S = "[email protected]" }},
{ "sk", new AttributeValue { S = "metadata" }}
}
},
ConsistentRead = true, //If you don't need consistent reads use false instead to have cheaper retrieve prices
}
} //You can add more tables to read here.
},
ReturnConsumedCapacity = "TOTAL"
};
try
{
//Execute the BatchGet task to obtain the data
var responseBatch =await client.BatchGetItemAsync(request);
//Read every table and show all the content retrieved from the table.
foreach(var table in responseBatch.Responses)
{
Console.WriteLine($"Results of table {table.Key}: number {table.Value.Count}");
Console.WriteLine(JsonConvert.SerializeObject(table.Value));
}
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
}
}
}
}