Page 10 - MSDN Magazine, February 2018
P. 10

Data Points JULIE LERMAN
Creating Azure Functions That Can Read from Cosmos DB with Almost No Code
In my previous two columns, I created a simple Universal Windows Platform (UWP) game, CookieBinge, that tracks the number of cookies a game player gobbles up. In the first of those columns, I showed you how to use Entity Framework Core 2 (EF Core 2) to store the game scores locally on the Windows 10 device where the game is being played. In the second column, I walked you through building an Azure Function that would take game scores sent over HTTP and store them into a Cosmos DB database. The ultimate goal is to modify the game to be able to send its scores to this Function, as well as to retrieve score data from parallel functions.
In this column, I’ll show you how to create the Azure Functions that will respond to requests to retrieve two sets of score data: the top five scores for one user across all of that user’s devices, and the top five scores of all users playing the CookieBinge game around the world. Next time, in the final column in the series, I’ll integrate the new Azure Functions into the CookieBinge UWP app to allow users to share and compare their scores.
The Azure Function I created in the previous article responds to an HTTP call and, using the Function’s integrations, is able to push the data from the HTTP request into the Cosmos DB data- base without me having to write any data access code. Figure 1 displays one of the documents stored in the Binges collection of the CookieBinge database. The first seven properties are what I stored (“logged” through “worthit”) and the rest are metadata created by Cosmos DB when the record was inserted.
For the next two functions, I’ll again take advantage of Azure Function integrations to interact with the same Cosmos DB da- tabase. The only code required for the database interaction this time will be some SQL that represents the necessary query string, and this gets built into the integrations, not the function’s code. Both functions will be triggered by HTTP calls. The first will take in the ID of the user making the request and then return the top five scores the user logged across all of the devices on which they played the game. The second won’t take any input; it will simply return the top five scores of all gamers from around the world.
I’ll walk you through building the first of these Functions using the Azure portal workflow, adding on to the lessons of the previous column. Walking through the first of these functions should help you comprehend how all of the puzzle pieces fit together. I’ll then show you a shortcut when it’s time to build the second. Remember
that it’s also possible to create Azure Functions with the tooling built into Visual Studio 2017, as well as by using the Functions command-line interface (CLI) plus Visual Studio Code.
Creating a Function to Return a User’s Top Scores
Let’s return now to the CookieBinge Azure Functions app in the Azure portal and add a new Function to the app. Recall that when you hover over the Functions group header under the function app’s name, you’ll see a plus sign on which you can click. When you do, you’ll be prompted to choose a template for the new function and, as with the first function you created, it will be a C# HTTP trigger. Name the new function GetUserScores, change the authorization level to Anonymous, then click Create.
In the previous article, I wired up an output integration to the CookieBinge Cosmos DB database, which caused the results of the function to be stored into the database.
Before modifying the code in the run.csx file, I’ll build the func- tion’s integrations. Recall that there are three types of integrations: • Trigger: There can only be a single trigger and in this case,
it’s the HTTP request to the Function.
• Input integrations: There can be multiple input integrations
and these represent other data you want to be available to
the Function.
• Output integrations: In the previous article, I wired up an
output integration to the CookieBinge Cosmos DB data- base, which caused the results of the Function to be stored into the database. With this new GetUserScores function, the output will be the results of the Function sent back to the requestor as an HTTP response.
I’ll wire up the input integration to retrieve data from the Cosmos DB database where the StoreScores Function from last month’s article stored them. This input was a confusing concept for
Code download available at msdn.com/magazine/0218magcode.
6 msdn magazine


































































































   8   9   10   11   12