use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.
the class Scenario method deleteDynamoDBTable.
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 Scenario method getItem.
// snippet-start:[dynamodb.java2.scenario.get_item.main]
public static void getItem(DynamoDbClient ddb) {
HashMap<String, AttributeValue> keyToGet = new HashMap<String, AttributeValue>();
keyToGet.put("year", AttributeValue.builder().n("1933").build());
keyToGet.put("title", AttributeValue.builder().s("King Kong").build());
GetItemRequest request = GetItemRequest.builder().key(keyToGet).tableName("Movies").build();
try {
Map<String, AttributeValue> returnedItem = ddb.getItem(request).item();
if (returnedItem != null) {
Set<String> keys = returnedItem.keySet();
System.out.println("Amazon DynamoDB table attributes: \n");
for (String key1 : keys) {
System.out.format("%s: %s\n", key1, returnedItem.get(key1).toString());
}
} else {
System.out.format("No item found with the key %s!\n", "year");
}
} 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 DynamoDBScanItems method scanItems.
// snippet-start:[dynamodb.java2.dynamoDB_scan.main]
public static void scanItems(DynamoDbClient ddb, String tableName) {
try {
ScanRequest scanRequest = ScanRequest.builder().tableName(tableName).build();
ScanResponse response = ddb.scan(scanRequest);
for (Map<String, AttributeValue> item : response.items()) {
Set<String> keys = item.keySet();
for (String key : keys) {
System.out.println("The key name is " + key + "\n");
System.out.println("The value is " + item.get(key).s());
}
}
} catch (DynamoDbException e) {
e.printStackTrace();
System.exit(1);
}
}
use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.
the class EnhancedPutItem method putRecord.
// snippet-start:[dynamodb.java2.mapping.putitem.main]
public static void putRecord(DynamoDbEnhancedClient enhancedClient) {
try {
DynamoDbTable<Customer> custTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
// Create an Instant
LocalDate localDate = LocalDate.parse("2020-04-07");
LocalDateTime localDateTime = localDate.atStartOfDay();
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
// Populate the Table
Customer custRecord = new Customer();
custRecord.setCustName("Susan red");
custRecord.setId("id146");
custRecord.setEmail("sred@noserver.com");
custRecord.setRegistrationDate(instant);
// Put the customer data into a DynamoDB table
custTable.putItem(custRecord);
} 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 DeleteItem method deleteDymamoDBItem.
// snippet-start:[dynamodb.java2.delete_item.main]
public static void deleteDymamoDBItem(DynamoDbClient ddb, String tableName, String key, String keyVal) {
HashMap<String, AttributeValue> keyToGet = new HashMap<String, AttributeValue>();
keyToGet.put(key, AttributeValue.builder().s(keyVal).build());
DeleteItemRequest deleteReq = DeleteItemRequest.builder().tableName(tableName).key(keyToGet).build();
try {
ddb.deleteItem(deleteReq);
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
Aggregations