use of software.amazon.awssdk.services.dynamodb.DynamoDbClient in project aws-doc-sdk-examples by awsdocs.
the class UpdateItem method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " UpdateItem <tableName> <key> <keyVal> <name> <updateVal>\n\n" + "Where:\n" + " tableName - the Amazon DynamoDB table (for example, Music3).\n" + " key - the name of the key in the table (for example, Artist).\n" + " keyVal - the value of the key (for example, Famous Band).\n" + " name - the name of the column where the value is updated (for example, Awards).\n" + " updateVal - the value used to update an item (for example, 14).\n" + " Example:\n" + " UpdateItem Music3 Artist Famous Band Awards 14\n";
if (args.length != 5) {
System.out.println(USAGE);
System.exit(1);
}
String tableName = args[0];
String key = args[1];
String keyVal = args[2];
String name = args[3];
String updateVal = args[4];
Region region = Region.US_EAST_1;
DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
updateTableItem(ddb, tableName, key, keyVal, name, updateVal);
ddb.close();
}
use of software.amazon.awssdk.services.dynamodb.DynamoDbClient in project aws-doc-sdk-examples by awsdocs.
the class DynamoDBService method getClient.
private DynamoDbClient getClient() {
// Create a DynamoDbClient object.
Region region = Region.US_EAST_1;
DynamoDbClient ddb = DynamoDbClient.builder().credentialsProvider(EnvironmentVariableCredentialsProvider.create()).region(region).build();
return ddb;
}
use of software.amazon.awssdk.services.dynamodb.DynamoDbClient in project aws-doc-sdk-examples by awsdocs.
the class DynamoDBService method archiveItem.
// Archives an item based on the key.
public String archiveItem(String id) {
DynamoDbClient ddb = getClient();
HashMap<String, AttributeValue> itemKey = new HashMap<String, AttributeValue>();
itemKey.put("id", AttributeValue.builder().s(id).build());
HashMap<String, AttributeValueUpdate> updatedValues = new HashMap<String, AttributeValueUpdate>();
// Update the column specified by name with updatedVal.
updatedValues.put("archive", AttributeValueUpdate.builder().value(AttributeValue.builder().s("Closed").build()).action(AttributeAction.PUT).build());
UpdateItemRequest request = UpdateItemRequest.builder().tableName("Work").key(itemKey).attributeUpdates(updatedValues).build();
try {
ddb.updateItem(request);
return "The item was successfully archived";
} catch (ResourceNotFoundException e) {
System.err.println(e.getMessage());
System.exit(1);
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
use of software.amazon.awssdk.services.dynamodb.DynamoDbClient in project aws-doc-sdk-examples by awsdocs.
the class DynamoDBService method getClient.
private DynamoDbClient getClient() {
// Create a DynamoDbClient object.
Region region = Region.US_EAST_1;
DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
return ddb;
}
use of software.amazon.awssdk.services.dynamodb.DynamoDbClient 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);
}
}
Aggregations