Thursday, 30 March 2017

How to use Amazon DynamoDB with Ruby on Rails

In this blog we will discuss about Amazon DynamoDB and how to use it with Ruby on Rails application.
Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability.
NoSQL databases are designed for scale, but their architectures are sophisticated, and there can be significant operational overhead in running a large NoSQL cluster.
With DynamoDB, you can create database tables that can store and retrieve any amount of data, and serve any level of request traffic.
DynamoDB allows you to delete expired items from tables automatically to help you reduce storage usage and the cost of storing data that is no longer relevant.

Now, we will integrate it with Rails application. For this we can use aws-sdk gem.

So, in Gemfile
    gem 'aws-sdk', '~>2'


Run $ bundle

Now, we will configure Aws access. for this create config/initializers/aws.rb

In aws.rb
     Aws.config.update({
        region: 'us-west-2',
        credentials: Aws::Credentials.new(
'REPLACE_WITH_ACCESS_KEY_ID', 
'REPLACE_WITH_SECRET_ACCESS_KEY'),
      })



We can started by initializing a client object of DynamoDB by
    dynamodb = Aws::DynamoDB::Client.new


Now, we can create a table
    dynamodb.create_table({
        table_name: 'VenueOfferTable',
        attribute_definitions: [
          {attribute_name: 'Title',attribute_type:'S'},
          {attribute_name: 'LongDescription', attribute_type: 'S'}
        ],
        key_schema: [
          {attribute_name: 'Title', key_type: 'HASH'},
        ],
        provisioned_throughput: {
          read_capacity_units: 1,
          write_capacity_units: 1
        }
      })


Above code will create a table in DynamoDB.

Now we have a table so, we can add items by
    dynamodb.put_item({
      table_name: 'VenueOfferTable',
      item: {
        'Title' => 'Mill Valley Kitchen',
        'LongDescription' => 'Restaurant and Bar '
      }
    })


Update an item
    dynamodb.update_item({
        table_name: 'VenueOfferTable',
        key: {
          'Title' => 'Mill Valley Kitchen'
        },
        update_expression: 'SET LongDescription = :LongDescription',
        expression_attribute_values: {
          ':LongDescription' => 'Premium Restaurant and Bar'
        }
      })


Fetching the items from table
    response = dynamodb.get_item({
      table_name: 'VenueOfferTable',
      key: {
        'Title' => 'Mill Valley Kitchen'
      }
    })


We can scan full table. scan method can be used to perform a full table scan, which by default will return every item in a table

    response = dynamodb.scan(table_name: 'VenueOfferTable')

    items = response.items



There are many api's for querying to meet your requirement. You can get this from here.

4 comments:

  1. Nice post! Thanks for sharing valuable information with us. Keep sharingRuby on Rails Online Course Hyderabad

    ReplyDelete
  2. Super blog, very useful information provided by use for more updates on AWS. Get touch with AWS Online Course

    ReplyDelete
  3. really Good blog post.provided a helpful information.I hope that you will post more updates like this Ruby on Rails Online Training Bangalore

    ReplyDelete
  4. Really very good post. I am following this blog through one of my friend and its really awesome. Hope will find more articles

    Regards

    Best aws training in hyderabad

    ReplyDelete