In this lesson, we are going to look at mounting the other containers that we have.
In the last lesson, we mounted our demo container.
But as you know, we have three more containers that are required for our project, which are raw,
processed and presentation.
Let's look at mounting them.
We can simply copy the command here that we use to mount our demo container and replace that with a
raw, processed and presentation.
But we don't want to do that because that's a lot of duplication.
And every time we mount a container we copy the same code and make the corresponding changes.
In a large project we will have a lot of storage accounts and containers.
So instead of hard coding everything, we could simply create a python function and call it with a parameter
such as storage account name and the container.
If you're new to Python functions in Python are just a set of reusable code which gets executed when
you call that function.
Also, you can send parameters to a function and it can also return results back to you.
So let's create a function to which we can pass in the storage account name and the container name,
and it mounts that for us.
Instead of working on this notebook, I'm going to create a new notebook.
So that's separate for you when you come to look at it.
And I'm going to call this one as eight mount containers for the project.
So let's start a new cell here in Python.
A function is defined using the def keyword, and then you need to provide a name for the function.
I'm going to call this as Mount ADLs.
Then you provide the parameters inside brackets.
I'm going to have two parameters as a set and I'm going to call them as storage account name and container
name.
And one more important thing is we need to end the line with a colon and you hit enter.
And this is where you start to write your body of the function.
And that's what gets executed every time you call that function.
Let's start with the secrets from the key vault, which will be the client ID, tenant ID and the client
secret that will go as the first statement here.
Please make sure that you've indented your statements here because otherwise that won't be treated as
part of the function within Python.
So I'm going to simply add a little comment as well.
And the next thing we want to do is to set the configurations.
Again, I'm going to simply copy what we got here and just paste it there.
And the last thing we want here is to mount our storage, which is using the Databricks file system
utility.
I'm going to intend this again.
And as you can see here, we've got the hardcoded values for the container and the storage account.
So we'll have to change that because for this function we are going to send them as parameters so we
can use those values here.
So storage account name will contain the storage account name.
So instead of having the hardcoded value, let's replace that with the name of the variable, which
is the storage account name within curly brackets.
And we're going to use the F string interpolation here.
So prefix that with the F as well.
And similarly for the container name, we're going to use the container name variable instead of demo.
And again, I'm putting that within curly brackets.
We have to do the same thing for the mount point.
So let's prefix the value here with the letter F and instead of Formula One, let's use the storage
account name variable.
And for the demo we are going to use the container name variable and we enclose both of them within
curly brackets as well.
So that's everything we need to do in order to have the function.
But you can also add other things within the function so that you don't have to repeat anything later
on.
So for example, if I wanted to also have the list of amounts so that I can see as soon as it's mounted,
my amount is being listed as well.
So let me quickly copy this statement here and put that into the function as well.
So that's brilliant.
That's done as well.
So let's attach our notebook to the cluster and we can run that now.
So let me quickly execute this function.
When you execute a function, it just creates the function.
It doesn't do anything else.
You will have to invoke the function with the parameter values for it to do something for you.
So let's say we want to mount our raw container first.
In order to do that, you just have to call the function mount underscore adls.
And you need to pass in the value of the storage account name.
In my case, it is Formula One and you need to pass in the value of the container name, which is a
raw in this case.
So let me execute that.
And as you can see, it's now mounted the raw container as well.
So we got the mount here, slash slash Formula one slash raw, and it's being mounted to the storage
account location here, which is brilliant.
So when we want to do it for the presentation or the process layer, you would simply call the function
with the value as presentation or processed.
You can even go a step further if you're keen on getting 100% production ready.
The one problem at the moment with this function is that if you rerun this one, it will fail because
the mount is already been mounted.
So we'll have to unmount it in order for it to be mounted again.
In order to do that, you can check to see whether a mount exists and then unmount that mount before
trying to mount it.
So this statement here would do that for you.
So let me just intend it right first.
So all it's doing is it's getting the list of mounds here into a mount variable and then it's going
through the mount points and checking to see if one of them is the one you're trying to create.
In our case, that will be slash MBA, slash Formula one slash arrow.
And if that is the case, it will simply go and unmount it and then it will do the mount for you.
If the mount doesn't exist, it will simply ignore that step here and just go and mount it.
So that is a bit more robust.
It doesn't fail when you already have a mount.
Instead it will just unmount that and then mount it back again.
So let's execute this function now.
And if you rerun this statement here to mount the raw container, it will simply unmount the mount,
which is already there and then mount it back again for you.
As you can see, it's done.
The unmount here and then it's listed all the mounts here for you and the raw mount is still there because
it's been mounted.
So let me quickly add a comment here as well.
So let's now mount our presentation and the process to containers as well here.
So I'm simply going to copy and paste that statement and change the value from raw to processed.
And also I'm going to have another cell and that's going to have the presentation container.
As you can see, we've successfully mounted all three containers for the project.
So this is exactly what you would do if you are in a production project.
One person in the project will write the function with all the capabilities that we need and everybody
else will just invoke the function.
So that's brilliant.
So let's quickly delete all the other statements that we don't need in this notebook.
I hope you now have a good understanding about how to mount a data lake storage container to your database
and also get that production ready.
That's the end of this lesson.
I'll see you in the next one.