Lab 4 - Building Globally Distributed Databases With Cosmos DB
Lab 4 - Building Globally Distributed Databases With Cosmos DB
with Cosmos DB
Estimated Time: 90 minutes
Pre-requisites: It is assumed that the case study for this lab has already been read. It is assumed that the
content and lab for module 1: Azure for the Data Engineer has also been completed
Lab files: The files for this lab are located in the Allfiles\Labfiles\Starter\DP-200.4 folder.
Lab overview
The students will be able to describe and demonstrate the capabilities that Azure Cosmos DB can bring to an
organization. They will be able to create a Cosmos DB instance and show how to upload and query data
through a portal and through a .Net application. They will then be able to demonstrate how to enable global
scale of the Cosmos DB database.
Lab objectives
Scenario
The developers and Information Services department at AdventureWorks are aware that a new service
known as Cosmos DB recently released on Azure can provided planetary scale access to data in near real-
time. They want to understand the capability that the service can offer and how it can bring value to
AdventureWorks, and in what circumstances.
The Information Services department want to understand how the service can be setup and how data can be
uploaded. The developers would like to see an example of an application that can be used to upload data to
the Cosmos. Both would like to understand how the claim of planetary scale can be met.
IMPORTANT: As you go through this lab, make a note of any issue(s) that you have encountered in any
provisioning or configuration tasks and log it in the table in the document located at \Labfiles\DP-200-
Issues-Doc.docx. Document the Lab number, note the technology, Describe the issue, and what was the
resolution. Save this document as you will refer back to it in a later module.
Individual exercise
3. In the New screen, click in the Search the Marketplace text box, and type the word Cosmos.
Click Azure Cosmos DB in the list that appears.
5. From the Create Azure Cosmos DB Account screen, create an Azure Cosmos DB Account with the
following settings:
Subscription: the name of the subscription you are using in this lab
API: Core(SQL)
Apache Spark: None
Location: the name of the Azure region which is closest to the lab location and
where you can provision Azure VMs.
Note: The provision will takes approximately 5 minutes. What is often avoided in these labs is a
description of the additional tabs when you provision any service in Azure. You may notice that in
the provisioning screen there will be additional tabs such as Network, Tags or Advanced. This
enables you to define any customized settings for a service. For example, the network tab of many
services enables you to define the configuration of virtual networks, so that you are able to control
and secure the network traffic against a given data service. The Tags option are name/value pairs
that enable you to categorize resources and view consolidated billing by applying the same tag to
multiple resources and resource groups. Advanced tabs will vary dependant on the service that has
it. But it is important to note that you have control over these areas and you wil want to collaborate
with your Network admins or indeed your finance department to see how these options should be
configured.
8. When the provisioning is complete, the "Your deployment is complete" screen appears, click on Go
to resource and move onto the next exercise.
Individual exercise
1. In the Azure portal, once the deployment of Cosmos DB is completed, click on the Go to
resources button.
4. In the Add Container blade, create a Products database with a container named Clothing with the
following settings:
o Database id: Products
o Throughput: 400
o Container id: Clothing
o Partition key: /productId
1. In the awcdbstudcto - Data Explorer screen, on the Data Explorer toolbar, opposite the button for
New Container, click on the Open Full Screen button. In the Open Full Screen dialog box,
click Open. A new tab opens up in Microsoft Edge.
2. In the SQL API pane, click in the refresh icon, and then expand Products, followed by Clothing and
click on Items.
3. In the Documents pane, click on the icon for New Item. A new document appears with a sample
JSON that you will now replace.
5. {
6. "id": "1",
7. "productId": "33218896",
8. "category": "Women's Clothing",
9. "manufacturer": "Contoso Sport",
10. "description": "Quick dry crew neck t-shirt",
11. "price": "14.99",
12. "shipping": {
13. "weight": 1,
14. "dimensions": {
15. "width": 6,
16. "height": 8,
17. "depth": 1
18. }
19. }
}
20. Once you've added the JSON to the Documents tab, click Save.
23. {
24. "id": "2",
25. "productId": "33218897",
26. "category": "Women's Outerwear",
27. "manufacturer": "Contoso",
28. "description": "Black wool pea-coat",
29. "price": "49.99",
30. "shipping": {
31. "weight": 2,
32. "dimensions": {
33. "width": 8,
34. "height": 11,
35. "depth": 3
36. }
37. }
}
38. Once you've added the JSON to the Documents tab, click Save.
39. You can see each document that has been saved by clicking each document on the left-hand menu.
The first item with id of 1, will have a value of 33218896, which is named after the productid, the
second item will be 33218897
1. In the Azure portal, in the Items screen, click on the button New SQL Query that is above the SQL
API Blade.
Note: A Query 1 screen tab appears which shows the query SELECT * FROM c .
2. Replace the query that returns a JSON file showing details for productId 1.
3. SELECT *
4. FROM Products p
WHERE p.id ="1"
6. [
7. {
8. "id": "1",
9. "productId": "33218896",
10. "category": "Women's Clothing",
11. "manufacturer": "Contoso Sport",
12. "description": "Quick dry crew neck t-shirt",
13. "price": "14.99",
14. "shipping": {
15. "weight": 1,
16. "dimensions": {
17. "width": 6,
18. "height": 8,
19. "depth": 1
20. }
21. },
22. "_rid": "I2YsALxG+-EBAAAAAAAAAA==",
23. "_self": "dbs/I2YsAA==/colls/I2YsALxG+-E=/docs/I2YsALxG+-EBAAAAAAAAAA==/",
24. "_etag": "\"0000844e-0000-1a00-0000-5ca79f840000\"",
25. "_attachments": "attachments/",
26. "_ts": 1554489220
27. }
]
28. In the existing query window. Write a query that returns the id, manufacturer and description in a
JSON file for productId
29. SELECT
30. p.id,
31. p.manufacturer,
32. p.description
33. FROM Products p
WHERE p.id ="1"
35. [
36. {
37. "id": "1",
38. "manufacturer": "Contoso Sport",
39. "description": "Quick dry crew neck t-shirt"
40. }
]
41. In the existing query window, write a query that returns returns the price, description, and product ID
for all products, ordered by price, in ascending order.
45. [
46. {
47. "price": "14.99",
48. "description": "Quick dry crew neck t-shirt",
49. "productId": "33218896"
50. },
51. {
52. "price": "49.99",
53. "description": "Black wool pea-coat",
54. "productId": "33218897"
55. }
]
Note: A New Stored Procedure screen appears which shows a sample stored procedure .
3. Use the following code to create a stored procedure in the Stored Procedure Body.
4. function createMyDocument() {
5. var context = getContext();
6. var collection = context.getCollection();
7.
8. var doc = {
9. "id": "3",
10. "productId": "33218898",
11. "description": "Contoso microfleece zip-up jacket",
12. "price": "44.99"
13. };
14.
15. var accepted = collection.createDocument(collection.getSelfLink(),
16. doc,
17. function (err, documentCreated) {
18. if (err) throw new Error('Error' + err.message);
19. context.getResponse().setBody(documentCreated)
20. });
21. if (!accepted) return;
}
```JSON
{
"id": "3",
"productId": "33218898",
"description": "Contoso microfleece zip-up jacket",
"price": "44.99",
"_rid": "I2YsALxG+-EDAAAAAAAAAA==",
"_self": "dbs/I2YsAA==/colls/I2YsALxG+-E=/docs/I2YsALxG+-EDAAAAAAAAAA==/",
"_etag": "\"0000874e-0000-1a00-0000-5ca7a7050000\"",
"_attachments": "attachments/"
}
```
1. In the Azure portal, in the Data Explorer full screen, click on the drop down button for New Stored
Procedure and click New UDF .
2. In the New Defined Function screen, in the User Defined Function Id text box, type producttax.
3. Use the following code to create a user defined function in the user defined function Body.
4. function producttax(price) {
5. if (price == undefined)
6. throw 'no input';
7.
8. var amount = parseFloat(price);
9.
10. if (amount < 1000)
11. return amount * 0.1;
12. else if (amount < 10000)
13. return amount * 0.2;
14. else
15. return amount * 0.4;
}
17. Click on the Query 1 tab, and replace the existing query with the following query:
```JSON
[
{
"id": "1",
"productId": "33218896",
"price": "14.99",
"producttax": 1.499
},
{
"id": "2",
"productId": "33218897",
"price": "49.99",
"producttax": 4.9990000000000005
},
{
"id": "3",
"productId": "33218898",
"price": "44.99",
"producttax": 4.4990000000000005
}
]
```
Individual exercise
2. Managing Failover
4. On the world map, single click a data center location within the continent you reside, and click
on Save.
Note The provisioning of the additional data centers will take approximately 7 minutes
2. Click on the Read Region datacenter location, then click on the check box next to "I understand and
agree to trigger a failover on my current Write Region.", and then click on OK.
Note The Manual Failover will take approximately 3 minutes. The screen will look as follows. Note the icon
colors have changed
1. In the awcdbstudxx - Replicate data globally window, click on Automatic Failover
2. In the "Automatic Failover" screen, click on the ON button, and then click on OK.
If time permits
Note: If you have completed the labs so far and there is time, ask the instructor if you can do exercise 4. It is
an example of building an application against Cosmos DB. This is not an exam requirement for DP200, and
this lab show what is possible
Individual exercise
3. In the Search Extensions in Marketplace text box, type Cosmos DB and click on Azure Cosmos DB. A
document appears in Visual Studio Code and then click on install
Note: A moving bar will appear whilst the extension is being installed. Then a tick will appear next to
Installed once completed.
5. In the Search Extensions in Marketplace text box, type Azure Account and click on Azure Account
extension. A document appears in Visual Studio Code and then click on install
Note: A moving bar will appear whilst the extension is being installed. Then a tick will appear next to
Installed once completed.
Note: Follow the prompts in the web browser to pick an account which authenticates your Visual
Studio Code session. A message in the browser will confirm that you are signed in. You can close
down this tab.
8. In Visual Studio Code, click the Azure icon on the left menu, under Cosmos DB, right-click your
Subscription, and click Create Account.
10. Create a new folder named learning-module in the location of your choice, and then click Select
Folder.
Note: It is possible at this point that Visual Studio Code restarts as you are setting up your working
folder. As a result you have to re- open you lab documentation.
11. In Visual Studio Code, click the Azure icon on the left menu, under Cosmos DB, right-click
your Subscription, and click Create Account.
12. In the text box at the top of the screen named Create new Cosmos DB Account, enter a unique
name for your Azure Cosmos DB account, named xxcosmos, where xx are your initials and then
press Enter on the keyboard.
13. Next, select SQL (DocumentDB), then select the awrgstudxx resource group, and then select
a location closest to you.
Note: A bar will appear at the bottom of Visual Studio Code stating "Creating Cosmos DB Account..."
and will take approximately 5 minutes. The bar will change to state that the creation has occurred
successfully.
14. After the account is created, expand your Azure subscription in the Azure: Cosmos DB pane and the
extension displays the new Azure Cosmos DB account.
16. In the input palette at the top of the screen, type Users for the database name and press Enter.
20. Expand the account in the Azure: Cosmos DB pane, and the new Users database and WebCustomers
collection are displayed.
1. Ensure that file auto-save is enabled by clicking on the File menu and checking Auto Save if it is
blank. You will be copying in several blocks of code, and this will ensure you are always operating
against the latest edits of your files.
2. Open the integrated terminal from Visual Studio Code by selecting View, Terminal from the main
menu.
3. Click in the terminal window, press Enter on the keyboard and then copy and paste the following
command.
4. In the terminal window, copy and paste the following command to run the "Hello World!" program.
dotnet run
5. At the terminal prompt, copy and paste the following command block to install the required NuGet
packages.
13. The terminal screen will load package and display the command at the end dotnet restore, in the
terminal window press Enter.
14. At the top of the Explorer pane, click Program.cs to open the file. Add the following using
statements after using System.
1. In the learning-module folder, create a new file named App.config, and add the following code:
8. Copy your connection string by clicking the Azure icon on the left, expanding your
Subscription, right-clicking your new Azure Cosmos DB account, and then clicking Copy
Connection String.
9. Paste the connection string into the end of the App.config file, and then copy
the AccountEndpoint portion from the connection string into the accountEndpoint value in
App.config.
10. Now copy the AccountKey value from the connection string into the accountKey value, and
then delete the original connection string you copied in.
dotnet run
Note: The program displays Hello World! in the terminal.
19. In the Explorer pane, click Program.cs to open the file. Add the following to the beginning of the
Program class.
20. Add a new asynchronous task to create a new client, and check whether the Users database exists by
adding the following method after the Main method.
30. In the integrated terminal, again, copy and paste the following command to run the program to
ensure that the code runs. "Hello World!" is returned in the console.
dotnet run
31. Copy and paste the following code into the Main method, overwriting the
current Console.WriteLine("Hello World!"); code:
32. try
33. {
34. Program p = new Program();
35. p.BasicOperations().Wait();
36. }
37. catch (DocumentClientException de)
38. {
39. Exception baseException = de.GetBaseException();
40. Console.WriteLine("{0} error occurred: {1}, Message: {2}", de.StatusCode,
de.Message, baseException.Message);
41. }
42. catch (Exception e)
43. {
44. Exception baseException = e.GetBaseException();
45. Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
46. }
47. finally
48. {
49. Console.WriteLine("End of demo, press any key to exit.");
50. Console.ReadKey();
}
51. In the integrated terminal, again, copy and paste the following command to run the program to
ensure it runs.
dotnet run
Create Documents
1. Create a User class that represents the objects to store in Azure Cosmos DB. We will also
create OrderHistory and ShippingPreference classes that are used within User. Note that
documents must have an Id property serialized as id in JSON.
58. In the integrated terminal, again, copy and paste the following command to run the program to
ensure it runs.
dotnet run
82. Then, return to the BasicOperations method and add the following to the end of that method.
156. In the integrated terminal, again, copy and paste the following command to run the program
to ensure it runs.
dotnet run
Read Documents
1. To read documents from the database, copy in the following code and place after
the WriteToConsoleAndPromptToContinue method in the Program.cs file.
20. Copy and paste the following code to the end of the BasicOperations method, after the await
this.CreateUserDocumentIfNotExists("Users", "WebCustomers", nelapin); line:
21. In the integrated terminal, again, copy and paste the following command to run the program to
ensure it runs.
dotnet run
Replace Documents
20. Copy and paste the following code to the end of the BasicOperations method, after the await
this.CreateUserDocumentIfNotExists("Users", "WebCustomers", nelapin); line.
22. In the integrated terminal, again, copy and paste the following command to run the program to
ensure it runs.
dotnet run
Delete Documents
20. Copy and paste the following code to the end of the BasicOperations method.
21. In the integrated terminal, again, copy and paste the following command to run the program to
ensure it runs.
dotnet run
1. The following sample shows how a query could be performed in SQL, LINQ, or LINQ lambda from
your .NET code. Copy the code and add it after private async Task
CreateUserDocumentIfNotExists towards the end of the Program.cs file.
32. Copy and paste the following code to your BasicOperations method, before the await
this.DeleteUserDocument("Users", "WebCustomers", yanhe); line.
this.ExecuteSimpleQuery("Users", "WebCustomers");
33. In the integrated terminal, again, copy and paste the following command to run the program to
ensure it runs.
dotnet run
2. In the text box at the top of the screen, type UpdateOrderTotal and press Enter to give the stored
procedure a name.
Note: By default, a stored procedure that retrieves the first item is provided. if not perform the
following steps:
4. To run this default stored procedure from your application, add the following code to
the Program.cs file as the first entry after the class Program.
9. Copy the following code and paste it before the await this.DeleteUserDocument("Users",
"WebCustomers", yanhe); line in the BasicOperations method.
10. In the integrated terminal, again, copy and paste the following command to run the program to
ensure it runs.
dotnet run