Archive for category RavenDb

From “Yes”SQL to NoSQL with Raven Db

What is “Yes”Sql?

Question :- Do you know what we can use to store all employees data?
Answer :- Yes! I would use a RDBMS (Relational Database Management System) software like Sql Server,
MySql, Oracle etc. I would store all your employee data in different database tables with relationships between them.

Question :- Do you know how can we can access and manage employees data?
Answer :- Yes! I would use a query language named SQL (Structured Query Language) to query and manage data.

If you have been also answering “Yes” to the above questions, you have been doing “Yes”SQL. I have been doing the “Yes”SQL for quite some time and feeling like the frog in its well of RBMS’s  without even realizing the outside world has become better and should be explored.

image
source :- http://bit.ly/VVxmMU

What is NoSql

Stop being the “frog in the well”!! Take the red pill and find out how deep the rabbit hole goes.image

What is NoSQL

As we have been doing “Yes”SQL for quite some time, we will first take a look at what we have been doing for
storing and retrieving data.

Following is very simplified employee database tables.

image

Add some data:-
BEGIN TRY  
    BEGIN TRAN  
    INSERT INTO Department(Id, Name) VALUES(1, ‘Accounts’)  
    INSERT INTO Department(Id, Name) VALUES(2, ‘Engineering’)  
    INSERT INTO Employee (ID, Name, DepartmentID) VALUES (1, ‘Ashish’, 1)  
    INSERT INTO Employee (ID, Name, DepartmentID) VALUES (2, ‘John’, 2) 
    COMMIT 
  END TRY 
  BEGIN CATCH 
    ROLLBACK 
  END CATCH

If we want to retrieve name of the employee and the department in which he/she works using the employee id,
need to join the two tables :-
SELECT E.Name AS EmployeeName, D.Name AS DepartmentName FROM Employee E
INNER JOIN Department D ON  E.DepartmentID = D.Id
WHERE E.Id = 1

Why we needed to join the tables? because all the information for that particular employee is not stored at one table. This is the main characteristic of a RDBMS and this is first thing we need to get off our minds in NoSQL.
Now, there are different types of NoSQL databases – Raven DB, Mongo DB etc. However, in all of them, the basic element is same – No relationship!
In Raven Db, the all data for this employee would be stored in a “document”.
employees/1 
{  
          “Name”: “Ashish”,  
          “Department”:   
            { 
            “Id”: 1,  
            “Name”: “Accounts”  
            }  
}

As you see – all data (which includes department details as well) for employee with id “1” is stored in one
place – a “document”, in RavenDB terms.  This is similar to a “row” in the RDBMS except the fact that data is not distributed in different tables, rather all data is at one place – in a document. For each employee, there would be be one document.

More on this next.

NoSQL using RavenDB

What is Raven Db

Raven DB is a document database.  It has following characteristics :-

  • Non- Relational
  • All data is stored and represented in JSON
  • There is no schema
  • Transactional

Installation
http://hibernatingrhinos.com/builds/ravendb-stableimage

I downloaded the last stable build of Raven DB and extracted to my local “D:\Ashish\Research\RavenDB” folder. Look for /Server/RavenServer.exe.config and change the Anonymous access to “All”

image

Run the Start.cmd as an administrator.image

It should open a very nice looking management studio for Raven Db (performing function “similar” to SQL server management studio). It will ask you to create a new database as you don’t have any. Put a name for the database.image

The new database is created :-image

 

Client application to access and manage data in the Raven DB
Adding the RavenDB client to the client application via Nuget Package managerimage

Below is the client code which when executed creates one document per entity (in this example, a company). Notice that a DocumentStore is similar to a connection. We can use the same connection to create multiple documents. However, each document is bound to a session and that’s why It needs to be disposed before creating a new one. Also notice the RabenDb displays the documents created in the server admin browser.

image

Advertisements

, ,

Leave a comment

Random Thoughts

The World as I see it

Simple Programmer

Making The Complex Simple

Ionic Solutions

Random thoughts on software construction, design patterns and optimization.

Long (Way) Off

A tragic's view from the cricket hinterlands