Search in sources :

Example 16 with DynamoDbException

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

the class Scenario method scanMovies.

// snippet-end:[dynamodb.java2.scenario.query.main]
// snippet-start:[dynamodb.java2.scenario.scan.main]
// Scan the table.
public static void scanMovies(DynamoDbClient ddb, String tableName) {
    System.out.println("******* Scanning all movies.\n");
    try {
        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
        DynamoDbTable<Movies> custTable = enhancedClient.table("Movies", TableSchema.fromBean(Movies.class));
        Iterator<Movies> results = custTable.scan().items().iterator();
        while (results.hasNext()) {
            Movies rec = results.next();
            System.out.println("The movie title is " + rec.getTitle());
            System.out.println("The movie year is " + rec.getYear());
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 17 with DynamoDbException

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

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

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

the class DynamoDBService method setItem.

public void setItem(PopData pop) {
    // Create a DynamoDbEnhancedClient.
    DynamoDbClient ddb = getClient();
    DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
    try {
        // Create a DynamoDbTable object.
        DynamoDbTable<Population> workTable = enhancedClient.table("Country", TableSchema.fromBean(Population.class));
        // Populate the table.
        Population record = new Population();
        String name = pop.getName();
        String code = pop.getCode();
        record.setId(name);
        record.setCode(code);
        record.set2010(pop.get2010());
        record.set2011(pop.get2011());
        record.set2012(pop.get2012());
        record.set2013(pop.get2013());
        record.set2014(pop.get2014());
        record.set2015(pop.get2015());
        record.set2016(pop.get2016());
        record.set2017(pop.get2017());
        record.set2018(pop.get2018());
        record.set2019(pop.get2019());
        // Put the customer data into a DynamoDB table.
        workTable.putItem(record);
        System.out.println("Added record " + recNum);
        recNum++;
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 20 with DynamoDbException

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

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