[JAVA-15756] Upgraded dependencies versions + Updated code for dynamodb (#12959)

* [JAVA-15756] Upgraded dependencies versions + Updated code for dynamodb

* [JAVA-15756] Simplify/Clean up code

Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
panos-kakos 2022-11-06 17:24:04 +00:00 committed by GitHub
parent 2495e6f101
commit 198bf2181b
3 changed files with 24 additions and 22 deletions

View File

@ -95,8 +95,8 @@
<properties>
<json-simple.version>1.1.1</json-simple.version>
<aws-lambda-java-events.version>1.3.0</aws-lambda-java-events.version>
<aws-lambda-java-core.version>1.2.0</aws-lambda-java-core.version>
<aws-lambda-java-events.version>3.11.0</aws-lambda-java-events.version>
<aws-lambda-java-core.version>1.2.1</aws-lambda-java-core.version>
<gson.version>2.8.2</gson.version>
</properties>

View File

@ -1,12 +1,12 @@
package com.baeldung.lambda.dynamodb;
import com.amazonaws.regions.Region;
import java.util.HashMap;
import java.util.Map;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.PutItemOutcome;
import com.amazonaws.services.dynamodbv2.document.spec.PutItemSpec;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
@ -15,7 +15,7 @@ import com.baeldung.lambda.dynamodb.bean.PersonResponse;
public class SavePersonHandler implements RequestHandler<PersonRequest, PersonResponse> {
private DynamoDB dynamoDb;
private AmazonDynamoDB amazonDynamoDB;
private String DYNAMODB_TABLE_NAME = "Person";
private Regions REGION = Regions.US_WEST_2;
@ -30,20 +30,22 @@ public class SavePersonHandler implements RequestHandler<PersonRequest, PersonRe
return personResponse;
}
private PutItemOutcome persistData(PersonRequest personRequest) throws ConditionalCheckFailedException {
return this.dynamoDb.getTable(DYNAMODB_TABLE_NAME)
.putItem(
new PutItemSpec().withItem(new Item()
.withNumber("id", personRequest.getId())
.withString("firstName", personRequest.getFirstName())
.withString("lastName", personRequest.getLastName())
.withNumber("age", personRequest.getAge())
.withString("address", personRequest.getAddress())));
private void persistData(PersonRequest personRequest) throws ConditionalCheckFailedException {
Map<String, AttributeValue> attributesMap = new HashMap<>();
attributesMap.put("id", new AttributeValue(String.valueOf(personRequest.getId())));
attributesMap.put("firstName", new AttributeValue(personRequest.getFirstName()));
attributesMap.put("lastName", new AttributeValue(personRequest.getLastName()));
attributesMap.put("age", new AttributeValue(String.valueOf(personRequest.getAge())));
attributesMap.put("address", new AttributeValue(personRequest.getAddress()));
amazonDynamoDB.putItem(DYNAMODB_TABLE_NAME, attributesMap);
}
private void initDynamoDbClient() {
AmazonDynamoDBClient client = new AmazonDynamoDBClient();
client.setRegion(Region.getRegion(REGION));
this.dynamoDb = new DynamoDB(client);
this.amazonDynamoDB = AmazonDynamoDBClientBuilder.standard()
.withRegion(REGION)
.build();
}
}

View File

@ -22,7 +22,7 @@
</modules>
<properties>
<aws-java-sdk.version>1.11.290</aws-java-sdk.version>
<aws-java-sdk.version>1.12.331</aws-java-sdk.version>
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
</properties>