-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathQueryConsistentRead.cs
69 lines (63 loc) · 2.56 KB
/
QueryConsistentRead.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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.DynamoDBv2.Model;
using DotnetSamples.Models;
namespace DotnetSamples.WorkingWithQueries
{
public class QueryConsistentRead
{
public static readonly DynamoDBOperationConfig DB_CONFIG =
new DynamoDBOperationConfig { OverrideTableName="MyTableName"};
public static async Task ConsistenReadDbContextExampleAsync()
{
try
{
var client = new AmazonDynamoDBClient(RegionEndpoint.USEast1);
var context = new DynamoDBContext(client);
var queryOperationConfig = new QueryOperationConfig();
queryOperationConfig.ConsistentRead = true;
var keyExpression = new Expression();
keyExpression.ExpressionStatement = "PK=:pk and begins_with(SK, :sk)";
keyExpression.ExpressionAttributeValues = new Dictionary<string, DynamoDBEntry>();
keyExpression.ExpressionAttributeValues.Add(":pk", "Customer1");
keyExpression.ExpressionAttributeValues.Add(":sk", "ORDER|");
queryOperationConfig.KeyExpression = keyExpression;
var queryResult = context.FromQueryAsync<CustomerOrder>(queryOperationConfig, DB_CONFIG);
var results = await queryResult.GetRemainingAsync();
Console.WriteLine("Success");
}
catch(Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
}
public static async Task ConsistentReadClientExampleAsync()
{
try
{
var client = new AmazonDynamoDBClient(RegionEndpoint.USEast1);
var request = new QueryRequest
{
TableName="MyTableName",
ConsistentRead=true,
KeyConditionExpression="PK = :pk AND begins_with(SK, :sk)",
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
{
{ ":pk", new AttributeValue { S="Customer1" } },
{ ":sk", new AttributeValue { S="ORDER|"} }
}
};
var response = await client.QueryAsync(request);
}
catch(Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
}
}
}