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);
}
}
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!");
}
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!");
}
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);
}
}
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);
}
}
Aggregations