DynamoDBEnhanced.java - AWS Code Sample

DynamoDBEnhanced.java

/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.example.handlingformsubmission; import static software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.primaryPartitionKey; import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; import software.amazon.awssdk.enhanced.dynamodb.TableSchema; import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableSchema; import software.amazon.awssdk.enhanced.dynamodb.model.PutItemEnhancedRequest; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput; import org.springframework.stereotype.Component; @Component("DynamoDBEnhanced") public class DynamoDBEnhanced { private final ProvisionedThroughput DEFAULT_PROVISIONED_THROUGHPUT = ProvisionedThroughput.builder() .readCapacityUnits(50L) .writeCapacityUnits(50L) .build(); private final TableSchema<GreetingItems> TABLE_SCHEMA = StaticTableSchema.builder(GreetingItems.class) .newItemSupplier(GreetingItems::new) .addAttribute(String.class, a -> a.name("idblog") .getter(GreetingItems::getId) .setter(GreetingItems::setId) .tags(primaryPartitionKey())) .addAttribute(String.class, a -> a.name("author") .getter(GreetingItems::getName) .setter(GreetingItems::setName)) .addAttribute(String.class, a -> a.name("title") .getter(GreetingItems::getTitle) .setter(GreetingItems::setTitle)) .addAttribute(String.class, a -> a.name("body") .getter(GreetingItems::getMessage) .setter(GreetingItems::setMessage)) .build(); // Uses the Enhanced Client to inject a new post into a DynamoDB table public void injectDynamoItem(Greeting item){ Region region = Region.US_EAST_1; DynamoDbClient ddb = DynamoDbClient.builder() .region(region) .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) .build(); try { DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder() .dynamoDbClient(ddb) .build(); //Create a DynamoDbTable object DynamoDbTable<GreetingItems> mappedTable = enhancedClient.table("Greeting", TABLE_SCHEMA); GreetingItems gi = new GreetingItems(); gi.setName(item.getName()); gi.setMessage(item.getBody()); gi.setTitle(item.getTitle()); gi.setId(item.getId()); PutItemEnhancedRequest enReq = PutItemEnhancedRequest.builder(GreetingItems.class) .item(gi) .build(); mappedTable.putItem(enReq); } catch (Exception e) { e.getStackTrace(); } } public class GreetingItems { //Set up Data Members that correspond to columns in the Work table private String id; private String name; private String message; private String title; public GreetingItems() { } public String getId() { return this.id; } public void setId(String id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getMessage(){ return this.message; } public void setMessage(String message){ this.message = message; } public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } } }