Tuesday, June 16, 2015

Azure DocumentDB - First look

DocumentDB is the Microsofts NoSQL database as a service implementation on Azure for modern web and mobile applications. Some of the benefits of DocumentDB is that it can deliver consistently fast reads and writes, allow schema flexibility as in the case of all NoSQL databases and thus with the ability to easily scale a database up and down on demand.

It’s well integrated with JavaScript and supports complex queries in a SQL language and also has a strong server side programming model of UDFs, functions and stored procedures. If you have a valid Azure subscription, you can create a document db account and start working immediately. All resources within DocumentDB are modeled and stored as JSON documents. Resources are managed as items, which are JSON documents containing metadata, and as feeds which are collections of items. Sets of items are contained within their respective feeds.

You can create a DocumentDB account by logging into Azure and choosing Azure Document DB under the Data + Storage options.

Enter a valid AccountId, choose a resource group and create.

Once the DocumentDB account is created successfully, you can see the notifications changed with the details and the account details in the home page.

Click on the account details on the home page and then navigate to the Keys section to see, the details of the uri and keys for the account. Also note the databases section which is empty right now.

We’ll see how we can use the .NET APIs to use code first approach on creating a database and add entries to the collection. For that, we’ll use an ASP.NET MVC application.

The DocumentDB .NET SDK is packaged and distributed as a NuGet package, which can be installed using the Install-Package cmdlet.

The uri and key management for our demo application is handled in the configuration file as app settings.

Once we have these settings configured, we can use these details to create an instance of the DocumentClient object which we’ll use to create the database and collections.

var endpoint = ConfigurationManager.AppSettings["endpoint"];
var authKey = ConfigurationManager.AppSettings["authKey"];
var endpointUri = new Uri(endpoint);

_client = new DocumentClient(endpointUri, authKey);

A database instance can be created like

_database = _client.CreateDatabaseAsync(new Database { Id = DatabaseId }).Result;

If you have the database created, then using the database instance the collection can be created as


_client.CreateDocumentCollectionAsync(_database.SelfLink, new DocumentCollection { Id = CollectionId }).Result;


No comments: