use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.
the class DynamoDBAsyncGetItem method getItem.
// snippet-start:[dynamoasyc.java2.get_item.main]
public static void getItem(DynamoDbAsyncClient client, String tableName, String key, String keyVal) {
HashMap<String, AttributeValue> keyToGet = new HashMap<String, AttributeValue>();
keyToGet.put(key, AttributeValue.builder().s(keyVal).build());
try {
// Create a GetItemRequest instance
GetItemRequest request = GetItemRequest.builder().key(keyToGet).tableName(tableName).build();
// Invoke the DynamoDbAsyncClient object's getItem
java.util.Collection<AttributeValue> returnedItem = client.getItem(request).join().item().values();
// Convert Set to Map
Map<String, AttributeValue> map = returnedItem.stream().collect(Collectors.toMap(AttributeValue::s, s -> s));
Set<String> keys = map.keySet();
for (String sinKey : keys) {
System.out.format("%s: %s\n", sinKey, map.get(sinKey).toString());
}
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
// snippet-end:[dynamoasyc.java2.get_item.main]
}
use of software.amazon.awssdk.services.dynamodb.model.DynamoDbException in project aws-doc-sdk-examples by awsdocs.
the class EnhancedQueryRecordsWithSortKey method queryTableSort.
// snippet-start:[dynamodb.java2.mapping.querykey.main]
public static void queryTableSort(DynamoDbEnhancedClient enhancedClient) {
try {
// Create a DynamoDbTable object based on Issues.
DynamoDbTable<Issues> table = enhancedClient.table("Issues", TableSchema.fromBean(Issues.class));
String dateVal = "2013-11-19";
DynamoDbIndex<Issues> secIndex = enhancedClient.table("Issues", TableSchema.fromBean(Issues.class)).index("dueDateIndex");
AttributeValue attVal = AttributeValue.builder().s(dateVal).build();
// Create a QueryConditional object that's used in the query operation.
QueryConditional queryConditional = QueryConditional.keyEqualTo(Key.builder().partitionValue(attVal).build());
// Get items in the Issues table.
SdkIterable<Page<Issues>> results = secIndex.query(QueryEnhancedRequest.builder().queryConditional(queryConditional).build());
AtomicInteger atomicInteger = new AtomicInteger();
atomicInteger.set(0);
results.forEach(page -> {
Issues issue = (Issues) page.items().get(atomicInteger.get());
System.out.println("The issue title is " + issue.getTitle());
atomicInteger.incrementAndGet();
});
} 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 CreateTableCompositeKey method createTableComKey.
// snippet-start:[dynamodb.java2.create_table_composite_key.main]
public static String createTableComKey(DynamoDbClient ddb, String tableName) {
CreateTableRequest request = CreateTableRequest.builder().attributeDefinitions(AttributeDefinition.builder().attributeName("Language").attributeType(ScalarAttributeType.S).build(), AttributeDefinition.builder().attributeName("Greeting").attributeType(ScalarAttributeType.S).build()).keySchema(KeySchemaElement.builder().attributeName("Language").keyType(KeyType.HASH).build(), KeySchemaElement.builder().attributeName("Greeting").keyType(KeyType.RANGE).build()).provisionedThroughput(ProvisionedThroughput.builder().readCapacityUnits(new Long(10)).writeCapacityUnits(new Long(10)).build()).tableName(tableName).build();
String tableId = "";
try {
CreateTableResponse result = ddb.createTable(request);
tableId = result.tableDescription().tableId();
return tableId;
} 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 DescribeTable method describeDymamoDBTable.
// snippet-start:[dynamodb.java2.describe_table.main]
public static void describeDymamoDBTable(DynamoDbClient ddb, String tableName) {
DescribeTableRequest request = DescribeTableRequest.builder().tableName(tableName).build();
try {
TableDescription tableInfo = ddb.describeTable(request).table();
if (tableInfo != null) {
System.out.format("Table name : %s\n", tableInfo.tableName());
System.out.format("Table ARN : %s\n", tableInfo.tableArn());
System.out.format("Status : %s\n", tableInfo.tableStatus());
System.out.format("Item count : %d\n", tableInfo.itemCount().longValue());
System.out.format("Size (bytes): %d\n", tableInfo.tableSizeBytes().longValue());
ProvisionedThroughputDescription throughputInfo = tableInfo.provisionedThroughput();
System.out.println("Throughput");
System.out.format(" Read Capacity : %d\n", throughputInfo.readCapacityUnits().longValue());
System.out.format(" Write Capacity: %d\n", throughputInfo.writeCapacityUnits().longValue());
List<AttributeDefinition> attributes = tableInfo.attributeDefinitions();
System.out.println("Attributes");
for (AttributeDefinition a : attributes) {
System.out.format(" %s (%s)\n", a.attributeName(), a.attributeType());
}
}
} 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 EnhancedBatchWriteItems method putBatchRecords.
// snippet-start:[dynamodb.java2.mapping.batchitems.main]
public static void putBatchRecords(DynamoDbEnhancedClient enhancedClient) {
try {
DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
LocalDate localDate = LocalDate.parse("2020-04-07");
LocalDateTime localDateTime = localDate.atStartOfDay();
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
Customer record2 = new Customer();
record2.setCustName("Fred Pink");
record2.setId("id110");
record2.setEmail("fredp@noserver.com");
record2.setRegistrationDate(instant);
Customer record3 = new Customer();
record3.setCustName("Susan Pink");
record3.setId("id120");
record3.setEmail("spink@noserver.com");
record3.setRegistrationDate(instant);
// Create a BatchWriteItemEnhancedRequest object
BatchWriteItemEnhancedRequest batchWriteItemEnhancedRequest = BatchWriteItemEnhancedRequest.builder().writeBatches(WriteBatch.builder(Customer.class).mappedTableResource(mappedTable).addPutItem(r -> r.item(record2)).addPutItem(r -> r.item(record3)).build()).build();
// Add these two items to the table
enhancedClient.batchWriteItem(batchWriteItemEnhancedRequest);
System.out.println("done");
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
Aggregations