forked from aws-samples/aws-dynamodb-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransactGetItems.cs
83 lines (69 loc) · 3.03 KB
/
TransactGetItems.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
75
76
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
namespace DotnetSamples.WorkingWithItems
{
public class TransactGetItems
{
/// <summary>
/// Example that gets items in a transaction from DynamoDB
/// </summary>
/// <returns></returns>
public static async Task TransactGetItemsExample()
{
var client = new AmazonDynamoDBClient(RegionEndpoint.USWest2);
var request = new TransactGetItemsRequest
{
//Up to 25 Items to retrieve from one or more tables but from one or more indexes for a same table.
TransactItems = new List<TransactGetItem>
{
new TransactGetItem()
{
Get = new Get()
{
TableName = "RetailDatabase",
Key = new Dictionary<string, AttributeValue>
{
{ "pk", new AttributeValue { S = "[email protected]" }},
{ "sk", new AttributeValue { S = "metadata" }}
},
ProjectionExpression = "pk,sk", //Specific attributes to retrieve from the table separated by comas if empty all will be returned
}
},
new TransactGetItem()
{
Get = new Get()
{
TableName = "RetailDatabase",
Key = new Dictionary<string, AttributeValue>
{
{ "pk", new AttributeValue { S = "[email protected]" }},
{ "sk", new AttributeValue { S = "metadata" }}
},
}
}
},
ReturnConsumedCapacity = "TOTAL"
};
try
{
var responseTransaction = await client.TransactGetItemsAsync(request);
foreach (var item in responseTransaction.Responses)
{
foreach(var attribute in item.Item)
{
Console.WriteLine($"{attribute.Key} : { attribute.Value.S}"); // For the example I'm supposing all the values are string, use the type property that corresponds
}
Console.WriteLine(string.Empty);
}
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
}
}
}
}