{"id":313,"date":"2024-03-07T11:18:40","date_gmt":"2024-03-07T11:18:40","guid":{"rendered":"https:\/\/ramansaini.in\/blog\/?p=313"},"modified":"2024-03-12T09:17:56","modified_gmt":"2024-03-12T09:17:56","slug":"dynamodb-when-and-how-to-use","status":"publish","type":"post","link":"https:\/\/ramansaini.in\/blog\/dynamodb-when-and-how-to-use\/","title":{"rendered":"DynamoDB, When and how to use it."},"content":{"rendered":"\n<p>DynamoDB is a database service provided and managed by AWS. DynamoDB is a serverless database completely optimized and managed by AWS. It is really helpful when we need to work with big data or we just need to dump the data in the database. <\/p>\n\n\n\n<p>DynamoDB is a completely NoSQL database that provides independent tables instead of a complete database. Being NoSQL means, we do not need to define and maintain the structure of the data before or after the storage. But still, many limitations make us analyze the requirement before using DynamoDB as the primary database.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">DynamoDB basic actions<\/h3>\n\n\n\n<p>When working on DynamoDB we can only run queries on a single table rather than aggregating multiple tables. This is one of the limitations we need to keep in mind when working with DynamoDB. We can run 4 types of operations on DynamoDB, i.e. Create, Update, Delete, Fetch. <\/p>\n\n\n\n<p>First, we start by creating the table. This can be done either from the AWS console or using the <a href=\"https:\/\/aws.amazon.com\/cli\/\" target=\"_blank\" rel=\"noopener\">aws cli<\/a>. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Create a table<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>aws dynamodb create-table \\\n    --table-name Art \\\n    --attribute-definitions \\\n        AttributeName=Artist,AttributeType=S \\\n        AttributeName=ArtTitle,AttributeType=S \\\n    --key-schema \\\n        AttributeName=Artist,KeyType=HASH \\\n        AttributeName=ArtTitle,KeyType=RANGE \\\n    --provisioned-throughput \\\n        ReadCapacityUnits=10,WriteCapacityUnits=5<\/code><\/pre>\n\n\n\n<p>Above we are creating a new table named <code>Art<\/code>. It has two attributes, Artist as Hash key and Art Title as Range key.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Write item to a table<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>aws dynamodb put-item \\ --table-name Art \\ --item file:\/\/arts.json<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Read items from a table<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>aws dynamodb get-item \\ --table-name Art \\ --item file:\/\/arts.json<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete item from a table<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>aws dynamodb delete-item --table-name Art --key file:<em>\/\/key.json<\/em><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Query a table<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>aws dynamodb query --table-name Art \n--key-condition-expression \"Artist=:Artist and ArtTitle=:Songtitle\" <\/code><\/pre>\n\n\n\n<p>There are two ways in which we can fetch the data from DynamoDB.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Scan:<\/strong><br>A Scan operation returns one or more items or item attributes by accessing every item in a table or a secondary index.\u00a0Scan operations are generally slower and more expensive. Scan operations proceed sequentially.\u00a0Applications can request a parallel Scan operation for faster performance on a large table or secondary index.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Query:<\/strong><br>A Query operation finds items based on primary key values.\u00a0Query operations are designed to be query-optimized.\u00a0Query operations support a subset of comparison operators on key attribute values.\u00a0They only support an equal operator evaluation of the Primary Key.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>Also, learn about <a href=\"https:\/\/ramansaini.in\/blog\/serverless-flask-application-from-scratch\/\">Creating serverless flask application from scratch.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>DynamoDB is a database service provided and managed by AWS. DynamoDB is a serverless database completely optimized and managed by AWS. It is really helpful when we need to work with big data or we just need to dump the data in the database. DynamoDB is a completely NoSQL database that provides independent tables instead&hellip;&nbsp;<a href=\"https:\/\/ramansaini.in\/blog\/dynamodb-when-and-how-to-use\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">DynamoDB, When and how to use it.<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":315,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","_themeisle_gutenberg_block_has_review":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5,7],"tags":[21,13,11,17],"class_list":["post-313","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","category-cloud-devops","tag-aws","tag-database","tag-performance","tag-serverless"],"jetpack_featured_media_url":"https:\/\/ramansaini.in\/blog\/wp-content\/uploads\/2024\/03\/DynamoDB.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ramansaini.in\/blog\/wp-json\/wp\/v2\/posts\/313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ramansaini.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ramansaini.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ramansaini.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ramansaini.in\/blog\/wp-json\/wp\/v2\/comments?post=313"}],"version-history":[{"count":2,"href":"https:\/\/ramansaini.in\/blog\/wp-json\/wp\/v2\/posts\/313\/revisions"}],"predecessor-version":[{"id":316,"href":"https:\/\/ramansaini.in\/blog\/wp-json\/wp\/v2\/posts\/313\/revisions\/316"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ramansaini.in\/blog\/wp-json\/wp\/v2\/media\/315"}],"wp:attachment":[{"href":"https:\/\/ramansaini.in\/blog\/wp-json\/wp\/v2\/media?parent=313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ramansaini.in\/blog\/wp-json\/wp\/v2\/categories?post=313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ramansaini.in\/blog\/wp-json\/wp\/v2\/tags?post=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}