Learning Azure DocumentDB - Sample Chapter
Learning Azure DocumentDB - Sample Chapter
pl
e
P r o f e s s i o n a l
$ 34.99 US
22.99 UK
P U B L I S H I N G
Sa
Riccardo Becker
Learning Azure
DocumentDB
ee
E x p e r t i s e
D i s t i l l e d
Learning Azure
DocumentDB
Create outstanding enterprise solutions around DocumentDB
using the latest technologies and programming tools with Azure
Riccardo Becker
P U B L I S H I N G
Preface
In August 2014, Microsoft announced the preview of their DocumentDB service
offering. DocumentDB is a NoSQL database that meets the requirements of today's
applications that evolve at a higher pace, including modifications to the data model.
DocumentDB covers all this by offering a schema-free document database that
offers flexibility, scalability, automated indexing, and a mature range of querying
possibilities. Recently, Microsoft announced support for geospatial indexing and
querying, opening a whole new range of possibilities.
Setting an example, Microsoft utilizes DocumentDB in its renewed
http://www.msn.com, servicing 450 million unique monthly visitors.
25 percent of these visitors have their own personalized data and this data
is services from DocumentDB.
DocumentDB greatly enhances the flexibility of your evolving applications and offers
a rich querying environment for any kind of data that you store.
This book covers all the concepts and possibilities of DocumentDB by covering the
theory with lots of examples in every chapter. This book is linear and every chapter
builds on the knowledge of the last. For the more experienced users, chapters can
also be read individually and in any order.
By the end of this book, you will be able to use DocumentDB in your own daily work
and benefit from best practices.
Preface
Using the Fiddler tool to demonstrate the usage of the REST API
[ 63 ]
Check out the following table to see the differences between the verbs and
some examples:
Verb
POST
GET
PUT
DELETE
Operation
Happy flow
Unhappy flow
Creates a
resource
POST/devices
201 created
Gets a resource
GET/devices
GET/devices/{id}
Updates a
resource
PUT/devices
PUT/devices/{id}
Deletes a
resource
DELETE/devices
DELETE/devices/{id}
Description
Client-server
Stateless
[ 64 ]
Chapter 5
Constraint
Description
Cacheable
Uniform
interface
The base URI for the rest of the resources are based on the URI endpoint that is
specifically created for the database account name. The following table shows the
base URI for each type of resource:
Resources
Database
User
Permission
Collection
Stored
procedure
Base URI
https://{databaseaccount}.documents.azure.com/dbs/{_riddb}
https://{databaseaccount}.documents.azure.com/dbs/{_riddb}/users/{_rid-user}
https://{databaseaccount}.documents.azure.com/dbs/{_riddb}/users/{_rid-user}/permissions/{_rid-perm}
https://{databaseaccount}.documents.azure.com/dbs/{_riddb}/colls/{_rid-coll}
https://{databaseaccount}.documents.azure.com/dbs/{_riddb}/colls/{_rid-coll}/sprocs/{_rid-sproc}
[ 66 ]
Chapter 5
Resources
Trigger
UDF
Document
Attachment
Base URI
https://{databaseaccount}.documents.azure.com/dbs/{_riddb}/colls/{_rid-coll}/triggers/{_rid-trigger}
https://{databaseaccount}.documents.azure.com/dbs/{_riddb}/colls/{_rid-coll}/udfs/{_rid-udf}
https://{databaseaccount}.documents.azure.com/dbs/{_riddb}/colls/{_rid-coll}/docs/{_rid-doc}
https://{databaseaccount}.documents.azure.com/dbs/{_riddb}/colls/{_rid-coll}/docs/{_rid-doc/attachments/{_ridattch}
Description
Authorization
Content-Type
[ 67 ]
masterKeyBytes = Convert.FromBase64String(masterKey);
hmacSha256 = new HMACSHA256(masterKeyBytes);
resourceIdInput = resourceId ?? string.Empty;
resourceTypeInput = resourceType ?? string.Empty;
Depending on the verb (GET, POST, and so on), the resourceType (dbs, colls, or
docs), the resourceId (optional), and the master key that can be found in the Azure
portal, the authorization key is generated.
[ 68 ]
Chapter 5
Using Fiddler to enter the raw REST input, we get the following result:
Adding a document
The following snippets show how to create a document in a certain collection:
//get signature based on masterkey, collection resource id and
resource type documents (docs)
signature = GetSignature(masterKey, collectionRid, "docs", "POST");
client.DefaultRequestHeaders.Remove("authorization");
[ 69 ]
This time, we need to use the POST verb (which affects the authorization key as well).
The payload of the PostAsync call is a JSON file and the final status code is created.
Again, the REST representation of this operation in Fiddler is shown in the
following screenshot:
Now that we have seen how to use the GET and POST verb against the REST API of
DocumentDB, we can execute all operations needed in an enterprise scenario for
platforms that do not have a DocumentDB SDK available.
The source projects also demonstrate how to get the right collection (similar to listing
all databases) by providing a resource ID representing the designated database.
[ 70 ]
Chapter 5
Summary
In this chapter, we covered the very basics of the REST protocol and what verbs are
available in the protocol. We saw that every single resource inside our DocumentDB
can be referenced by a unique URI. This URI is the basis for executing REST-based
queries against our databases, collections, documents, and all other available
resources. We wrote some C# code in order to generate the authorization key that
is needed for every REST request. Finally, we fired some REST queries against our
DocumentDB to get a list of all the databases and how to create a document inside
a collection. We used C# and we saw the raw HTTP representations of the same
queries inside Fiddler.
In the next chapter, you will learn how to use Node.js to perform a subset of the
activities from this chapter. The focus will be on how to use DocumentDB from
Node.js to kick-start your Node.js projects.
[ 71 ]
www.PacktPub.com
Stay Connected: