{"id":16,"date":"2020-07-24T09:52:45","date_gmt":"2020-07-24T09:52:45","guid":{"rendered":"https:\/\/system.camp\/index.php\/2020\/07\/24\/batch-load-objects-using-dynamodbmapper\/"},"modified":"2020-10-06T07:21:00","modified_gmt":"2020-10-06T07:21:00","slug":"batch-load-objects-using-dynamodbmapper","status":"publish","type":"post","link":"https:\/\/system.camp\/tutorial\/batch-load-objects-using-dynamodbmapper\/","title":{"rendered":"Batch load objects using AWS dynamoDBMapper in JAVA"},"content":{"rendered":"\n

DynamoDbMapper is an amazing class to abstract out a lot of operations for AWS DynamoDb. But this library is not well documented. Here is an example of how to batchLoad objects using dynamoDBMapper in JAVA<\/p>\n\n\n\n

List<ArticlesDao> getArticleById(List<LikeDao> likeDaos) {  List<ArticlesDao> articlesDaoList = new ArrayList<>();\n  \/\/The primary key for ArticleDao is \"articleId\"\n  for (LikeDao likeDao : likeDaos) {\n    articlesDaoList.add(ArticlesDao.builder().articleId(likeDao.getArticleId()).build());\n  }\n  \/\/Batch Load this list of objects\n  Map<String, List<Object>> batchResult = dynamoDBMapper.batchLoad(articlesDaoList);\n  if (batchResult.containsKey(ArticlesDao.TABLE_NAME)\n      && batchResult.get(ArticlesDao.TABLE_NAME) != null) {\n    List<Object> articles = batchResult.get(ArticlesDao.TABLE_NAME);\n    \/\/Convert the list of Objects into \"ArticlesDao\n    @SuppressWarnings(\"unchecked\")\n    List<ArticlesDao> articlesDaos = (List<ArticlesDao>) (List<?>) articles;\n    return articlesDaos;\n  }\n  return null;\n}<\/code><\/pre>\n\n\n\n

In the above code we are creating a ArrayList, you can also create a HashMap with the object “key” as the hash key for the HashMap.<\/p>\n\n\n\n

Map<String, ArticlesDao> getArticlesById(List<LikeDao> likeDaos) {\n  Map<String, ArticlesDao> articlesDaoMap = new HashMap<>();\n  List<ArticlesDao> articlesDaoList = new ArrayList<>();\n  \/\/The primary key for ArticleDao is \"articleId\"\n  for (LikeDao likeDao : likeDaos) {\n    articlesDaoList.add(ArticlesDao.builder().articleId(likeDao.getArticleId()).build());\n  }\n  \/\/Batch Load this list of objects\n  Map<String, List<Object>> batchResult = dynamoDBMapper.batchLoad(articlesDaoList);\n  if (batchResult.containsKey(ArticlesDao.TABLE_NAME)\n      && batchResult.get(ArticlesDao.TABLE_NAME) != null) {\n    List<Object> articles = batchResult.get(ArticlesDao.TABLE_NAME);\n    for (Object object : articles) {\n      ArticlesDao articlesDao = (ArticlesDao) object;\n      articlesDaoMap.put(articlesDao.getArticleId(), articlesDao);\n    }\n    return articlesDaoMap;\n  }\n  return null;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

DynamoDbMapper is an amazing class to abstract out a lot of operations for AWS DynamoDb. But this library is not well documented. Here is an example of how to batchLoad objects using dynamoDBMapper in JAVA<\/p>\n","protected":false},"author":1,"featured_media":105,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[41,35],"tags":[14,5,15,3],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/16"}],"collection":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":3,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":108,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/16\/revisions\/108"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/media\/105"}],"wp:attachment":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}