VOOZH about

URL: https://www.javacodegeeks.com/2016/06/insert-items-dynamodb-tables-using-java.html

โ‡ฑ Insert Items to DynamoDB Tables using Java - Java Code Geeks


On 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 the primary key. For the table users the primary key is the attribute email. You can add as many attributes as you want however 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, however 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 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 sourcecode on github.

Reference: Insert Items to DynamoDB Tables using Java from our JCG partner Emmanouil Gkatziouras at the gkatzioura blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

๐Ÿ‘ Photo of Emmanouil Gkatziouras
Emmanouil Gkatziouras
June 29th, 2016Last Updated: June 28th, 2016
1 170 1 minute read

Emmanouil Gkatziouras

He is a versatile software engineer with experience in a wide variety of applications/services.He is enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
lihy70
9 years ago

Insert/query can be simplified by AbacusUtil: http://www.landawn.com/dynamodb-executor.html

Account account = createAccount2();
dbExecutor.putItem(โ€œaccountโ€, account);

Account dbAccount = dbExecutor.getItem(Account.class, โ€œaccountโ€, DynamoDBExecutor.asKey(โ€œidโ€, account.getId()));

final QueryRequest queryRequest = new QueryRequest(โ€œaccountโ€).withKeyConditions(Filters.eq(โ€œidโ€, โ€œabc123โ€));
List accounts = dbExecutor.query(Account.class, queryRequest);

0
Reply
Back to top button
Close
wpDiscuz