这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions aws-modules/aws-dynamodb-v2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target/
.idea/
113 changes: 113 additions & 0 deletions aws-modules/aws-dynamodb-v2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>aws-dynamodb-v2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>aws-dynamodb-v2</name>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>aws-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
<version>2.31.23</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>localstack</artifactId>
<version>1.20.6</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.20.6</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sdk-core</artifactId>
<version>2.31.23</version>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-core</artifactId>
<version>2.31.23</version>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>netty-nio-client</artifactId>
<version>2.31.23</version>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>utils</artifactId>
<version>2.31.23</version>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>identity-spi</artifactId>
<version>2.31.23</version>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>checksums</artifactId>
<version>2.31.23</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-plugins-version}</version>
<executions>
<execution>
<id>copy</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope></includeScope>
<includeTypes>so,dll,dylib</includeTypes>
<outputDirectory>native-libs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<aws-java-sdk.version>1.12.331</aws-java-sdk.version>
<gson.version>2.11.0</gson.version>
<dynamodblocal.version>1.21.1</dynamodblocal.version>
<maven-plugins-version>3.1.1</maven-plugins-version>
</properties>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ public List<Map<String, AttributeValue>> getOrdersByUserId(String userId) {
))
.build();

return dynamoDb.query(request).items();
QueryResponse response = dynamoDb.query(request);

List<Map<String, AttributeValue>> items = response.items();

for (Map<String, AttributeValue> item : items) {
System.out.println("Order item: " + item.get("item").s());
}

return response.items();
}

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

return dynamoDb.query(request).items();
}

public List<Map<String, AttributeValue>> getAllOrdersPaginated(String userId) {
List<Map<String, AttributeValue>> allItems = new ArrayList<>();
Map<String, AttributeValue> lastKey = null;

do {
QueryRequest.Builder requestBuilder = QueryRequest.builder()
.tableName("UserOrders")
.keyConditionExpression("userId = :uid")
.expressionAttributeValues(Map.of(
":uid", AttributeValue.fromS(userId)
));

if (lastKey != null) {
requestBuilder.exclusiveStartKey(lastKey);
}

QueryResponse response = dynamoDb.query(requestBuilder.build());
allItems.addAll(response.items());
lastKey = response.lastEvaluatedKey();
} while (lastKey != null && !lastKey.isEmpty());

return allItems;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ void givenMonthPrefix_whenGetOrdersByMonth_thenReturnMonthlyOrders() {
assertEquals(List.of("Mouse", "Keyboard"), names);
}

@Test
void givenUserId_whenGetAllOrdersPaginated_thenReturnAllUserOrders() {
List<Map<String, AttributeValue>> items = repository.getAllOrdersPaginated("user1");

assertEquals(4, items.size());

List<String> itemNames = items.stream()
.map(item -> item.get("item").s())
.collect(Collectors.toList());

assertTrue(itemNames.containsAll(List.of("Laptop", "Monitor", "Mouse", "Keyboard")));
}

@AfterAll
void tearDown() {
if (localstack != null) {
Expand Down
18 changes: 0 additions & 18 deletions aws-modules/aws-dynamodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,6 @@
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
<version>2.31.23</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>localstack</artifactId>
<version>1.20.6</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.20.6</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
1 change: 1 addition & 0 deletions aws-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<module>amazon-textract</module>
<module>aws-app-sync</module>
<module>aws-dynamodb</module>
<module>aws-dynamodb-v2</module>
<module>aws-lambda-modules</module>
<module>aws-miscellaneous</module>
<module>aws-reactive</module>
Expand Down