DynamoDB in Codeigniter


Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability.

DynamoDB is non-relational (no joins) so references (“foreign keys”) between documents are generally resolved client-side by additional queries to the server (“linking”).

To start you need an Amazon AWS account
http://aws.amazon.com/dynamodb/

After that you need to download the Amazone SDK for PHP and place it in /application/libraries/
http://aws.amazon.com/sdkforphp/
The AWS SDKs have been updated and now include complete support for DynamoDB.

Don’t forget to update the /application/config/aws.inc.php with your credentials so you save your data in your own database.

If you get issues connection to the database check your access permissions can reach the DynamoDB and that you have the required minimum specs on your machine.

Here are some examples that I put together using the AWS SDK for PHP.

My full working example is available at https://github.com/glynrob/DynamoDB-in-Codeigniter

This example allows you to add/edit/delete new members to your NoSQL database.

This also creates the example table for you if it does not already exist with a simple one click call.

I’m not going to describe how to create and manage Controllers/Models/Views as these are CodeIgniter basics.

In your controller you populate your constructor:

This calls basic helpers to manage urls and date formatting.
Also I have called my Model Users here as each function requires access to this data.

Then in the index function:

This calls my Model to retrieve the data, loops through all results and enters it into an array to pass to the view.

Now to create the Model.
These are some general functions that would be useful for many different requests that you require.

Inside the constructor I connect to the DynamoDB SDK and then set the credentials for access.
This now allows me to easily call the SDK for connection to the database.

As you can see in the function getAll() call we get all data inside the users table with the call $dynamodb->scan();
After that point you can then limit if you require.
There is also the option to call $dynamodb->query(); if you wish to make a more complicated request though you may need to use a Hash and Range table. See AWS documentation for more details on this.

Then on the View you only need to add

This will list out all the results in the $members array displaying the name of the member.

Have a look through the GitHub repo to see how to add/edit/delete members.
Its all very similar just with different syntax.

Hope this helps you get going on a NoSQL DynamoDB site.

Some useful resources are available at:
http://aws.amazon.com/articles/4261?_encoding=UTF8&jiveRedirect=1
and
http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/GettingStartedDynamoDB.html

Leave a Reply

Your email address will not be published. Required fields are marked *