Search in sources :

Example 11 with DynamoDbException

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

the class Query method queryTable.

// snippet-start:[dynamodb.java2.query.main]
public static int queryTable(DynamoDbClient ddb, String tableName, String partitionKeyName, String partitionKeyVal, String partitionAlias) {
    // Set up an alias for the partition key name in case it's a reserved word
    HashMap<String, String> attrNameAlias = new HashMap<String, String>();
    attrNameAlias.put(partitionAlias, partitionKeyName);
    // Set up mapping of the partition name with the value
    HashMap<String, AttributeValue> attrValues = new HashMap<String, AttributeValue>();
    attrValues.put(":" + partitionKeyName, AttributeValue.builder().s(partitionKeyVal).build());
    QueryRequest queryReq = QueryRequest.builder().tableName(tableName).keyConditionExpression(partitionAlias + " = :" + partitionKeyName).expressionAttributeNames(attrNameAlias).expressionAttributeValues(attrValues).build();
    try {
        QueryResponse response = ddb.query(queryReq);
        return response.count();
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return -1;
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) QueryResponse(software.amazon.awssdk.services.dynamodb.model.QueryResponse)

Example 12 with DynamoDbException

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

the class SyncPagination method autoPaginationWithResume.

public static void autoPaginationWithResume(DynamoDbClient client) {
    System.out.println("running AutoPagination with resume in case of errors...\n");
    ListTablesRequest listTablesRequest = ListTablesRequest.builder().limit(3).build();
    ListTablesIterable responses = client.listTablesPaginator(listTablesRequest);
    ListTablesResponse lastSuccessfulPage = null;
    try {
        for (ListTablesResponse response : responses) {
            response.tableNames().forEach(System.out::println);
            lastSuccessfulPage = response;
        }
    } catch (DynamoDbException exception) {
        if (lastSuccessfulPage != null) {
            System.out.println(exception.getMessage());
        }
    }
}
Also used : ListTablesIterable(software.amazon.awssdk.services.dynamodb.paginators.ListTablesIterable) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) ListTablesRequest(software.amazon.awssdk.services.dynamodb.model.ListTablesRequest) ListTablesResponse(software.amazon.awssdk.services.dynamodb.model.ListTablesResponse)

Example 13 with DynamoDbException

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

the class ScanEmployees method sendEmployeMessage.

public Boolean sendEmployeMessage() {
    Boolean send = false;
    String myDate = getDate();
    Region region = Region.US_WEST_2;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    // Create a DynamoDbEnhancedClient and use the DynamoDbClient object.
    DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
    // Create a DynamoDbTable object based on Employee.
    DynamoDbTable<Employee> table = enhancedClient.table("Employee", TableSchema.fromBean(Employee.class));
    try {
        AttributeValue attVal = AttributeValue.builder().s(myDate).build();
        // Get only items in the Employee table that match the date.
        Map<String, AttributeValue> myMap = new HashMap<>();
        myMap.put(":val1", attVal);
        Map<String, String> myExMap = new HashMap<>();
        myExMap.put("#startDate", "startDate");
        Expression expression = Expression.builder().expressionValues(myMap).expressionNames(myExMap).expression("#startDate = :val1").build();
        ScanEnhancedRequest enhancedRequest = ScanEnhancedRequest.builder().filterExpression(expression).limit(// you can increase this value.
        15).build();
        // Get items in the Employee table.
        Iterator<Employee> employees = table.scan(enhancedRequest).items().iterator();
        while (employees.hasNext()) {
            Employee employee = employees.next();
            String first = employee.getFirst();
            String phone = employee.getPhone();
            // Send an anniversary message.
            sentTextMessage(first, phone);
            send = true;
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return send;
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) ScanEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.ScanEnhancedRequest) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) Region(software.amazon.awssdk.regions.Region) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 14 with DynamoDbException

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

the class PersistCase method putRecord.

// Puts an item into a DynamoDB table
public void putRecord(String caseId, String employeeName, String email) {
    // Create a DynamoDbClient object
    Region region = Region.US_WEST_2;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    // Create a DynamoDbEnhancedClient and use the DynamoDbClient object
    DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
    try {
        // Create a DynamoDbTable object
        DynamoDbTable<Case> caseTable = enhancedClient.table("Case", TableSchema.fromBean(Case.class));
        // Create an Instant object
        LocalDate localDate = LocalDate.parse("2020-04-07");
        LocalDateTime localDateTime = localDate.atStartOfDay();
        Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
        // Populate the table
        Case caseRecord = new Case();
        caseRecord.setName(employeeName);
        caseRecord.setId(caseId);
        caseRecord.setEmail(email);
        caseRecord.setRegistrationDate(instant);
        // Put the case data into a DynamoDB table
        caseTable.putItem(caseRecord);
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("done");
}
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) Region(software.amazon.awssdk.regions.Region) LocalDate(java.time.LocalDate) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 15 with DynamoDbException

use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException 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)

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