Introduction to Cosmos DB
Introduction to Using Cosmos DB
Introduction to Creating an App with Cosmos DB
The course is part of these learning pathsSee 5 more
Cosmos DB is one of many database solutions in a crowded market. From DynamoDB to Cassandra to CockroachDB, the questions one would naturally ask when examining Cosmos DB are, “what makes it special and how can I get started with it?”
This course answers both of those questions as thoroughly and concisely as possible. This course is for anyone with an interest in database technologies or creating software using Microsoft Azure. Whether you are a DevOps engineer, a database admin, a product manager, or a sales expert, this course will help you learn how to power your technology with one of Azure's most innovative data solutions.
From this course, you will learn how to make use of Cosmos DB's incredible flexibility and performance. This course is made up of 9 comprehensive lectures to guide you from the basics to how to create your own app using Cosmos DB.
- Learn the basic components of Cosmos DB
- Learn how to use Cosmos DB via the web portal, libraries, and CLI tools
- Learn how to create an application with Cosmos DB as the data backend
- People looking to build applications using Microsoft Azure
- People interested in database technologies
- General knowledge of IT architecture
- General knowledge of databases
Okay, so now we're coming into the final section of our demo. We're going to do an end-to-end validation of our app. What we did before with Cosmos DB, we were able to send a test message through the Azure function dashboard, and we were able to see it persisted in Cosmos DB. So that's good, but what we really want to see is that the Event Hub works is able to capture a message and then that will trigger the Azure function, and then it will be persisted in Cosmos DB.
So we wanna see all three steps. Now, in order to do that we have to send a message to our Event Hub. And there's a lot of ways we can do this. If you're familiar with PowerShell or command line, if you can use a Python Repl, you can write some code. you could send a curl command with the right authentication and all that. But these aren't the most user-friendly approaches if you are not, if you don't have experience in programming.
So if you're not an experienced programmer, then I would say the easiest thing to do is to just use Visual Studio and the SampleSender library that you could get from Microsoft GitHub. So we'll give you links for that, downloading and installing Visual Studio is not too difficult. You can do it right, obviously on Windows works great or other platforms. And then the SampleSender library, you'll just download that from GitHub. And once you have it, you're just gonna open it, you're gonna go into you're gonna wanna open a project solution yeah that's correct. You can look for an SLN file and that should just pop up in SampleSender directory. And you're gonna want here SampleSender.SLN, okay. And once you have that, you're ready to go. You're basically ready to send your messages, although actually sorry, you're not quite ready to go.
You have to change two things here. You have to put in the right name for your Event Hub and you have to put in the right authentication, the right connection string. This is the credential it's using it. And right now it has the old ones for my previous test. So we have to get the correct name and connection string. So we have to figure out where can we get that from.
Well, you can get it from the dashboard and I'll show you how to do that in one sec. Okay, so in order to get the connection string, we're gonna go into the dashboard and I'll show you how that's, how you obtain it. First, we can change this to bethune2, because we know already the Event Hub name is bethune2, that's in the dashboard.
If we click on bethune2 namespace, we will see here, this is the Event Hub itself bethune2. Now that connection string, in order to get that, we need to have a policy, a connection policy that will generate that credential. So in here where it says shared access policies, there's none by default. What we can do is we click add, and then this will let us create a policy. We'll just call it default. And we'll say that this is for sending and for listening, we don't need to give it management permission. I mean we can, it doesn't really matter. This is just for a test, but for this one, we'll go ahead and click create, we'll call it default and we'll see it should generate those connection strings if we... Okay yeah already created it.
So we click here and then here are the strings that we need. There's a primary key, secondary key, connection string, a secondary key here. We care about this one connection string, primary key. We wanna copy this to our clipboard. And then we go into the code and we're gonna get rid of this one here. We don't need this anymore. This is the old one, and then we'll just paste in the new one and there you go. And so now we have our credentials set properly.
So the last thing to do is so we'll save it. We'll make sure that this is all set there. The last thing we have to do is actually run this code and we should see the messages appear in the Cosmos DB backend.
Now just to show that they're not already there as a some kind of slight of hand trick here, we'll go into Cosmos DB now. We'll go into our Data Explorer and we should see as it connects, if we go into the collection, we should see that there's only maybe one or two items from when we ran the tests with the Azure function.
So if we go here and then go to this container, we click on items. It's only loading that one silly test message we have here, this banana face thing. So there's nothing else there. Cause this is selecting everything, it's not finding anything. So if we run this code and it succeeds we should see more messages propagate.
So, let's see if it works. We click on this green arrow here to run the code samplecenter, and it should start spinning everything up. We'll see a terminal pop-up and there we go. All right, our code is executing and it should send exactly 100 messages. They're all just small string that says the message and the number. And then if everything is working properly, these messages will hit the Event Hub trigger the Azure function, and then the function output will put them into Cosmos DB.
So we'll go ahead and click anything here. Continuous X out I suppose and then we'll close this. Now one thing we can do to validate the traffic is we can actually look at the Event Hub logs as well. We can go here to bethune2 and look at this bethune2 Event Hub. And this will tell us if any traffic or messages are going through.
Now it'll take a while for them to show up. We don't see it right away. We can also look at the, at the Azure function. We can see the logs for the Azure function. We can see if it's executing at all. So we can go to our bethune2 function app here, and we can see if we go to our functions, we can see here this one, and we can look at the monitor for it. Well actually we don't need the insights monitor. We can actually this, we didn't configure application insights so we can just go to the activity log and it should tell us if anything is happening.
So we're not seeing anything yet, but let's check Cosmos DB. Let's see if there's any activity there. So we will go ahead and reload that and give it a moment. Close this, oh sorry one sec. Let's go back to that Cosmos DB and we'll see if we have any messages coming through. Oh, I hope this works all right, we're connecting to Cosmos DB and we'll go to our bethune test database container. We'll click on items, drum roll please. And there it is, there are all the messages that we sent.
So the messages are now in Cosmos DB, which means that the function executed properly. Now we're not seeing them in the activity logs here. It might take a minute or so for it to show up because we don't have any advanced insights configured just yet. Actually yes, we are seeing it now.
So if we look at the overview, we could see this spike in activity for the function app. This shows us that the function executed, and then we may see something similar in the Event Hub. If we look at throughput, we might we should see requested messages. Well, it might take a moment for that to propagate, but the most important thing is here are the actual messages are there.
So now we know that the code worked successfully. Our Cosmos DB, Event hub, and Azure function app is working as designed. So congrats if you're able to get this working on your own and thanks for playing along, cheers.
Jonathan Bethune is a senior technical consultant working with several companies including TopTal, BCG, and Instaclustr. He is an experienced devops specialist, data engineer, and software developer. Jonathan has spent years mastering the art of system automation with a variety of different cloud providers and tools. Before he became an engineer, Jonathan was a musician and teacher in New York City. Jonathan is based in Tokyo where he continues to work in technology and write for various publications in his free time.