Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amazon-dynamodb-quickstart with enhanced client example #1186

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion amazon-dynamodb-quickstart/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<quarkus-amazon-services.version>1.2.0</quarkus-amazon-services.version>
<quarkus-amazon-services.version>1.3.0</quarkus-amazon-services.version>
<awssdk.testcontainers.version>1.12.57</awssdk.testcontainers.version>
</properties>

Expand Down Expand Up @@ -52,6 +52,10 @@
<groupId>io.quarkiverse.amazonservices</groupId>
<artifactId>quarkus-amazon-dynamodb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.amazonservices</groupId>
<artifactId>quarkus-amazon-dynamodb-enhanced</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>url-connection-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.acme.dynamodb.enhanced;

import io.smallrye.mutiny.Uni;
import org.acme.dynamodb.Fruit;
import org.acme.dynamodb.FruitAsyncService;

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("/enhanced-async-fruits")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class FruitAsyncResource {

@Inject
FruitAsyncService service;

@GET
public Uni<List<Fruit>> getAll() {
return service.findAll();
}

@GET
@Path("{name}")
public Uni<Fruit> getSingle(String name) {
return service.get(name);
}

@POST
public Uni<List<Fruit>> add(Fruit fruit) {
service.add(fruit);
return getAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.acme.dynamodb.enhanced;

import io.smallrye.mutiny.Uni;
import org.acme.dynamodb.Fruit;
import software.amazon.awssdk.enhanced.dynamodb.*;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

@ApplicationScoped
public class FruitAsyncService {
private DynamoDbAsyncTable<FruitDynamoDbBean> fruitTable;

@Inject
FruitAsyncService(DynamoDbEnhancedAsyncClient dynamoEnhancedClient) {
fruitTable = dynamoEnhancedClient.table("QuarkusFruits",
TableSchema.fromClass(FruitDynamoDbBean.class));
}

public Uni<List<Fruit>> findAll() {

List<Fruit> listFruit = new LinkedList<>();

try {
fruitTable.scan().items().subscribe((a) -> {listFruit.add(convertToFruit(a));}).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
return Uni.createFrom().item(listFruit);

}

public Uni<List<Fruit>> add(Fruit fruit) {
return Uni.createFrom().completionStage(() -> fruitTable.putItem(convertToFruitDynamoDbBean(fruit)))
.onItem().ignore().andSwitchTo(this::findAll);
}

public Uni<Fruit> get(String name) {
Key key = Key.builder().partitionValue(name).build();
return Uni.createFrom().completionStage(() -> fruitTable.getItem(key))
.onItem().transform(resp -> convertToFruit(resp));
}

private Fruit convertToFruit(FruitDynamoDbBean dynamoDbBean) {
Fruit fruit = new Fruit();
fruit.setName(dynamoDbBean.getFruitName());
fruit.setDescription(dynamoDbBean.getFruitDescription());
return fruit;
}

private FruitDynamoDbBean convertToFruitDynamoDbBean(Fruit fruit) {
FruitDynamoDbBean fruitDynamoDbBean = new FruitDynamoDbBean();
fruitDynamoDbBean.setFruitDescription(fruit.getDescription());
fruitDynamoDbBean.setFruitName(fruit.getName());
return fruitDynamoDbBean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.acme.dynamodb.enhanced;

import io.quarkus.runtime.annotations.RegisterForReflection;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;

@RegisterForReflection
@DynamoDbBean
public class FruitDynamoDbBean {
private String fruitName;
private String fruitDescription;

@DynamoDbPartitionKey()
public String getFruitName() {
return fruitName;
}

public void setFruitName(String fruitName) {
this.fruitName = fruitName;
}

public String getFruitDescription() {
return fruitDescription;
}

public void setFruitDescription(String fruitDescription) {
this.fruitDescription = fruitDescription;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.acme.dynamodb.enhanced;

import org.acme.dynamodb.Fruit;

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("/enhanced-fruits")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class FruitResource {

@Inject
FruitSyncService service;

@GET
public List<Fruit> getAll() {
return service.findAll();
}

@GET
@Path("{name}")
public Fruit getSingle(String name) {
return service.get(name);
}

@POST
public List<Fruit> add(Fruit fruit) {
service.add(fruit);
return getAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.acme.dynamodb.enhanced;

import org.acme.dynamodb.Fruit;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.enhanced.dynamodb.Key;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.List;
import java.util.stream.Collectors;

@ApplicationScoped
public class FruitSyncService {
private DynamoDbTable<FruitDynamoDbBean> fruitTable;

@Inject
FruitSyncService(DynamoDbEnhancedClient dynamoEnhancedClient) {
fruitTable = dynamoEnhancedClient.table("QuarkusFruits",
TableSchema.fromClass(FruitDynamoDbBean.class));
}

public List<Fruit> findAll() {
return fruitTable.scan().items().stream().collect(Collectors.toList()).stream().map(this::convertToFruit).collect(Collectors.toList());
}

public List<Fruit> add(Fruit fruit) {
fruitTable.putItem(convertToFruitDynamoDbBean(fruit));
return findAll();
}

public Fruit get(String name) {
Key partitionKey = Key.builder().partitionValue(name).build();
return convertToFruit(fruitTable.getItem(partitionKey));
}

private Fruit convertToFruit(FruitDynamoDbBean dynamoDbBean) {
Fruit fruit = new Fruit();
fruit.setName(dynamoDbBean.getFruitName());
fruit.setDescription(dynamoDbBean.getFruitDescription());
return fruit;
}

private FruitDynamoDbBean convertToFruitDynamoDbBean(Fruit fruit) {
FruitDynamoDbBean fruitDynamoDbBean = new FruitDynamoDbBean();
fruitDynamoDbBean.setFruitDescription(fruit.getDescription());
fruitDynamoDbBean.setFruitName(fruit.getName());
return fruitDynamoDbBean;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class DynamodbResourcesTest {
.format("{\"name\":\"%s\", \"description\":\"%s\"}", name, description);

@ParameterizedTest
@ValueSource(strings = {"fruits", "async-fruits"})
@ValueSource(strings = {"fruits", "async-fruits", "enhanced-fruits", "enhanced-async-fruits"})
void testResource(final String testedResource) {
List<String> data = Arrays.asList("Cherry", "Pear");

Expand Down