Skip to content

Commit cfd579b

Browse files
BAEL-9218 - move code to separate module (#18507)
1 parent aec0821 commit cfd579b

File tree

6 files changed

+162
-19
lines changed

6 files changed

+162
-19
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target/
2+
.idea/
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>aws-dynamodb-v2</artifactId>
7+
<version>0.1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
<name>aws-dynamodb-v2</name>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>aws-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>software.amazon.awssdk</groupId>
20+
<artifactId>dynamodb</artifactId>
21+
<version>2.31.23</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.testcontainers</groupId>
25+
<artifactId>localstack</artifactId>
26+
<version>1.20.6</version>
27+
<scope>test</scope>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>org.testcontainers</groupId>
32+
<artifactId>testcontainers</artifactId>
33+
<version>1.20.6</version>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>software.amazon.awssdk</groupId>
39+
<artifactId>sdk-core</artifactId>
40+
<version>2.31.23</version>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>software.amazon.awssdk</groupId>
45+
<artifactId>aws-core</artifactId>
46+
<version>2.31.23</version>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>software.amazon.awssdk</groupId>
51+
<artifactId>netty-nio-client</artifactId>
52+
<version>2.31.23</version>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>software.amazon.awssdk</groupId>
57+
<artifactId>utils</artifactId>
58+
<version>2.31.23</version>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>software.amazon.awssdk</groupId>
63+
<artifactId>identity-spi</artifactId>
64+
<version>2.31.23</version>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>software.amazon.awssdk</groupId>
69+
<artifactId>checksums</artifactId>
70+
<version>2.31.23</version>
71+
</dependency>
72+
</dependencies>
73+
74+
<build>
75+
<plugins>
76+
<plugin>
77+
<groupId>org.apache.maven.plugins</groupId>
78+
<artifactId>maven-dependency-plugin</artifactId>
79+
<version>${maven-plugins-version}</version>
80+
<executions>
81+
<execution>
82+
<id>copy</id>
83+
<phase>compile</phase>
84+
<goals>
85+
<goal>copy-dependencies</goal>
86+
</goals>
87+
<configuration>
88+
<includeScope></includeScope>
89+
<includeTypes>so,dll,dylib</includeTypes>
90+
<outputDirectory>native-libs</outputDirectory>
91+
</configuration>
92+
</execution>
93+
</executions>
94+
</plugin>
95+
<plugin>
96+
<groupId>org.apache.maven.plugins</groupId>
97+
<artifactId>maven-compiler-plugin</artifactId>
98+
<configuration>
99+
<source>9</source>
100+
<target>9</target>
101+
</configuration>
102+
</plugin>
103+
</plugins>
104+
</build>
105+
106+
<properties>
107+
<aws-java-sdk.version>1.12.331</aws-java-sdk.version>
108+
<gson.version>2.11.0</gson.version>
109+
<dynamodblocal.version>1.21.1</dynamodblocal.version>
110+
<maven-plugins-version>3.1.1</maven-plugins-version>
111+
</properties>
112+
113+
</project>

aws-modules/aws-dynamodb/src/main/java/com/baeldung/dynamodb/query/UserOrdersRepository.java renamed to aws-modules/aws-dynamodb-v2/src/main/java/com/baeldung/dynamodb/query/UserOrdersRepository.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ public List<Map<String, AttributeValue>> getOrdersByUserId(String userId) {
2222
))
2323
.build();
2424

25-
return dynamoDb.query(request).items();
25+
QueryResponse response = dynamoDb.query(request);
26+
27+
List<Map<String, AttributeValue>> items = response.items();
28+
29+
for (Map<String, AttributeValue> item : items) {
30+
System.out.println("Order item: " + item.get("item").s());
31+
}
32+
33+
return response.items();
2634
}
2735

2836
public List<Map<String, AttributeValue>> getOrdersAfterDate(String userId, String startDate) {
@@ -64,4 +72,28 @@ public List<Map<String, AttributeValue>> getOrdersByMonth(String userId, String
6472

6573
return dynamoDb.query(request).items();
6674
}
75+
76+
public List<Map<String, AttributeValue>> getAllOrdersPaginated(String userId) {
77+
List<Map<String, AttributeValue>> allItems = new ArrayList<>();
78+
Map<String, AttributeValue> lastKey = null;
79+
80+
do {
81+
QueryRequest.Builder requestBuilder = QueryRequest.builder()
82+
.tableName("UserOrders")
83+
.keyConditionExpression("userId = :uid")
84+
.expressionAttributeValues(Map.of(
85+
":uid", AttributeValue.fromS(userId)
86+
));
87+
88+
if (lastKey != null) {
89+
requestBuilder.exclusiveStartKey(lastKey);
90+
}
91+
92+
QueryResponse response = dynamoDb.query(requestBuilder.build());
93+
allItems.addAll(response.items());
94+
lastKey = response.lastEvaluatedKey();
95+
} while (lastKey != null && !lastKey.isEmpty());
96+
97+
return allItems;
98+
}
6799
}

aws-modules/aws-dynamodb/src/test/java/com/baeldung/dynamodb/query/UserOrdersRepositoryIntegrationTest.java renamed to aws-modules/aws-dynamodb-v2/src/test/java/com/baeldung/dynamodb/query/UserOrdersRepositoryIntegrationTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@ void givenMonthPrefix_whenGetOrdersByMonth_thenReturnMonthlyOrders() {
108108
assertEquals(List.of("Mouse", "Keyboard"), names);
109109
}
110110

111+
@Test
112+
void givenUserId_whenGetAllOrdersPaginated_thenReturnAllUserOrders() {
113+
List<Map<String, AttributeValue>> items = repository.getAllOrdersPaginated("user1");
114+
115+
assertEquals(4, items.size());
116+
117+
List<String> itemNames = items.stream()
118+
.map(item -> item.get("item").s())
119+
.collect(Collectors.toList());
120+
121+
assertTrue(itemNames.containsAll(List.of("Laptop", "Monitor", "Mouse", "Keyboard")));
122+
}
123+
111124
@AfterAll
112125
void tearDown() {
113126
if (localstack != null) {

aws-modules/aws-dynamodb/pom.xml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,6 @@
3636
<artifactId>gson</artifactId>
3737
<version>${gson.version}</version>
3838
</dependency>
39-
<dependency>
40-
<groupId>software.amazon.awssdk</groupId>
41-
<artifactId>dynamodb</artifactId>
42-
<version>2.31.23</version>
43-
</dependency>
44-
<dependency>
45-
<groupId>org.testcontainers</groupId>
46-
<artifactId>localstack</artifactId>
47-
<version>1.20.6</version>
48-
<scope>test</scope>
49-
</dependency>
50-
51-
<dependency>
52-
<groupId>org.testcontainers</groupId>
53-
<artifactId>testcontainers</artifactId>
54-
<version>1.20.6</version>
55-
<scope>test</scope>
56-
</dependency>
5739
</dependencies>
5840

5941
<build>

aws-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<module>amazon-textract</module>
1919
<module>aws-app-sync</module>
2020
<module>aws-dynamodb</module>
21+
<module>aws-dynamodb-v2</module>
2122
<module>aws-lambda-modules</module>
2223
<module>aws-miscellaneous</module>
2324
<module>aws-reactive</module>

0 commit comments

Comments
 (0)