forked from aws-samples/aws-dynamodb-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableAsyncQueryIndex.java
107 lines (79 loc) · 3.51 KB
/
TableAsyncQueryIndex.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.example.myapp;
import software.amazon.awssdk.core.async.SdkPublisher;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbAsyncIndex;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbAsyncTable;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedAsyncClient;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSecondaryPartitionKey;
import software.amazon.awssdk.enhanced.dynamodb.model.Page;
import software.amazon.awssdk.enhanced.dynamodb.model.PagePublisher;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient;
import static software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional.keyEqualTo;
public class TableAsyncQueryIndex {
public static void main(String[] args) {
// Create Async Client
DynamoDbAsyncClient client = DynamoDbAsyncClient.builder()
.region(Region.EU_WEST_1)
.build();
// Create Enhanced Async Client, Passing Async Client
DynamoDbEnhancedAsyncClient enhancedClient = DynamoDbEnhancedAsyncClient.builder()
.dynamoDbClient(client)
.build();
try {
// Mapping a Table to DynamoDB Async Table
DynamoDbAsyncTable<Customer> customerTable = enhancedClient.table("test",
TableSchema.fromBean(Customer.class));
// Mapping an Index to DynamoDB Async Index
DynamoDbAsyncIndex<Customer> customersByName = customerTable.index("email-index");
// Perform Query and Pass Result to SDK Publisher
SdkPublisher<Page<Customer>> customersWithName =
customersByName.query(r -> r.queryConditional(keyEqualTo(k -> k.partitionValue("[email protected]"))));
// Create a Page Publisher with our Result
PagePublisher<Customer> pages = PagePublisher.create(customersWithName);
// Subscribe to our Result and Print Result on Each Returned Item per Page
pages.items().subscribe(item -> System.out.println(item.getName() + " " + item.getId()));
// Keep Thread Alive For Results (only needed for short lived threads/testing purposes)
Thread.sleep(3000);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// Customer Class
@DynamoDbBean
public static class Customer {
private String id;
private String name;
private String email;
private Long versionId;
public Customer() {
}
// Getter/Setter
@DynamoDbPartitionKey
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@DynamoDbSecondaryPartitionKey(indexNames = { "email-index" })
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getVersionId() { return versionId; }
public void setVersionId(Long versionId) {
this.versionId = versionId;
}
}
}