use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.
the class EnhancedQueryRecords method queryTable.
// snippet-start:[dynamodb.java2.mapping.query.main]
public static String queryTable(DynamoDbEnhancedClient enhancedClient) {
try {
DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
QueryConditional queryConditional = QueryConditional.keyEqualTo(Key.builder().partitionValue("id120").build());
// Get items in the table and write out the ID value
Iterator<Customer> results = mappedTable.query(queryConditional).items().iterator();
String result = "";
while (results.hasNext()) {
Customer rec = results.next();
result = rec.getId();
System.out.println("The record id is " + result);
}
return result;
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.
the class EnhancedQueryRecordsWithFilter method queryTableFilter.
// snippet-start:[dynamodb.java2.mapping.queryfilter.main]
public static void queryTableFilter(DynamoDbEnhancedClient enhancedClient) {
try {
DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
AttributeValue att = AttributeValue.builder().s("sblue@noserver.com").build();
Map<String, AttributeValue> expressionValues = new HashMap<>();
expressionValues.put(":value", att);
Expression expression = Expression.builder().expression("email = :value").expressionValues(expressionValues).build();
// Create a QueryConditional object that is used in the query operation.
QueryConditional queryConditional = QueryConditional.keyEqualTo(Key.builder().partitionValue("id103").build());
// Get items in the Customer table and write out the ID value.
Iterator<Customer> results = mappedTable.query(r -> r.queryConditional(queryConditional).filterExpression(expression)).items().iterator();
while (results.hasNext()) {
Customer rec = results.next();
System.out.println("The record id is " + rec.getId());
}
} 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 CreateTable method createTable.
// snippet-start:[dynamodb.java2.create_table.main]
public static String createTable(DynamoDbClient ddb, String tableName, String key) {
DynamoDbWaiter dbWaiter = ddb.waiter();
CreateTableRequest request = CreateTableRequest.builder().attributeDefinitions(AttributeDefinition.builder().attributeName(key).attributeType(ScalarAttributeType.S).build()).keySchema(KeySchemaElement.builder().attributeName(key).keyType(KeyType.HASH).build()).provisionedThroughput(ProvisionedThroughput.builder().readCapacityUnits(new Long(10)).writeCapacityUnits(new Long(10)).build()).tableName(tableName).build();
String newTable = "";
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);
newTable = response.tableDescription().tableName();
return newTable;
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.
the class DeleteTable method deleteDynamoDBTable.
// snippet-start:[dynamodb.java2.delete_table.main]
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!");
}
use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.
the class EnhancedGetItem method getItem.
// snippet-start:[dynamodb.java2.mapping.getitem.main]
public static String getItem(DynamoDbEnhancedClient enhancedClient) {
try {
// Create a DynamoDbTable object
DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
// Create a KEY object
Key key = Key.builder().partitionValue("id146").build();
// Get the item by using the key
Customer result = mappedTable.getItem(r -> r.key(key));
return "The email value is " + result.getEmail();
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
Aggregations