Search in sources :

Example 26 with DynamoDbException

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

the class Scenario method deleteDynamoDBTable.

public static void deleteDynamoDBTable(DynamoDbClient ddb, String tableName) {
    DeleteTableRequest request = DeleteTableRequest.builder().tableName(tableName).build();
    try {
        ddb.deleteTable(request);
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println(tableName + " was successfully deleted!");
}
Also used : DeleteTableRequest(software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException)

Example 27 with DynamoDbException

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

the class Scenario method getItem.

// snippet-start:[dynamodb.java2.scenario.get_item.main]
public static void getItem(DynamoDbClient ddb) {
    HashMap<String, AttributeValue> keyToGet = new HashMap<String, AttributeValue>();
    keyToGet.put("year", AttributeValue.builder().n("1933").build());
    keyToGet.put("title", AttributeValue.builder().s("King Kong").build());
    GetItemRequest request = GetItemRequest.builder().key(keyToGet).tableName("Movies").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", "year");
        }
    } 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 28 with DynamoDbException

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

the class DynamoDBScanItems method scanItems.

// snippet-start:[dynamodb.java2.dynamoDB_scan.main]
public static void scanItems(DynamoDbClient ddb, String tableName) {
    try {
        ScanRequest scanRequest = ScanRequest.builder().tableName(tableName).build();
        ScanResponse response = ddb.scan(scanRequest);
        for (Map<String, AttributeValue> item : response.items()) {
            Set<String> keys = item.keySet();
            for (String key : keys) {
                System.out.println("The key name is " + key + "\n");
                System.out.println("The value is " + item.get(key).s());
            }
        }
    } catch (DynamoDbException e) {
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : ScanRequest(software.amazon.awssdk.services.dynamodb.model.ScanRequest) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) ScanResponse(software.amazon.awssdk.services.dynamodb.model.ScanResponse)

Example 29 with DynamoDbException

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

the class EnhancedPutItem method putRecord.

// snippet-start:[dynamodb.java2.mapping.putitem.main]
public static void putRecord(DynamoDbEnhancedClient enhancedClient) {
    try {
        DynamoDbTable<Customer> custTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
        // Create an Instant
        LocalDate localDate = LocalDate.parse("2020-04-07");
        LocalDateTime localDateTime = localDate.atStartOfDay();
        Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
        // Populate the Table
        Customer custRecord = new Customer();
        custRecord.setCustName("Susan red");
        custRecord.setId("id146");
        custRecord.setEmail("sred@noserver.com");
        custRecord.setRegistrationDate(instant);
        // Put the customer data into a DynamoDB table
        custTable.putItem(custRecord);
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("done");
}
Also used : LocalDateTime(java.time.LocalDateTime) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) Instant(java.time.Instant) LocalDate(java.time.LocalDate)

Example 30 with DynamoDbException

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

the class DeleteItem method deleteDymamoDBItem.

// snippet-start:[dynamodb.java2.delete_item.main]
public static void deleteDymamoDBItem(DynamoDbClient ddb, String tableName, String key, String keyVal) {
    HashMap<String, AttributeValue> keyToGet = new HashMap<String, AttributeValue>();
    keyToGet.put(key, AttributeValue.builder().s(keyVal).build());
    DeleteItemRequest deleteReq = DeleteItemRequest.builder().tableName(tableName).key(keyToGet).build();
    try {
        ddb.deleteItem(deleteReq);
    } 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) DeleteItemRequest(software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException)

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