Search in sources :

Example 31 with DynamoDbException

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 "";
}
Also used : DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) DynamoDbSortKey(software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey) Key(software.amazon.awssdk.enhanced.dynamodb.Key) DynamoDbPartitionKey(software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey)

Example 32 with DynamoDbException

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);
    }
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest)

Example 33 with DynamoDbException

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);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) Instant(java.time.Instant) List(java.util.List) ArrayList(java.util.ArrayList) UUID(java.util.UUID) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Aggregations

DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)33 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)13 HashMap (java.util.HashMap)10 DynamoDbEnhancedClient (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)8 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)5 Instant (java.time.Instant)4 LocalDateTime (java.time.LocalDateTime)4 QueryConditional (software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional)4 Region (software.amazon.awssdk.regions.Region)4 ResourceNotFoundException (software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)4 LocalDate (java.time.LocalDate)3 DynamoDbPartitionKey (software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey)3 DynamoDbSortKey (software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey)3 CreateTableRequest (software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)3 CreateTableResponse (software.amazon.awssdk.services.dynamodb.model.CreateTableResponse)3 DescribeTableRequest (software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest)3 GetItemRequest (software.amazon.awssdk.services.dynamodb.model.GetItemRequest)3 ArrayList (java.util.ArrayList)2 Expression (software.amazon.awssdk.enhanced.dynamodb.Expression)2 Key (software.amazon.awssdk.enhanced.dynamodb.Key)2