Hey friends.
Azure Cosmos DB is a fully managed NoSQL database for modern app development.
The Azure Cosmos DB emulator and the API for MongoDB extension for Azure
Data Studio enable you to develop and test your application locally
without using your Azure subscription or incurring any costs.
Stefani Arroyo is here to show me how they work today on Azure Friday.
Hey friends, I'm Scott Hanselman and it's Azure Friday.
I'm here with Stefani and she's
going to teach me all about Azure Cosmos DB and the Cosmos DB emulator.
How are you?
I'm doing great.
How are you, Scott?
I'm very well.
I'm always excited to learn about new things.
And there's a Linux emulator for Cosmos DB
I did not know that.
Yes, it's great.
We have a lot of Linux developers and they've always wanted
for us to develop this emulator so that they can use their Linux.
Yes, Mac systems, all of that. Okay.
And this is like an emulator.
Like we're we're pretending to be the cloud, but it's all local.
Exactly.
It basically emulates Cosmos DB, the service.
And so I'll show you in a second all the different properties
that go about with using the Linux emulator.
So here's a doc of... this is basically the go-to guide
of where you can find all the details for how to run the Linux emulator locally
and then really important quick things
that you need to learn about is configuration options.
So this is one of the most common
questions are like, what can we configure on the emulator?
So there are things like ports where we have provisioned secure
ports for the emulator, things like partition counts.
How many partitions can that can we use on the emulator?
In this case, we have default ten, but usually sometimes to startup
or for demos we use like three or less just for getting up
quickly, memory, and then cores.
So we have our memory three GB or more is required
and then CPU's make sure to have at least two, but really
we recommend four cores for running the emulator
and then things like for MongoDB, we have different configuration options
and then if you want to enable your data to persist
after like emulator restarts, then you can set
this to True.
Yeah, I'll go into how to run this
locally.
I'll go to my handy dandy sublime too with all of the instructions.
Yes. So step one to get the emulator
running locally is you can set your IP address.
So I'll go ahead and copy this command.
This all of this, like I said, is in the doc,
so you'll just follow those steps.
So this is going to set your IP address into this variable called IP addr.
And then we go into the terminal and we can then run that configuration.
Next I'll go into how to the Docker run command.
Basically here, like I said, we've permission and ports 80, 81
and up to 10 to 55.
We have a memory set, which is what I just went over as well as partition count.
This case we're partition setting five partition and enable data persistence.
True.
And then we have the mongo endpoint using port.
One other thing I want to add
this emulator also support 4.2, which is the latest version for Cosmos
Mongo. And so yeah.
So once we copy this command, we can go into the terminal
and you can run this to basically pull the image.
So if you don't have the image locally, then by doing Docker, run it.
You can just it will download the image and then it will pull
it takes a little a few minutes to actually get the image to pull
just because it's a very large image that emulates the service.
But once that's downloaded, you can see we started with the five partitions
that we set and that's how you know the emulator is going to go.
Usually we do get a question regarding this evaluation version.
Why is it evaluation version?
Yeah.
Yeah, this is because this is so preview.
So the emulator, Linux emulator specifically
only supports sequel and Mongo API.
So we've just added this wording.
So if you're trying to use like Gremlin or other supported APIs
in production, this won't work for you.
I say okay.
And then probably in 90 days there'll be another version and I'll,
you know, I'll upgrade as well.
Exactly, exactly. Perfect.
So now that we have the emulator started, we have all of the partitions,
then we have to go to part two.
So interesting fact about the emulator is that it is
self signed certificates.
So we have to basically download this certificate
and then add it to our local certificate store for our Mac.
It's Keychain Access.
And then once you do that, you need to manually
trust that certificate to allow us to use it.
So that's for installing the certificate.
We need to open up new tab again, set the IP address
and then we can curl for the emulator dot.
Pam So to do that I'll open up a new tab
and we have the set, the IP address and then we can curl for the certificate.
I usually do a cat the emulator cert
that cert to make sure it's downloaded.
It takes oops 1/2
we curl that and then we can do.
I mean it helps if you use the right name here.
So let me just rewrite that.
Yeah.
Emulator or.
Emulator cert.
Happens to the best of us.
See you.
There.
You got so we have the emulator pulled and downloaded.
So what you're going to do next is, like I said, we have to add this
to our certificate store so we can go into keychain access for Mac.
And what you can do is go into file, import the items, and then it will ask you
to pull the certificate certificate that we just downloaded.
Once you do that, it will show up in your keychain as localhost.
So make sure to double click and then there's section under trust.
This is where we manually trust.
Select this as always, trust.
Usually it's set to never trust to make sure it's set to always trust.
And a really interesting thing, make sure you close this out after you do that.
I've made the mistake not closing it out.
And then I'm like, It doesn't actually work.
What's going on? It didn't update it, so make sure you close it out.
Usually asks you for your passwords and all of that.
So once you do that, we have the certificates
in our store and then we can actually run the emulator.
All right.
So you've trusted that self signed certificate now.
Exactly.
So now. Oops, sorry. Good.
No, I'm just I'm agreeing.
I'm trying to make sure I'm I'm following what you're saying.
So you you've got that South Side certificate, you called it.
You got that certificate from the emulator,
and now you trusted it locally.
So then you won't get those SSL, those SSL errors with a non trusted certificate.
Exactly.
So next what we'll do is now that we have the
emulator up
and running, we can go into localhost port 80, 81
and you should be able to see the Cosmos DB emulator here.
They give you options of like what my primary connection
string is for SQL API and then for Mongo API.
You can also see your connection string here.
There is also a explorer data explorer.
This is mimicking what we see in the Azure portal
and you can create new containers, you can
partition your container, etc.
So this is really nice experience.
You can start with a sample and you can use a bunch of different options here.
You can clear your data clearly complete.
Like I know it's obvious you were on local hosts 80, 81,
but we want whenever I see this, I think to myself,
like I'm on an airplane, I'm, you know, I'm doing this work completely offline
and I've got a really nice experience, totally offline.
And I haven't logged into anything.
I'm not using my Azure subscription right now.
Yeah, exactly.
And and then so for the Mongo API, so if you notice here we have sequel API.
This view actually only is supported on SQL API.
So what we'll do now is connect to a mongo accounts
that you can share your data and then I'll show you a visual tool
on how to view and query your data using Mongo Shell.
So yeah, next step is we can take this connection
string for Mongo API and put it into our spline text.
Here.
I have a couple of modifications that you need to do basically
and this is the
connection string that we've just copied and pasted from the portal.
And what we need to do is just set a few modification lines.
Number one is get your local IP address and set it into this connection string.
And then we just want to add a couple of different parameters
in this case, we want to add a retry rights equals false
and then TLS allow invalid certificates equals true.
So once you do that, you can then copy and paste this
connection
string and run any sample application.
So I have here a python application sample python application.
This you can find in our docs on how to quickly get started with Cosmos
DB and Mongo.
This application to extend your connection string
and this is where you would put in your connections that we've just added.
Typically we don't recommend adding the connection
string directly there, but just for the sample, I have it there
and then this application does delete read
update inserts creates a bunch of the basics.
In this case I wanted to just highlight you can also add those options here.
Tell us to until last
allow invalid certificates directly when you initialize the client
and you don't have to add it in the connection.
But we just did it there
so yeah.
So this is my python application.
I've added my connection string.
So now I want to just run this
python.
So when I run this I see that I've inserted one doc into my emulator.
So it's connected and B and I've inserted one document.
Now if you're wondering how can I like better visualize this data, how can I
modify it or
play around with what type of crazy I want?
So if you want to visualize this data,
we actually have a new extension with Azure Data Studio where
if you go into the portal, we have
this is our Azure Cosmos DB Mongo account.
We have a handy dandy
button where you can connect with ABS IDs is a desktop tool.
It's also a free tool that you can use to query your data and visualize your data.
So if you don't have Azure data installed, you can select download our data studio.
It downloads all of the necessary executable files with for you,
or you can just select connect account with Azure data Studio
and then opens up with this account in scope.
One interesting fact is that Azure Data Studio is actually a fork
of Visual Studio code.
So when we open it up in a second, you'll see the familiar like Visual Studio
layout.
So if we go
into the original extension, this looks very familiar
to what you would see in a Visual Studio code.
And a couple things to note is that
this is actually a really, really nice way to organize all of your connections.
So when you create a new connection,
you can then select Mongo account
and you can just simply select connection, string and add in your connection
string the same one that we just created, you copy and paste it there.
And then you can create server groups, which is super fun.
So if you have a lot of different like if you have different
clients or different projects, you can just create one,
one server group per project and it keeps it all organized for you.
So I love doing this with all the color coding, really convenient
and in
this case I've already created one for Linux emulator.
But if you double click on this, you should see this section here
where we have created options to create a new database
pairing with Mongo Shell documentation and really handy links.
In this case, I just want to go into my database overview.
So these are all the databases that are in my Linux emulator.
And if you select this Linux emulator database, it will take you
to all the collections that you've created and the number of documents.
So once you select a sample collection,
it will open up this terminal
that is actually running Mongo Shell locally on your computer.
So convenience because most Mongo users do
usually use mongo shell and then you can just run your
other mongo queries.
So for example, we want to do DBA DOT sample collection,
the name of my collection,
and we want to do like a find operation which is like a select start.
If you run this, you should be able to it's going to take a little bit of time.
But these are all of the objects and sample fields that I have here.
So it's really convenient way for you to play around with your queries
and test them out in this particular view and.
Sort of make sure that I understand.
So you happen to be on a mac, you could be on Linux,
you're running the Cosmos DB emulator in a container works great
you went Docker run we saw you do that little little IP address dance there
but it's it's a A in emulator of Cosmos DB
that means that it can do all development locally and I know it's going to work.
I have confidence it's going to work when I decide to go
and point that connection string to Cosmos to be in the cloud
and Cosmos DB itself, as we know, emulates mongo it.
I mean, it's scrambling to emulate other things.
So the Mongo shell doesn't even know that you're talking to Cosmos,
whether it be in the cloud or locally on the emulator, the elevator.
Is that good? Exactly.
Exactly. This is exactly right.
And this is why it's so convenient to use the emulator for local
development testing.
And a lot of our developers use it and they absolutely love this before
for testing applications.
And last thing I wanted to show you is once you're done using Linux
emulator and let's say you want to start using Cosmos be in production,
you can still use the same tool to get you started.
I have my Cosmos DB Mongo connection already here.
You would do the similar thing.
You would go into the portal, get your connection, string,
create your separate groups and names and create that connection.
And then once you have that, this is my Cosmos Mongo account
and you can see
all the similar details here, so you can still play around
similarly, the same way that you would do with the Linux emulator.
So convenient tool.
Very cool.
And other than the little dance with self scientific gates, which I think
all technologies have to deal with, this is a five
or ten minute operation and folks can be productive right away.
Exactly. Exactly.
A super easy, super fast. Very cool.
And can you remind me where I would go for the docs to learn more about this?
Yeah. So we have all of the docs
here in our
k links where you can set up the instructions
on how to use Docker for Linux on mongo Cosmos DB.
And then we also have had a quick start on how to get started with a.A Studio.
Very cool.
Well, I am very happy to learn about this.
I'm learning all about these desktop tools and these great emulators for developing
with Azure Cosmos.
Do you be on any platform for any developer today on Azure Friday?
Hey, thanks for watching this episode of Azure Friday.
Now I need you to like it, comment on it, tell your friends retweeted watch
more Azure Friday.