-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathDynamoDBProvider.java
74 lines (59 loc) · 2.64 KB
/
DynamoDBProvider.java
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
package software.amazon.lambda.powertools.parameters;
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
import software.amazon.awssdk.core.SdkSystemSetting;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.*;
import software.amazon.lambda.powertools.parameters.cache.CacheManager;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;
public class DynamoDBProvider extends BaseProvider {
private final DynamoDbClient client;
private final String tableName;
public DynamoDBProvider(CacheManager cacheManager, String tableName) {
this(cacheManager, DynamoDbClient.builder()
.httpClientBuilder(UrlConnectionHttpClient.builder())
//.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
//.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())))
.build(),
tableName
);
}
DynamoDBProvider(CacheManager cacheManager, DynamoDbClient client, String tableName) {
super(cacheManager);
this.client = client;
this.tableName = tableName;
}
@Override
protected String getValue(String key) {
GetItemResponse resp = client.getItem(GetItemRequest.builder()
.tableName(tableName)
.key(Collections.singletonMap("id", AttributeValue.fromS(key)))
.attributesToGet("val")
.build());
// If we have an item at the key, we should be able to get a 'val' out of it. If not it's
// exceptional.
// If we don't have an item at the key, we should return null.
if (resp.hasItem() && !resp.item().values().isEmpty()) {
return resp.item().get("val").s();
}
return null;
}
@Override
protected Map<String, String> getMultipleValues(String path) {
QueryResponse resp = client.query(QueryRequest.builder()
.tableName(tableName)
.keyConditionExpression("id = :v_id")
.expressionAttributeValues(Collections.singletonMap(":v_id", AttributeValue.fromS(path)))
.build());
return resp
.items()
.stream()
.collect(
Collectors.toMap(
(i) -> i.get("sk").s(),
(i) -> i.get("val").s()));
}
}