introduction to document databases with ravendb
DESCRIPTION
Introduction to Document Databases with RavenDB. David Green @ davidjeet. Agenda. NoSql databases Prereqs CAP Theory Features of Document Databases Features of RavenDB Getting Started – Deployment Raven Studio Code Demo 1 – CRUD + Http API - PowerPoint PPT PresentationTRANSCRIPT
Introduction to Document Databases with RavenDB
David Green@davidjeet
Agenda NoSql databases Prereqs CAP Theory Features of Document Databases Features of RavenDB Getting Started – Deployment Raven Studio Code Demo 1 – CRUD + Http API Code Demo 2 – MVC3 Template, Indexes, Map-Reduce,
Lucene, Http API Extras
Pre-Reqs: What you need to know
• .NET Framework, C# 3.5/4.0
• Basic familiarity with LINQ & lambda expressions
• Maybe some JavaScript (if you want to use HTTP API)
NoSQL databases
Graph Key-value store
Object Document
Document databases• Key/Value: A document database is, at its core, a key/value
store with one major exception.
• Format: Instead of just storing any blob in it, a document db requires that the data will be store in a format that the database can understand (XML, JSON, Binary JSON, etc.)
• Schema Free: A document database is schema free, that is, you don’t have to define your schema ahead of time and adhere to that.
• Relationships Not Enforced: It does not, however, support relations. Each document is standalone. There is nothing to enforce relational integrity.
• Benefit: The major benefit of using a document database comes from the fact that while it has all the benefits of a key/value store, you aren’t limited to just querying by key.Excerpts from: http://ayende.com/blog/4459/that-no-sql-thing-document-databases
CAP Theory
Id Title Author Price33 “War & Peace” “Leo Tolstoy” 29.99
Books/33
Id Title Author417 “Pro ASP.NET MVC 4” Adam
Freeman
Books/417
Id Title Price Inventory
59 “Poke the Box”
7.18 100
Books/59
Example of a schema-less table
RavenDB• RavenDB is a document database system
• Created by Oren Eini (Ayende Rahein)
• Beyond just being your typical database, it is also a web server.
• Uses LINQ for querying (i.e. Map-Reduce).
• Uses Lucene search engine.
• Supported in MVC4, WebForms, Winforms, Console…everything
• RavenDB is optimized for reading. (some Document databases optimized for write)
• Adheres to some BASE principles:
• Basic Availability• Soft-state• Eventual consistency
Getting Started - DeploymentRaven DB has four different methods for deployment:
1. Server mode (console, not to be used in production)
2. IIS
3. Windows service
4. Embedded
Getting Started - Server Mode1. Create (or use an existing) a new solution + project
that will be using RavenDB. It doesn’t matter what kind of project this is (i.e. console, WinForms, ASP.NET, etc.)
2. With NuGet package manager, search on “RavenDB” and you can select from:
1. Raven (Embedded)2. Raven (Client)3. Raven (Server)Select the third option “Raven” which will install the server piece in the packages folder of your solution.
Getting Started - Server Mode
Raven Studio (like SSMS)http://localhost:8080/raven/studio.html
It’s Silverlight based (make sure you have Silverlight installed)
It *sometimes* auto-launches when firing up the console:
1. In Solution Explorer, right-clickOpen File In Windows Explorer
2. Navigate to packages/RavenDB.x.y.zzz/server
3. Create a .cmd file (notepad) with the following line:start Raven.Server.exe --debug –browser
4. Use the .cmd file you just created to launch the console (it will also launch the browser)
Basic CRUD Demo – Music Store• Read (w/o model)
• Read (basic)
• Read (dynamic index)
• Create
• Update
• Delete
Set “Target Framework” for Console Apps
Advanced Demo – StackExchange• Using a Base Controller (MVC3)
• Handling Joins
• Map-Reduce
• Indexes
• Lucene Search Engine
• Http API
Relational View of StackExchange
MVC3 Template for RavenDB usage
• Can use Singleton Design pattern or Dependency Injection.
• Using a base class for the controller (i.e. “RavenController”)
• Overriding the OnActionExecuting and OnActionExecuted event.
How to Handle Relationships in RavenDB
Couple of ways to do this:
• One common technique is to use .Include<T>() method.
• Depending on the scenario, de-normalization can be used instead.
IndexesTwo types of Indexes in RavenDB:
• Static
• Dynamic
There are a few reasons why to prefer static indexes over dynamically created ones:
High latency - Index creation is not a cheap process, and may take a while to execute. Since dynamic indexes are created on the fly on first user query, first non-stale results may take a long time to return. Since dynamic indexes are created as temporary indexes, this is going to be a performance issue on first run.
Flexibility - Static indexes expose much more functionality, like custom sorting, boosting, Full Text Search, Live Projections, spatial search support, and more.
Map- Reduce
from post in docs.Postsfrom Tag in post.Tagsselect new { Tag, Count = 1 }
from result in resultsgroup result by result.Tag into gselect new{
Tag = g.Key,Count =
(int)g.Sum(x=>x.Count)}
Example: Creating an index called Posts/TagCount
Reduce
Map
Lucene
Examples:
With “Posts” Collection:• (PostTypeId: 1 AND Title: *relativity*) • (PostTypeId: 1 AND AnswerCount: 6)• Title: “particle energy"~4 //proximity search example
With “Users” Collection:• DisplayName: David~0.4 // fuzzy search example• DisplayName: David~0.8
Syntax: http://www.lucenetutorial.com/lucene-query-syntax.html
Extras• Additional Resources
• Deleted Scenes
• Behind the camera with David Green
RavenDB Deployment – Embedded Mode
Helpful Tools• Couple tools out there – Some are RavenDB specific, others like Fiddler and LinqPad also work well.
• For setup/install:• http://
blogs.hibernatingrhinos.com/5/ravendb-in-practice-part-1-an-introduction-to-ravendb
• http://ravendb.net/docs/intro/quickstart/adding-ravendb-to-your-application• http://www.codecapers.com/post/Using-RavenDB-with-ASPNET-MVC.aspx• http://msdn.microsoft.com/en-us/magazine/hh547101.aspx
• Best Intro:• http://ravendb.net/docs/client-api• http://ravendb.net/docs/http-api• http://ravendb.net/docs/server/deployment• http://ravendb.net/kb/3/using-ravendb-in-an-asp-net-mvc-website• http://
blogs.hibernatingrhinos.com/3073/ravendb-in-practice-part-2-using-the-client-api
• http://www.codeproject.com/Articles/74322/RavenDB-An-Introduction• http://www.codecapers.com/post/Using-RavenDB-with-ASPNET-MVC.aspx• http://
tech-rash.blogspot.com/2012/02/is-raven-db-all-its-cracked-up-to-be.html (review)
• Relations/joins: • http://daniellang.net/how-to-handle-relations-in-ravendb/• http://old.ravendb.net/faq/includes
Helpful Links
Resources
MongoDB vs. CouchDB vs. RavenDBTaken from:
http://weblogs.asp.net/britchie/archive/2010/08/17/document-databases-compared-mongodb-couchdb-and-ravendb.aspx
Pricing/features for RavenDB 1.2RavenDB is being split into three editions:
RavenDB Basic - monthly subscription only (5$ - 512 MB db size limit, 5 databases limit. 10$ - 1GB db size limit, 10 databases limit). A single indexing thread, 2 GB memory limit.
RavenDB Standard - what we have with RavenDB right now. 6 CPU + 12 GB limit. 999$ for a one time payment - 18 months upgrade protection. 399$ for a yearly subscription - as long as you have a current subscription, you can use the software and get free upgrades. 39$ for a monthly subscription
RavenDB Enterprise - per core licensing. No limits on CPU / Mem. Additional features, detailed in the previous email. Per core pricing (# of cores == Environment.ProcessCount) is 699$ per core . 18 months upgrade protection. 299$ per core per year. 79$ per core per quarter
In addition to that we have two additional licensing modes:1) OEM - embedded only - 1599$ per developer for the first year. 999$ per developer for renewal. 2) Scaleout - bundles of 10, 20 and 50 licenses for scaleout / sharding scenarios, which are sold at a reduced cost.
Development will continue to be free! (emphasis added).