Search in sources :

Example 1 with ResourceNotFoundException

use of software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException in project aws-doc-sdk-examples by awsdocs.

the class PutItemEncrypt method putItemInTable.

// snippet-start:[dynamodb.java2.put_item_enc.main]
public static void putItemInTable(DynamoDbClient ddb, KmsClient kmsClient, String tableName, String key, String keyVal, String albumTitle, String albumTitleValue, String awards, String awardVal, String songTitle, String songTitleVal, String keyId) {
    HashMap<String, AttributeValue> itemValues = new HashMap<String, AttributeValue>();
    // Encrypt the albumTitleValue before writing it to the table.
    SdkBytes myBytes = SdkBytes.fromUtf8String(albumTitleValue);
    EncryptRequest encryptRequest = EncryptRequest.builder().keyId(keyId).plaintext(myBytes).build();
    EncryptResponse response = kmsClient.encrypt(encryptRequest);
    // Get the encrypted data.
    SdkBytes encryptedData = response.ciphertextBlob();
    // Add content to the table.
    itemValues.put(key, AttributeValue.builder().s(keyVal).build());
    itemValues.put(songTitle, AttributeValue.builder().s(songTitleVal).build());
    itemValues.put(albumTitle, AttributeValue.builder().bs(encryptedData).build());
    itemValues.put(awards, AttributeValue.builder().s(awardVal).build());
    PutItemRequest request = PutItemRequest.builder().tableName(tableName).item(itemValues).build();
    try {
        ddb.putItem(request);
        System.out.println(tableName + " was successfully updated");
    } catch (ResourceNotFoundException e) {
        System.err.format("Error: The Amazon DynamoDB table \"%s\" can't be found.\n", tableName);
        System.err.println("Be sure that it exists and that you've typed its name correctly!");
        System.exit(1);
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) SdkBytes(software.amazon.awssdk.core.SdkBytes) EncryptResponse(software.amazon.awssdk.services.kms.model.EncryptResponse) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException) EncryptRequest(software.amazon.awssdk.services.kms.model.EncryptRequest)

Example 2 with ResourceNotFoundException

use of software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException in project aws-doc-sdk-examples by awsdocs.

the class Scenario method updateTableItem.

// snippet-end:[dynamodb.java2.scenario.populate_table.main]
// Update the record to include show only directors.
public static void updateTableItem(DynamoDbClient ddb, String tableName) {
    HashMap<String, AttributeValue> itemKey = new HashMap<String, AttributeValue>();
    // Specify the key and sort key.
    itemKey.put("year", AttributeValue.builder().n("1933").build());
    itemKey.put("title", AttributeValue.builder().s("King Kong").build());
    HashMap<String, AttributeValueUpdate> updatedValues = new HashMap<String, AttributeValueUpdate>();
    // Update the column specified by info with updatedVal.
    updatedValues.put("info", AttributeValueUpdate.builder().value(AttributeValue.builder().s("{\"directors\":[\"Merian C. Cooper\",\"Ernest B. Schoedsack\"]").build()).action(AttributeAction.PUT).build());
    UpdateItemRequest request = UpdateItemRequest.builder().tableName(tableName).key(itemKey).attributeUpdates(updatedValues).build();
    try {
        ddb.updateItem(request);
    } catch (ResourceNotFoundException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("Item was updated!");
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) AttributeValueUpdate(software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)

Example 3 with ResourceNotFoundException

use of software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException in project aws-doc-sdk-examples by awsdocs.

the class UpdateItem method updateTableItem.

// snippet-start:[dynamodb.java2.update_item.main]
public static void updateTableItem(DynamoDbClient ddb, String tableName, String key, String keyVal, String name, String updateVal) {
    HashMap<String, AttributeValue> itemKey = new HashMap<String, AttributeValue>();
    itemKey.put(key, AttributeValue.builder().s(keyVal).build());
    HashMap<String, AttributeValueUpdate> updatedValues = new HashMap<String, AttributeValueUpdate>();
    // Update the column specified by name with updatedVal
    updatedValues.put(name, AttributeValueUpdate.builder().value(AttributeValue.builder().s(updateVal).build()).action(AttributeAction.PUT).build());
    UpdateItemRequest request = UpdateItemRequest.builder().tableName(tableName).key(itemKey).attributeUpdates(updatedValues).build();
    try {
        ddb.updateItem(request);
    } catch (ResourceNotFoundException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) AttributeValueUpdate(software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)

Example 4 with ResourceNotFoundException

use of software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException in project cas by apereo.

the class DynamoDbTableUtils method waitForTableDescription.

private static TableDescription waitForTableDescription(final DynamoDbClient dynamo, final String tableName, final TableStatus desiredStatus, final int timeout, final int interval) throws Exception {
    val startTime = System.currentTimeMillis();
    val endTime = startTime + timeout;
    val tableRequest = DescribeTableRequest.builder().tableName(tableName).build();
    TableDescription table = null;
    while (System.currentTimeMillis() < endTime) {
        try {
            table = dynamo.describeTable(tableRequest).table();
            if (desiredStatus == null || table.tableStatusAsString().equals(desiredStatus.toString())) {
                return table;
            }
        } catch (final ResourceNotFoundException rnfe) {
            LOGGER.trace(rnfe.getMessage());
        }
        Thread.sleep(interval);
    }
    return table;
}
Also used : lombok.val(lombok.val) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException) TableDescription(software.amazon.awssdk.services.dynamodb.model.TableDescription)

Example 5 with ResourceNotFoundException

use of software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException in project aws-doc-sdk-examples by awsdocs.

the class PutItem method putItemInTable.

// snippet-start:[dynamodb.java2.put_item.main]
public static void putItemInTable(DynamoDbClient ddb, String tableName, String key, String keyVal, String albumTitle, String albumTitleValue, String awards, String awardVal, String songTitle, String songTitleVal) {
    HashMap<String, AttributeValue> itemValues = new HashMap<String, AttributeValue>();
    // Add all content to the table
    itemValues.put(key, AttributeValue.builder().s(keyVal).build());
    itemValues.put(songTitle, AttributeValue.builder().s(songTitleVal).build());
    itemValues.put(albumTitle, AttributeValue.builder().s(albumTitleValue).build());
    itemValues.put(awards, AttributeValue.builder().s(awardVal).build());
    PutItemRequest request = PutItemRequest.builder().tableName(tableName).item(itemValues).build();
    try {
        ddb.putItem(request);
        System.out.println(tableName + " was successfully updated");
    } catch (ResourceNotFoundException e) {
        System.err.format("Error: The Amazon DynamoDB table \"%s\" can't be found.\n", tableName);
        System.err.println("Be sure that it exists and that you've typed its name correctly!");
        System.exit(1);
    } 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) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)

Aggregations

ResourceNotFoundException (software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)5 HashMap (java.util.HashMap)4 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)4 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)4 AttributeValueUpdate (software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate)2 PutItemRequest (software.amazon.awssdk.services.dynamodb.model.PutItemRequest)2 UpdateItemRequest (software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest)2 lombok.val (lombok.val)1 SdkBytes (software.amazon.awssdk.core.SdkBytes)1 TableDescription (software.amazon.awssdk.services.dynamodb.model.TableDescription)1 EncryptRequest (software.amazon.awssdk.services.kms.model.EncryptRequest)1 EncryptResponse (software.amazon.awssdk.services.kms.model.EncryptResponse)1