use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.
the class EnhancedModifyItem method modifyItem.
// snippet-start:[dynamodb.java2.mapping.moditem.main]
public static String modifyItem(DynamoDbEnhancedClient enhancedClient, String keyVal, String email) {
try {
// Create a DynamoDbTable object
DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
// Create a KEY object
Key key = Key.builder().partitionValue(keyVal).build();
// Get the item by using the key and update the email value.
Customer customerRec = mappedTable.getItem(r -> r.key(key));
customerRec.setEmail(email);
mappedTable.updateItem(customerRec);
return customerRec.getEmail();
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.
the class GetItem method getDynamoDBItem.
// snippet-start:[dynamodb.java2.get_item.main]
public static void getDynamoDBItem(DynamoDbClient ddb, String tableName, String key, String keyVal) {
HashMap<String, AttributeValue> keyToGet = new HashMap<String, AttributeValue>();
keyToGet.put(key, AttributeValue.builder().s(keyVal).build());
GetItemRequest request = GetItemRequest.builder().key(keyToGet).tableName(tableName).build();
try {
Map<String, AttributeValue> returnedItem = ddb.getItem(request).item();
if (returnedItem != null) {
Set<String> keys = returnedItem.keySet();
System.out.println("Amazon DynamoDB table attributes: \n");
for (String key1 : keys) {
System.out.format("%s: %s\n", key1, returnedItem.get(key1).toString());
}
} else {
System.out.format("No item found with the key %s!\n", key);
}
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.
the class DynamoDBService method persistItem.
// Persist the PPE Items into the Gear table.
public void persistItem(List<ArrayList<GearItem>> gearList) {
DynamoDbClient ddb = getClient();
try {
DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
DynamoDbTable<Gear> gearTable = enhancedClient.table("Gear", TableSchema.fromBean(Gear.class));
Gear gearRecord;
// Create an Instant.
// current date and time
LocalDateTime now = LocalDateTime.now();
LocalDateTime timeVal = now.toLocalDate().atStartOfDay();
Instant instant = timeVal.toInstant(ZoneOffset.UTC);
// Persist the data into a DynamoDB table.
for (Object o : gearList) {
// Need to get the WorkItem from each list.
List innerList = (List) o;
for (Object value : innerList) {
gearRecord = new Gear();
UUID uuid = UUID.randomUUID();
GearItem gearItem = (GearItem) value;
gearRecord.setId(uuid.toString());
gearRecord.setKey(gearItem.getKey());
gearRecord.setDate(instant.toString());
gearRecord.setItem(gearItem.getName());
gearRecord.setCoverDescription(gearItem.getBodyCoverDescription());
gearRecord.setItemDescription(gearItem.getItemDescription());
gearRecord.setConfidence(gearItem.getConfidence());
// Put PPE data into a DynamoDB table.
gearTable.putItem(gearRecord);
}
}
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
Aggregations