Search Here

Sunday, December 7, 2014

Get Hands Dirty with NoSQL + Cloud Data Storage

NoSQL, Big Data, Cloud Data Storage, Scalability, Shards etc .. are buzzwords in the Data management industry. I thought to get my hands wet with these.

Let me quickly introduce them, (Ya very quick because you will find good intro in the Wikipedia)

  • NoSQL - Data management without traditional Table format, Without traditional SQL queries. 

  • Big Data - Huge Data Sets. Like weather details of the whole century.

  • Cloud Data Storage - Get the data storage from online cloud vendors instead of storing the data in your own servers / machines.

  • Scalability - When the Data grows, Easily upgrade the computing capacity so data base operations like read , search and writes are remain faster.

  • Shards - To achieve Scalability, Instead of increasing computing power (RAM, HDD Space etc..) on the single server, Split the Data base into smaller chunks and place them in a smaller servers / machines. These chunks are calles Shards and collection of Shards forms a logical single database.
Data storage and retrial needs changes over the time. Now, we are in the world where the application users are very impatience, Can not wait for 30 sec to get the data.

The motivation behind the NoSQL is for high performance, High availability, Simple to store / search / get data and easy administration.

For example, If you are developing a mobile game and you want to store few details like userid, gamescore, devicename in the database. The data itself is very simple, creating SQL schema, creating table, querying data in SQL way is too over kill for this usecase. What you need is a online connected data storage, easy to store and super raw speed.

No offence to SQL guys, SQL is too big for us.

Hey, Good usecase right, Let me use this usecase and create online, high available, cloud provided, NoSQL database (hubbbaa...)..

When we are talking about the NoSQL, there are multiple thought process and Databases types are available in the industry. Each type stores the data in different format but the goals of the the NoSQL DBs are same. (You might need to read NoSQL in Wikipedia, if you haven't done yet).

OK. Stop talking, This post suppose to be a hands on ...

The reason why I was talking so much because I am afraid to read the manual, download, install the Db engine, configure and the use ... I am too lazy.

This is a cloud era, So lets create a online, high available, cloud provided, NoSQL database without installing anything in your machine. (I can see smile and continue to read face :) )

I chose MongoDB NoSQL database.

  • MongoDB - Open Source NoSQL Database. Uses JSON like documents for storage. Gives super speeeeeeeeeeeeeeeed.
Multiple Cloud vendors are providing MongoDB as a hosted service. Most of them provide 512 MB , Non production grade MongoDB space for free to tryout. Awesome!! Lets start,

  1. Go to https://mongolab.com/signup/ and Sign up. Mongolab offers consolidated dashboard and tools for various data base hosting service provides (Like google cloud, Microsoft Azure, Amazon cloud etc..) 


 2. Fill the data and sign up. Once you Create account, You will get a mail in your provided mail address from Mongolab for Email verification. Check your mail Box and complete the Email verification process.

3. Go to https://mongolab.com/home  - This is your Database Dashboard

4. Click "Create New" button

5. Choose your favourite cloud computing provider, I Chose Amazon.

Note: Do not change the "Location" Its only for production, Paid databases. 

6. Under "Plan" choose Single-node, Then Choose "Sandbox" for free 512MB Mongo DB. 

7. Provide Database name (Mandatory for free databases)


8. Click "Create new MongoDB deployment"

9. Once its created, You can see the database in your home / dashboard view.

Note: You can create multiple MongoDBs from Multiple Cloud providers. All of them can be managed with Dashboard page. Each database comes with 512MB data limit. But its from non production area not suitable for real bussines.


10. Good. Now you have your own database.

11. Lets add some data. In NoSQL database, as I have mentioned above there is no scheme and no need to create a table / data storage structure. In MongoDB terms,

  • Collection - Like SQL Table. Collection of documents.
  • Document - Like SQL table row data. A data record.
12.  Click your newly created MongoDB database in the dashboard. My database name is npointsolutionsmdb.

13. mongolab will take you to the Database dashboard page. Click "Add collection" button and provide collection (SQL Table) name. I gave "userDetailsCollection" as a collection name to demonstrate my above mobile game usecase.

14. Now, collection might have created. In the Dashboard, Click the just created collection name so we can add the data into collection.

15. Once you click the collection, mongolab will take you to the collection administration page, Click "Add document" button

16. You will see a Create Document page. In MongoDB, Data is represented as a JSON style documents. We can add any vaild JSON document in the Create Document page. I have added following JSON data,


{
        "userName": "test1",
        "OS": "Linux Fedora 20",
        "DeviceArch": "x86_64",
        "LastLogin": "Dec 07, 2014",
        "Score": 103
}


17. If you like my data format, Just copy paste the above data into "Create document" area and Click "Create and go back" button.

18. You can add any number of documents (one by one, by following step #15, #17) into your database. But maximum size limit for whole database (Non productional, No backup) is 512MB.

19. You can check the Database size in the Top Dashboard https://mongolab.com/home

20. If you are adding your own Data, Check its JSON syntax validity in http://jsonlint.com/ before you paste in "Create document".

21.  You can see all the documents from the collections page


22. From the collections page, You can edit / delete each document.

23. Try edit one of the document, Using "Edit" button right most side of each document


Note: You can see "_id" field is added. This is unique object ID added for each document by the MongoDB. "_id" acts like a Primary Key for the document.

24. Click "Cancel and go back" button.

25. Lets try to create index for our collection, Index will make the query run faster.

26. Click on the "Indexes" tab parallel to "Documents" tab

27. In the Add new index, Add following code (almost all the data representaion in the MongoDB is a JSON format)


{
        "userName": 1,
        "Score": 1
        
}



28. Click "Create in background" button. MongoDB also supports creating index as a foreground operation but it will block all the database operations. Usually forground index is very fast but it will take long time if you have large data set. So always create index in background.

29. Go to your collections by clicking db: "<your db name>" link.

30. Click User tab parallel to Collections tab


31. Lets add Database users so they can access our data. Click "Add database user" button

32. Provide user details, I have created user name "user1" with password "user123"


33. READ ONLY? shows that the user can only read the documents or even update / insert documents. false indicates the user can update and insert the documents into collections.

34. Goto home / dashboard , https://mongolab.com/home



35. You can see your MongoDB database size is grown up because we have added few documents into the collections, created users and indexes.


Congrats !!! , Now you have your own online, high available, cloud provided, NoSQL database. 

Keep your  MongoDB read, I will show you how to access the Online, cloud provided, MongoDB via application program code (mostly Perl). Stay tune ...

Refer:



3. And google is your best friend.

No comments:

Post a Comment