Retrieving Data from Cosmos DB

In this section, we'll retrieve documents from the Cosmos DB for display or manipulation. We have deployed Cosmos DB with transactions in the collection:

Let's begin by implementing the following steps:

  1. Create an Azure Function called GetTransactions and use the HttpTrigger template without parameters and Anonymous access rights.
  2. Add the line shown in the following screenshot to the method signature. You are adding a DocumentDB client as an input to the function, below the trigger. This allows you to execute more complex queries against DocumentDB:
  1. Next, you need to set some options for your query, setting the max amount of records the Cosmos DB will return. Remove the code inside the Run method. Create a FeedOptions object and set MaxItemCount to –1 (allowing dynamic page size):
The FeedOptions object allows you to set options for things like which partition to search in and the maximum degree of parallelism for Cosmos DB to operate at.

Refer to the complete code placed at: Code/Serverless-Architectures-with-Azure/Lesson 2/BeginningAzureServerlessArchitecture/GetTransactionsExBStep2.cs.

Go to https://goo.gl/e3dAqx to access the code.
  1. Create the query. Use the CreateDocumentQuery method on the DocumentClient object. There is a helper method for creating the collection uri that it demands in UriFactory.CreateDocumentCollectionUri. Finally, pass in the FeedOptions object that you created before:
Refer to the complete code placed at: Code/Serverless-Architectures-with-Azure/Lesson 2/BeginningAzureServerlessArchitecture/GetTransactionsExBStep5.cs.

Go to https://goo.gl/aZybgD to access the code.
  1. Execute the query against Cosmos DB by creating a foreach loop through each transaction in the IQueryable result. Add each to a list of transactions:
Refer to the complete code placed at: Code/Serverless-Architectures-with-Azure/Lesson
2/BeginningAzureServerlessArchitecture/GetTransactionsExBStep7.cs.

Go to https://goo.gl/A7pyzk to access the code.

If you test this function with Postman, you will see the transactions that you previously entered into the Cosmos DB. You now have a completely serverless RESTful API. This requires no operational setup up of containers or virtual machines (VMs). The API will scale infinitely, along with the database behind it, for any use case. A schema has effectively been enforced by the use of C# objects on the API, so you can rely upon whatever you receive from it, despite it being stored in a NoSQL database.

It's still a good idea to add more complete error handling to avoid unexpected objects from the Cosmos DB causing problems.

A RESTful web service is one that implements the Representational State Transfer (REST) architectural style. This is a very popular architectural style on the web.

Outcome

You have linked an Azure Function to Cosmos DB and successfully retrieved the documents.

One of the interesting developments of the Microservices architecture movement is the concept of monetizing APIs. The idea is that your information is valuable, and as such you can charge for access to your APIs. Most organizations settle for charging for API keys, with few having the data granularity to charge by request. But how do you set that price? Serverless offers a great indicator, because you can literally see how much each request is costing you. There is no sunk cost offsetting that price either, so pricing your APIs at a certain profit margin above that cost is enough to make the service profitable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.218.221