270 likes | 308 Views
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
E N D
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 Document Object
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
Example of a schema-less table Books/33 Books/417 Books/59
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: • BasicAvailability • Soft-state • Eventual consistency
Getting Started - Deployment Raven DB has four different methods for deployment: Server mode (console, not to be used in production) IIS Windows service Embedded
Getting Started - Server Mode • 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.) • With NuGet package manager, search on “RavenDB” and you can select from: • Raven (Embedded) • Raven (Client) • Raven (Server) • Select the third option “Raven” which will install the server piece in the packages folder of your solution.
RavenStudio(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: In Solution Explorer, right-clickOpen File In Windows Explorer Navigate to packages/RavenDB.x.y.zzz/server Create a .cmd file (notepad) with the following line:start Raven.Server.exe --debug –browser 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
Advanced Demo – StackExchange • Using a Base Controller (MVC3) • Handling Joins • Map-Reduce • Indexes • Lucene Search Engine • Http API
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.
Indexes • Two 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 Example: Creating an index called Posts/TagCount Map from post in docs.Posts from Tag in post.Tags select new { Tag, Count = 1 } Reduce from result in results group result by result.Tag into g select new { Tag = g.Key, Count = (int)g.Sum(x=>x.Count) }
Lucene Syntax:http://www.lucenetutorial.com/lucene-query-syntax.html • 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
Extras • Additional Resources • Deleted Scenes • Behind the camera with David Green
Resources Helpful Tools • Couple tools out there – Some are RavenDB specific, others like Fiddler and LinqPad also work well. Helpful Links • 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
MongoDB vs. CouchDB vs. RavenDB Taken from: http://weblogs.asp.net/britchie/archive/2010/08/17/document-databases-compared-mongodb-couchdb-and-ravendb.aspx
Pricing/features for RavenDB 1.2 RavenDB 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).