Translate

Sunday, November 11, 2012

RavenDB tutorial (A beginners introduction to RavenDB)


What is NoSQL?

RavenDB is a NoSQL database. Lets take a brief look at NoSQL before proceeding to RavenDB. NoSQL stands for not only SQL. Since the late 80s RDBMS (Oracle SQL etc) have been the technology of choice to save large amounts of data. They offered many advantages such as ability to perform advanced queries, ability to set referential integrity to maintain consistency of data and so on. For a majority of the enterprise needs, RDBMS lends itself very well. However there is one fatal flaw in RDBMS. They do not scale very well. You could employ sharding (Database sharding is a technique of horizontal partitioning data across multiple physical servers to provide application scale-out.) to scale SQL server, but then you lose many of the advantages such as referential integrity, ability to join etc, that RDMS have to offer. Plus there are other technical challenges that is beyond the scope of this article. To overcome the hurdle of scaling RDBMS, NoSQL was invented. Google (with their Big Table) and Amazon (with their Dynamo) are widely believed to have started the trend towards NoSQL.

When to use NoSQL?

If your application demands the following, NoSQL should be considered as an alternative to SQL server or Oracle
  • You application needs to scale across multiple servers.
  • Your data is semi structured.  (eg discussion threads, product catalogs, this blog article !)

However if your application has the following needs, RDBMS would be strongly preferred.

  • You need to run advanced queries against your data.
  • Referential integrity between data is very important.













By far scalability is the most important reason to choose NoSQL.

It is important to note that within a single application you could use both a relational database and NoSQL based on the requirements of the module. This new trend of using multiple persistence technologies within an application is called Polyglot Persistence.

Kinds of NoSQL databases.

NoSQL can be broadly classified into the following three kinds
  • Key-Value store
  • Document database (Lotus notes was the first document database to be sold commercially)
  • Column family database

In this article we will be focusing on one kind of a document database called the RavenDb. Although MongoDB is the most popular document DB out there, we still choose Raven Db because its the best document database for .net developers. Mainly because it supports LINQ. (However there are drivers out there which facilitate some use of Linq in MongoDB. I think in time it might develop into a full fledged LINQ support.)

Advantages of RavenDB
  • It is not limited to querying by key.
  • Supports partial update.
  • Supports Linq
  • Facilitates coarse grained locks

Installing RavenDB Management Studio

Perquisites
  • .net 4.0 framework
  • Silverlight
  • IIS 7.5

Follow these steps to manipulate RavenDB database using a browser.

1>Download RavenDB
http://hibernatingrhinos.com/builds/ravendb-stable-v1.0/960

2>Unzip the file and copy "Web" folder to C:\inetpub\wwwroot

3>Modify the web.config to update
<add key="Raven/AnonymousAccess" value="Get"/> 
to
<add key="Raven/AnonymousAccess" value="All"/>
 (Note: This is not recommended for production deployment. Check here for more)


4>In IIS manager, add an application that points to this folder.Set the Alias of the application as RavenStudio.  Make sure the application pool is set to use .net framework 4.0.























Now you can view and edit the database in ie (or any browser) using this url
http://localhost/RavenStudio/raven/studio.html






















Play around in this management studio to get a good feel for RavenDB.  Note that the files that you save in RavenDB are NOT visible in the file folder as separate files. They are all created inside a single file called Data. If you delete this file, all the files in the database is lost.


Setting up Visual studio to manipulate RavenDB


Follow these steps to be able use C# code to manipulate RavenDB. Please note that you should have successfully set up RavenDB Studio as shown above before proceeding ahead.

1>Create an MVC 3 (or 4) application using Visual Studio

2>Add references to the following files that you got in the download above. It is located under RavenDB-Build-960\Client.

  • Raven.Abstractions.dll
  • Raven.Client.Lightweight.dll
  • Raven.Client.MvcIntegration
  • Newtonsoft.Json.dll


3>Add this to your web.config

<connectionStrings>
   <add name="Server" connectionString="Url=http://localhost/Raven"/>
</connectionStrings>

4>Qualify the following namespaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Raven.Client;
using Raven.Client.Document;




Accessing RavenDB through code

Suppose a class Person as shown below exists


public class Person
{
    public string Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}


Before you can start using RavenDb, you need to create a store. Note that creating a store is very expensive and only one store must exist per application.


var store = new DocumentStore
 {
   ConnectionStringName = "Server"
 };
  store.Initialize();



To save a new object in RavenDB


var person = new Person { Id="1", Name = "Bill", Age = 50 };

using (IDocumentSession session = store.OpenSession())
{

    session.Store(person);
    session.SaveChanges();
}





To query RavenDB

using (IDocumentSession session = store.OpenSession())
{
    var results = from p in session.Query<Person>()
    where p.Name == "Bill"
    select p;
}






To update a Person record in RavenDB where the ID is 1


using (IDocumentSession session = store.OpenSession())
{ 
  var existingPerson = session.Load<Person>("1");

  existingPerson.Age = 47;
  session.SaveChanges();
}



To delete an existing object in RavenDB

using (IDocumentSession session = store.OpenSession())
{

    var existingPerson = session.Load<Person>("1");

    session.Delete(existingPerson);
    session.SaveChanges();
}


3 comments:

  1. Thanks for so great article! It's made my first steps with RavenDB much easier.

    If you have time and if you want please make more articles regarding this DB!

    ReplyDelete
  2. It was very helpful. Thank you

    ReplyDelete

Comments will appear once they have been approved by the moderator