Support of DynamoDB for Java
CAST supports DynamoDB via its com.castsoftware.nosqljava extension. Details about how this support is provided for Java source code is discussed below.
Supported Client Libraries
Library | Supported |
---|---|
AWS SDK for Java API - v1 | |
AWS SDK for Java API - v2 |
Supported Operations
Operations | Scenario | Methods Supported |
---|---|---|
Insert | AWS SDK JAVA v1 | com.amazonaws.services.dynamodbv2.AmazonDynamoDB.putItem com.amazonaws.services.dynamodbv2.document.Table.putItem com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync.putItemAsync com.amazonaws.services.dynamodbv2.document.DynamoDB.createTable com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync.createTableAsync com.amazonaws.services.dynamodbv2.AmazonDynamoDB.createTable com.amazonaws.services.dynamodbv2.util.TableUtils.createTableIfNotExists com.amazonaws.services.dynamodbv2.AmazonDynamoDB.batchWriteItem com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync.batchWriteItemAsync com.amazonaws.services.dynamodbv2.AmazonDynamoDB.transactWriteItems com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync.transactWriteItemsAsync |
Insert | DDB Mapper v1 | com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.save com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.save com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.batchSave com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.batchSave com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.batchWrite com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.batchWrite com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.transactionWrite com.amazonaws.services.dynamodbv2.datamodeling.TransactionWriteRequest.addPut |
Insert | AWS SDK JAVA v2 | software.amazon.awssdk.services.dynamodb.DynamoDbClient.putItem software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient.putItem software.amazon.awssdk.services.dynamodb.DynamoDbClient.createTable software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient.createTable software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter.createTable |
Insert | Enhanced v2 | software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable.putItem software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable.putItemWithResponse software.amazon.awssdk.enhanced.dynamodb.DynamoDbAsyncTable.putItem software.amazon.awssdk.enhanced.dynamodb.DynamoDbAsyncTable.putItemWithResponse software.amazon.awssdk.enhanced.dynamodb.model.TransactWriteItemsEnhancedRequest.Builder.addPutItem software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest.Builder.writeBatches |
Update | AWS SDK JAVA v1 | com.amazonaws.services.dynamodbv2.document.Table.updateTable com.amazonaws.services.dynamodbv2.document.Table.upateItem com.amazonaws.services.dynamodbv2.AmazonDynamoDB.updateItem com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync.updateItemAsync com.amazonaws.services.dynamodbv2.AmazonDynamoDB.updateTable com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync.updateTableAsync |
Update | DDB Mapper v1 | com.amazonaws.services.dynamodbv2.datamodeling.TransactionWriteRequest.addUpdate |
Update | AWS SDK JAVA v2 | software.amazon.awssdk.services.dynamodb.DynamoDbClient.updateTable software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient.updateTable software.amazon.awssdk.services.dynamodb.DynamoDbClient.updateItem software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient.updateItem |
Update | Enhanced v2 | software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable.updateItem software.amazon.awssdk.enhanced.dynamodb.DynamoDbAsyncTable.updateItem software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable.updateItemWithResponse software.amazon.awssdk.enhanced.dynamodb.DynamoDbAsyncTable.updateItemWithResponse software.amazon.awssdk.enhanced.dynamodb.model.TransactWriteItemsEnhancedRequest.Builder.addUpdateItem |
Select | AWS SDK JAVA v1 | com.amazonaws.services.dynamodbv2.document.Table.query com.amazonaws.services.dynamodbv2.AmazonDynamoDB.query com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync.queryAsync com.amazonaws.services.dynamodbv2.document.Table.scan com.amazonaws.services.dynamodbv2.AmazonDynamoDB.scan com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync.scanAsync com.amazonaws.services.dynamodbv2.document.Table.describe com.amazonaws.services.dynamodbv2.document.Table.getTableName com.amazonaws.services.dynamodbv2.document.Table.getItemOutcome com.amazonaws.services.dynamodbv2.document.Table.getItem com.amazonaws.services.dynamodbv2.AmazonDynamoDB.getItem com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync.getItemAsync com.amazonaws.services.dynamodbv2.document.DynamoDB.getTable com.amazonaws.services.dynamodbv2.AmazonDynamoDB.describeTable com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync.describeTableAsync com.amazonaws.services.dynamodbv2.AmazonDynamoDB.batchGetItem com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync.batchGetItemAsync com.amazonaws.services.dynamodbv2.AmazonDynamoDB.transactGetItems com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync.transactGetItemsAsync |
Select | DDB Mapper v1 | com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.load com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.load com.amazonaws.services.dynamodbv2.datamodeling.TransactionLoadRequest.addLoad com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.scan com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.scan com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.query com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.query com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.count com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.count com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.scanPage com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.ScanPage com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.queryPage com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.queryPage com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.parallelScan com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.parallelScan com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.getTableModel com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.getTableModel |
Select | AWS SDK JAVA v2 | software.amazon.awssdk.services.dynamodb.DynamoDbClient.describeTable software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient.describeTable software.amazon.awssdk.services.dynamodb.DynamoDbClient.getItem software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient.getItem software.amazon.awssdk.services.dynamodb.DynamoDbClient.query software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient.query software.amazon.awssdk.services.dynamodb.DynamoDbClient.scan software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient.scan |
Select | Enhanced v2 | software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient.table software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedAsyncClient.table software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable.getItem software.amazon.awssdk.enhanced.dynamodb.DynamoDbAsyncTable.getItem software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable.query software.amazon.awssdk.enhanced.dynamodb.DynamoDbAsyncTable.query software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable.scan software.amazon.awssdk.enhanced.dynamodb.DynamoDbAsyncTable.scan software.amazon.awssdk.enhanced.dynamodb.model.BatchGetItemEnhancedRequest.Builder.readBatches software.amazon.awssdk.enhanced.dynamodb.model.TransactGetItemsEnhancedRequest.Builder.addGetItem |
Delete | AWS SDK JAVA v1 | com.amazonaws.services.dynamodbv2.document.Table.deleteItem com.amazonaws.services.dynamodbv2.AmazonDynamoDB.deleteItem com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync.deletItemAsync com.amazonaws.services.dynamodbv2.document.Table.delete com.amazonaws.services.dynamodbv2.AmazonDynamoDB.deleteTable com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync.deleteTableAsync com.amazonaws.services.dynamodbv2.util.TableUtils.deleteTableIfExists |
Delete | DDB Mapper v1 | com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.delete com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.delete com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.batchDelete com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.batchDelete com.amazonaws.services.dynamodbv2.datamodeling.TransactionWriteRequest.addDelete |
Delete | AWS SDK JAVA v2 | software.amazon.awssdk.services.dynamodb.DynamoDbClient.deleteTable software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient.deleteTable software.amazon.awssdk.services.dynamodb.DynamoDbClient.deleteItem software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient.deleteItem |
Delete | Enhanced v2 | software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable.deleteItem software.amazon.awssdk.enhanced.dynamodb.DynamoDbAsyncTable.deleteItem software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable.deleteItemWithResponse software.amazon.awssdk.enhanced.dynamodb.DynamoDbAsyncTable.deleteItemWithResponse software.amazon.awssdk.enhanced.dynamodb.model.TransactWriteItemsEnhancedRequest.Builder.addDeleteItem |
Note: batchWriteItems, transactWriteItems may result in useInsertLink, useUpdateLink or useDeleteLink depnding on the usage.
Objects
Icon | Description |
---|---|
Java AWS DynamoDB Client | |
Java AWS DynamoDB Table | |
Java AWS Unknown DynamoDB Client | |
Java AWS Unknown DynamoDB Table |
Links
Link type | Source and destination of link | Methods Supported in AWS SDK Java v1 | Methods Supported in AWS SDK Java v1 DynamoDB Mapper | Methods Supported in AWS SDK Java v2 | Methods Supported in v2 Enhanced | Remarks |
---|---|---|---|---|---|---|
belongsTo | From Java AWS DynamoDB Table object to Java AWS DynamoDB Client object |
- | - | - | - | - |
useLink | Between the caller Java Method objects and Java AWS DynamoDB table object | createTable createTableAsync |
- | createTable |
- | - |
useInsertLink | Between the caller Java Method objects and Java AWS DynamoDB table object | putItem putItemAsync |
save batchSave |
putItem |
putItem putItemWithResponse |
batchWriteItems, transactWriteItems may result in useInsertLink, useUpdateLink or useDeleteLink depnding on the usage. |
useUpdateLink | Between the caller Java Method objects and Java AWS DynamoDB table object | updateTable updateItem updateTableAsync updateItemAsync |
- | updateTable updateItem |
updateItem updateItemWithReponse |
As above |
useSelectLink |
Between the caller Java Method objects and Java AWS DynamoDB table object | query queryAsync describeTable describeTableAsync getItem getItemAsync batchGetItem batchGetItemAsync transactGetItems transactGetItemsAsync |
load query count scan scanPage queryPage parallelScan getTableModel batchLoad transasctionLoad |
describeTable getItem query scan |
getItem query scan readBatches batchGetItem transactGetItems |
As above |
useDeleteLink | Between the caller Java Method objects and Java AWS DynamoDB table object | DeleteTable DeleteItem |
delete batchDelete |
deleteTable deleteItem |
deleteItem deleteItemWithResponse |
As above |
What results can you expect?
Some example scenarios are shown below:
AWS SDK JAVA v1
DynamoDB Client Configuration
public static void main(String[] args)
{
final String USAGE = "\n" +
"Usage:\n" +
" CreateTable <table>\n\n" +
"Where:\n" +
" table - the table to create.\n\n" +
"Example:\n" +
" CreateTable HelloTable\n";
if (args.length < 1) {
System.out.println(USAGE);
System.exit(1);
}
/* Read the name from command args */
String table_name = "Customer Table";
System.out.format(
"Creating table \"%s\" with a simple primary key: \"Name\".\n",
table_name);
CreateTableRequest request = new CreateTableRequest()
.withAttributeDefinitions(new AttributeDefinition(
"Name", ScalarAttributeType.S))
.withKeySchema(new KeySchemaElement("Name", KeyType.HASH))
.withProvisionedThroughput(new ProvisionedThroughput(
new Long(10), new Long(10)))
.withTableName(table_name);
final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
try {
CreateTableResult result = ddb.createTable(request);
System.out.println(result.getTableDescription().getTableName());
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
System.out.println("Done!");
}
Insert Operation
public static void main(String args[]) throws InterruptedException {
AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder
.standard()
.withRegion(Regions.US_EAST_2)
.withCredentials(new DefaultAWSCredentialsProviderChain())
.build();
int maxItemCount = 100;
for (Integer i = 1; i <= maxItemCount; i++) {
System.out.println("Processing item " + i + " of " + maxItemCount);
// Write a new item
Map<String, AttributeValue> item = new HashMap<>();
item.put("Id", new AttributeValue().withN(i.toString()));
item.put("Message", new AttributeValue().withS("New item!"));
dynamoDBClient.putItem(tableName, item);
}
}
Delete Operation
public static void TestDeleteTable() {
String table_name = "Products2";
DeleteTableRequest request = new DeleteTableRequest().withTableName(table_name);
final AmazonDynamoDB ddb2 = AmazonDynamoDBClientBuilder.defaultClient();
try {
ddb2.deleteTable(request);
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
System.out.println("Done!");
}
Update Operation
public static void main(String args[]) throws InterruptedException {
AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder
.standard()
.withRegion(Regions.US_EAST_2)
.withCredentials(new DefaultAWSCredentialsProviderChain())
.build();
Map<String, AttributeValue> key = new HashMap<>();
key.put("Id", new AttributeValue().withN(i.toString()));
Map<String, AttributeValueUpdate> attributeUpdates = new HashMap<>();
attributeUpdates.put("Message", new AttributeValueUpdate()
.withAction(AttributeAction.PUT)
.withValue(new AttributeValue()
.withS("This item has changed")));
dynamoDBClient.updateItem(tableName, key, attributeUpdates);
Select Operation
public static void main(String args[]) throws InterruptedException {
AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder
.standard()
.withRegion(Regions.US_EAST_2)
.withCredentials(new DefaultAWSCredentialsProviderChain())
.build();
String tableName = "TestTableForStreams";
// Print the stream settings for the table
DescribeTableResult describeTableResult = dynamoDBClient.describeTable(tableName);
String streamArn = describeTableResult.getTable().getLatestStreamArn();
System.out.println("Current stream ARN for " + tableName + ": " +
describeTableResult.getTable().getLatestStreamArn());
Batch Operations
BatchWriteRequest
Map<String, AttributeValue> book_item1 = new HashMap<String, AttributeValue>();
WriteRequest req1= new WriteRequest().withDeleteRequest(new DeleteRequest(book_item1));
WriteRequest req2= new WriteRequest().withPutRequest(new PutRequest(book_item2));
List<WriteRequest> requests1=new ArrayList<WriteRequest>();
requests1.add(req1);
requests1.add(req2);
List<WriteRequest> requests2=new ArrayList<WriteRequest>();
requests2.add(new WriteRequest().withDeleteRequest(new DeleteRequest(book_item3)));
Map<String, List<WriteRequest>> requestItems = new HashMap<String, List<WriteRequest>>() ;
requestItems.put("table1",requests1);
requestItems.put("table2",requests2);
BatchWriteItemRequest batchWriteItemRequest1=new BatchWriteItemRequest().withRequestItems(requestItems);
ddb1.batchWriteItem(batchWriteItemRequest1) ;
BatchGetItemRequest
KeysAndAttributes item1 = null;
KeysAndAttributes item2 = null;
Map<String, KeysAndAttributes> requestItems = new HashMap<String, KeysAndAttributes>();
requestItems.put("table1",item1);
requestItems.put("table2",item2);
BatchGetItemRequest batchGetItemRequest1 = new BatchGetItemRequest().withRequestItems(requestItems);
ddb2.batchGetItem(batchGetItemRequest1) ;
TransactWriteItemsRequest
Put putItem1= new Put().withTableName("table1").withItem(book_item1);
Delete deleteItem1= new Delete().withTableName("table1").withKey(book_item2);
Update updateItem1=new Update().withTableName("table2").withKey(book_item3);
TransactWriteItem transactItem1=new TransactWriteItem().withPut(putItem1);
TransactWriteItem transactItem2=new TransactWriteItem().withDelete(deleteItem1);
TransactWriteItem transactItem3=new TransactWriteItem().withUpdate(updateItem1);
TransactWriteItemsRequest transactWriteItemsRequest= new TransactWriteItemsRequest().withTransactItems(transactItem1,transactItem2,transactItem3);
ddb3.transactWriteItems(transactWriteItemsRequest);
TransactGetItemsRequest
Get getItem1= new Get().withTableName("table1").withKey(book_item1);
Get getItem2=new Get().withTableName("table2").withKey(book_item2);
TransactGetItem transactItem1=new TransactGetItem().withGet(getItem1);
TransactGetItem transactItem2=new TransactGetItem().withGet(getItem2);
TransactGetItemsRequest transactGetItemsRequest= new TransactGetItemsRequest().withTransactItems(transactItem1,transactItem2);
ddb4.transactGetItems(transactGetItemsRequest);
AWS SDK JAVA v1 DynamoDB Mapper
Insert Operation
public void likeMysfit(String mysfitId) {
Mysfit mysfitToUpdate = mapper.load(Mysfit.class, mysfitId);
Integer likes = mysfitToUpdate.getLikes() + 1;
mysfitToUpdate.setLikes(likes);
mapper.save(mysfitToUpdate);
}
Select Operation
public Mysfits queryMysfits(String filter, String value) {
HashMap<String, AttributeValue> attribValue = new HashMap<String, AttributeValue>();
attribValue.put(":"+value, new AttributeValue().withS(value));
DynamoDBQueryExpression<Mysfit> queryExpression = new DynamoDBQueryExpression<Mysfit>()
.withIndexName(filter+"Index")
.withKeyConditionExpression(filter + "= :" + value)
.withExpressionAttributeValues(attribValue)
.withConsistentRead(false);
List<Mysfit> mysfits = mapper.query(Mysfit.class, queryExpression);
Mysfits allMysfits = new Mysfits(mysfits);
return allMysfits;
}
AWS SDK JAVA v2
Insert Operation
public static void putItemInTable(DynamoDbClient ddb,
String tableName,
String key,
String keyVal,
String albumTitle,
String albumTitleValue,
String awards,
String awardVal,
String songTitle,
String songTitleVal){
HashMap<String,AttributeValue> itemValues = new HashMap<String,AttributeValue>();
// Add all content to the table
itemValues.put(key, AttributeValue.builder().s(keyVal).build());
itemValues.put(songTitle, AttributeValue.builder().s(songTitleVal).build());
itemValues.put(albumTitle, AttributeValue.builder().s(albumTitleValue).build());
itemValues.put(awards, AttributeValue.builder().s(awardVal).build());
PutItemRequest request = PutItemRequest.builder()
.tableName(tableName)
.item(itemValues)
.build();
try {
ddb.putItem(request);
System.out.println(tableName +" was successfully updated");
} catch (ResourceNotFoundException e) {
System.err.format("Error: The Amazon DynamoDB table \"%s\" can't be found.\n", tableName);
System.err.println("Be sure that it exists and that you've typed its name correctly!");
System.exit(1);
}
Delete Operation
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!");
}
Update Operation
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!");
}
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!");
}
Select Operation
public static int queryTable(DynamoDbClient ddb,
String tableName,
String partitionKeyName,
String partitionKeyVal,
String partitionAlias) {
HashMap<String,String> attrNameAlias = new HashMap<String,String>();
attrNameAlias.put(partitionAlias, partitionKeyName);
// Set up mapping of the partition name with the value
HashMap<String, AttributeValue> attrValues =
new HashMap<String,AttributeValue>();
attrValues.put(":"+partitionKeyName, AttributeValue.builder()
.s(partitionKeyVal)
.build());
QueryRequest queryReq = QueryRequest.builder()
.tableName(tableName)
.keyConditionExpression(partitionAlias + " = :" + partitionKeyName)
.expressionAttributeNames(attrNameAlias)
.expressionAttributeValues(attrValues)
.build();
try {
QueryResponse response = ddb.query(queryReq);
return response.count();
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return -1;
}
AWS JAVA SDK v2 - Enhanced
Insert Operation
public static void putRecord(DynamoDbEnhancedClient enhancedClient) {
try {
DynamoDbTable<Customer> custTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
// Create an Instant
LocalDate localDate = LocalDate.parse("2021-08-16");
LocalDateTime localDateTime = localDate.atStartOfDay();
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
// Populate the Table
Customer custRecord = new Customer();
custRecord.setCustName("Susan red");
custRecord.setId("id66");
custRecord.setEmail("abc@xyz.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");
}
Update Operation
public static String modifyItem(DynamoDbEnhancedClient enhancedClient, String keyVal, String email) {
try {
//Create a DynamoDbTable object
DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
//Create a KEY object
Key key = Key.builder()
.partitionValue(keyVal)
.build();
// Get the item by using the key and update the email value.
Customer customerRec = mappedTable.getItem(r->r.key(key));
customerRec.setEmail(email);
mappedTable.updateItem(customerRec);
return customerRec.getEmail();
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
Select Operation
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("id120")
.build();
// Get the item by using the key
Customer result = mappedTable.getItem(r->r.key(key));
return "The email valie is "+result.getEmail();
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
Region_property
public static void main(String[] args) {
final String usage = "\n" +
"Usage:\n" +
" <tableName>\n\n" +
"Where:\n" +
" tableName - The Amazon DynamoDB table to get information about (for example, Music3).\n\n" ;
if (args.length != 1) {
System.out.println(usage);
System.exit(1);
}
String tableName = "Test";
System.out.format("Getting description for %s\n\n", tableName);
Regions region = Regions.US_WEST_2;
AmazonDynamoDB ddb1 = AmazonDynamoDBClientBuilder.standard()
.withRegion(region)
.build();
CreateTableRequest request = new CreateTableRequest()
.withAttributeDefinitions(new AttributeDefinition("Name", ScalarAttributeType.S))
.withKeySchema(new KeySchemaElement("Name", KeyType.HASH))
.withProvisionedThroughput(new ProvisionedThroughput(new Long(10), new Long(10))).withTableName(Table1);
TestDescribeTable(ddb1,tableName);
ddb1.close();
System.out.println("Done!");
}
public static void TestDescribeTable(AmazonDynamoDB ddb1,String tableName) {
System.out.format("Getting description for %s\n\n", table_name);
try {
DescribeTableResult table_info1 = ddb1.describeTable(tableName);
// ddb4.describeTable(table_name).getTable();
TableDescription table_info = table_info1.getTable();
if (table_info != null) {
System.out.format("Table name : %s\n", table_info.getTableName());
System.out.format("Table ARN : %s\n", table_info.getTableArn());
System.out.format("Status : %s\n", table_info.getTableStatus());
System.out.format("Item count : %d\n", table_info.getItemCount().longValue());
System.out.format("Size (bytes): %d\n", table_info.getTableSizeBytes().longValue());
ProvisionedThroughputDescription throughput_info = table_info.getProvisionedThroughput();
System.out.println("Throughput");
System.out.format(" Read Capacity : %d\n", throughput_info.getReadCapacityUnits().longValue());
System.out.format(" Write Capacity: %d\n", throughput_info.getWriteCapacityUnits().longValue());
List<AttributeDefinition> attributes = table_info.getAttributeDefinitions();
System.out.println("Attributes");
for (AttributeDefinition a : attributes) {
System.out.format(" %s (%s)\n", a.getAttributeName(), a.getAttributeType());
}
}
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
System.out.println("\nDone!");
}
}
Known Limitations
- If the table name is not resolved in the CRUD API, then the link is created with an unknown Table object