use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.
the class UpdateTable method updateDynamoDBTable.
// snippet-start:[dynamodb.java2.update_table.main]
public static void updateDynamoDBTable(DynamoDbClient ddb, String tableName, Long readCapacity, Long writeCapacity) {
System.out.format("Updating %s with new provisioned throughput values\n", tableName);
System.out.format("Read capacity : %d\n", readCapacity);
System.out.format("Write capacity : %d\n", writeCapacity);
ProvisionedThroughput tableThroughput = ProvisionedThroughput.builder().readCapacityUnits(readCapacity).writeCapacityUnits(writeCapacity).build();
UpdateTableRequest request = UpdateTableRequest.builder().provisionedThroughput(tableThroughput).tableName(tableName).build();
try {
ddb.updateTable(request);
} 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 ListTables method listAllTables.
// snippet-start:[dynamodb.java2.list_tables.main]
public static void listAllTables(DynamoDbClient ddb) {
boolean moreTables = true;
String lastName = null;
while (moreTables) {
try {
ListTablesResponse response = null;
if (lastName == null) {
ListTablesRequest request = ListTablesRequest.builder().build();
response = ddb.listTables(request);
} else {
ListTablesRequest request = ListTablesRequest.builder().exclusiveStartTableName(lastName).build();
response = ddb.listTables(request);
}
List<String> tableNames = response.tableNames();
if (tableNames.size() > 0) {
for (String curName : tableNames) {
System.out.format("* %s\n", curName);
}
} else {
System.out.println("No tables found!");
System.exit(0);
}
lastName = response.lastEvaluatedTableName();
if (lastName == null) {
moreTables = false;
}
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
System.out.println("\nDone!");
}
use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.
the class Scenario method putRecord.
public static void putRecord(DynamoDbClient ddb) {
try {
// Create a DynamoDbEnhancedClient.
DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
// Create a DynamoDbTable object.
DynamoDbTable<Movies> table = enhancedClient.table("Movies", TableSchema.fromBean(Movies.class));
// Populate the Table.
Movies record = new Movies();
record.setYear(2020);
record.setTitle("My Movie2");
record.setInfo("no info");
// Put the data into a DynamoDB table.
table.putItem(record);
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
System.out.println("Added a new issue to the table.");
}
use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.
the class Scenario method createTable.
// snippet-start:[dynamodb.java2.scenario.create_table.main]
// Create a table with a Sort key.
public static void createTable(DynamoDbClient ddb, String tableName) {
DynamoDbWaiter dbWaiter = ddb.waiter();
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
// Define attributes.
attributeDefinitions.add(AttributeDefinition.builder().attributeName("year").attributeType("N").build());
attributeDefinitions.add(AttributeDefinition.builder().attributeName("title").attributeType("S").build());
ArrayList<KeySchemaElement> tableKey = new ArrayList<KeySchemaElement>();
KeySchemaElement key = KeySchemaElement.builder().attributeName("year").keyType(KeyType.HASH).build();
KeySchemaElement key2 = KeySchemaElement.builder().attributeName("title").keyType(// Sort
KeyType.RANGE).build();
// Add KeySchemaElement objects to the list.
tableKey.add(key);
tableKey.add(key2);
CreateTableRequest request = CreateTableRequest.builder().keySchema(tableKey).provisionedThroughput(ProvisionedThroughput.builder().readCapacityUnits(new Long(10)).writeCapacityUnits(new Long(10)).build()).attributeDefinitions(attributeDefinitions).tableName(tableName).build();
try {
CreateTableResponse response = ddb.createTable(request);
DescribeTableRequest tableRequest = DescribeTableRequest.builder().tableName(tableName).build();
// Wait until the Amazon DynamoDB table is created.
WaiterResponse<DescribeTableResponse> waiterResponse = dbWaiter.waitUntilTableExists(tableRequest);
waiterResponse.matched().response().ifPresent(System.out::println);
String newTable = response.tableDescription().tableName();
System.out.println("The " + newTable + " was successfully created.");
} 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 queryTable.
// snippet-end:[dynamodb.java2.scenario.create_table.main]
// snippet-start:[dynamodb.java2.scenario.query.main]
// Query the table.
public static void queryTable(DynamoDbClient ddb) {
try {
DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
DynamoDbTable<Movies> custTable = enhancedClient.table("Movies", TableSchema.fromBean(Movies.class));
QueryConditional queryConditional = QueryConditional.keyEqualTo(Key.builder().partitionValue(2013).build());
// Get items in the table and write out the ID value
Iterator<Movies> results = custTable.query(queryConditional).items().iterator();
String result = "";
while (results.hasNext()) {
Movies rec = results.next();
System.out.println("The title of the movie is " + rec.getTitle());
System.out.println("The movie information is " + rec.getInfo());
}
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
Aggregations