Page 11 - MSDN Magazine, February 2018
P. 11

Figure 1 A Record from the CookieBinge Binges Collection
me at first. I assumed I’d have to write some code in the Function to connect to that database and query for the users’ scores. But the input integration is pretty cool. I can define it with the relevant SQL for querying the Cosmos DB database and let it know to use the UserId value that was sent in with the HTTP request. And Azure Functions takes care of the rest—connecting to the database, ex- ecuting the query and handing me the query results to work with in the function code. Yes, very cool indeed! Let’s do it.
Creating an Input Integration to Read
from Cosmos DB Using HTTP Request Data
Start by clicking New Input in the Integrations panel, then from the list of input types presented, choose Cosmos DB and click the Select button below it. This results in the Cosmos DB input form, its fields populated with default values. Modify the values as shown in Figure 2.
The values for the Database name, Collection Name and Azure Cosmos DB account connection should match the ones you used for creating the StoreScores function’s output integration. In my case, that’s CookieBinge, Binges and datapointscosmosdb_DOCUMENTDB. I’ve named my parameter “documents,”
Azure Cosmos DB has a SQL query syntax that makes it nicely familiar to work with.
Notice the placeholder for the userID variable, {userId}. I’ll use this variable to wire up the trigger and this input integration. First, I need to save the settings of this input. Then I’ll need to create a binding param- eter to bind the trigger to the input. This can be done in a way that was not immediately obvious to me. In fact, before I got it set up correctly, I kept getting an error about the binding parameter being missing and it took some effort putting together what I was learning from the docs, Web search results and some experimentation before I
got it right. Part of the problem was my lack of understanding, al- though once I found the solution, the connections made sense to me. Hopefully you’ll see the light more quickly because I’m about to spoon-feed the solution to you!
Azure Cosmos DB has a SQL query syntax that makes it nicely familiar to work with.
Select the HTTP (req) trigger and specify a route template for the trigger. The template should first provide a name for the route, and then specify the binding variable, userId.
The route doesn’t have to match the name of the function, but it’s probably clearer if it does. I named mine GetUserScores/{userId}, as shown in Figure 3. Don’t forget to save your changes.
The output integration can stay at its default, which is an HTTP response.
which I’ll use in my function’s code. Document ID is left blank. If you were building a function to retrieve a sin- gle Cosmos DB document by its ID, this would be a handy option. But this function will be querying based on an incoming UserId.
The SQL Query is cut off in the screenshot, so here it is in full. Keep in mind that you need to add it as a single line, though here I’ve used
line returns to make it easier to read:
SELECT TOP 5 c.score,c.worthit,c. deviceName,c.dateTime
FROM c
WHERE c.userId={userId}
ORDER by c.score DESC
Figure 2 The Azure Cosmos DB Input Integration Settings
msdnmagazine.com
February 2018 7


































































































   9   10   11   12   13