Insert Items to DynamoDB Tables Using Java
Read on to learn how you can use good old Java to get items set up within DynamoDB tables.
Join the DZone community and get the full member experience.
Join For FreeIn a previous article, we learned how to create DynamoDB Tables using Java.
Next step is to insert items to the DynamoDB Tables previously created.
Keep in mind that for the insert action, the most basic step is to specify the primary key.
For the table users, the primary key is the attribute email. You can add as many attributes as you want, but the cumulative size should not surpass 400 KB.
Map<String,AttributeValue> attributeValues = new HashMap<>();
attributeValues.put("email",new AttributeValue().withS("jon@doe.com"));
attributeValues.put("fullname",new AttributeValue().withS("Jon Doe"));
PutItemRequest putItemRequest = new PutItemRequest()
.withTableName("Users")
.withItem(attributeValues);
PutItemResult putItemResult = amazonDynamoDB.putItem(putItemRequest);
DynamoDB also supports batch writes. In this case, the main benefit lies on less I/O, though nothing changes regarding consumed capacity. In our case, we will add a batch of login attempts.
Map<String,AttributeValue> firstAttributeValues = new HashMap<>();
firstAttributeValues.put("email",new AttributeValue().withS("jon@doe.com"));
Long date = new Date().getTime();
firstAttributeValues.put("timestamp",new AttributeValue().withN(Long.toString(date)));
PutRequest firstPutRequest = new PutRequest();
firstPutRequest.setItem(firstAttributeValues);
WriteRequest firstWriteRequest = new WriteRequest();
firstWriteRequest.setPutRequest(firstPutRequest);
Map<String,AttributeValue> secondAttributeValues = new HashMap<>();
secondAttributeValues.put("email",new AttributeValue().withS("jon@doe.com"));
secondAttributeValues.put("timestamp",new AttributeValue().withN(Long.toString(date+100)));
PutRequest secondPutRequest = new PutRequest();
secondPutRequest.setItem(secondAttributeValues);
WriteRequest secondWriteRequest = new WriteRequest();
secondWriteRequest.setPutRequest(secondPutRequest);
List<WriteRequest> batchList = new ArrayList<WriteRequest>();
batchList.add(firstWriteRequest);
batchList.add(secondWriteRequest);
Map<String, List<WriteRequest>> batchTableRequests = new HashMap<String, List<WriteRequest>>();
batchTableRequests.put("Logins",batchList);
BatchWriteItemRequest batchWriteItemRequest = new BatchWriteItemRequest();
batchWriteItemRequest.setRequestItems(batchTableRequests);
amazonDynamoDB.batchWriteItem(batchWriteItemRequest);
In the case of an insert with a global/local secondary index, all you have to do is to specify the corresponding attributes for the index. Take into consideration that you can have empty index related attributes or even duplicates.
Map<String,AttributeValue> attributeValues = new HashMap<>();
attributeValues.put("name",new AttributeValue().withS("Random SuperVisor"));
attributeValues.put("company",new AttributeValue().withS("Random Company"));
attributeValues.put("factory",new AttributeValue().withS("Jon Doe"));
PutItemRequest putItemRequest = new PutItemRequest()
.withTableName("Supervisors")
.withItem(attributeValues);
PutItemResult putItemResult = amazonDynamoDB.putItem(putItemRequest);
You can find the source code on GitHub.
Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments