Data Exploration & Experimentation with Notebooks in Azure
Microsoft invests in Notebook tech like Jupyter & .NET Interactive for data analysis, adding .NET support for Spark and Azure's Databricks integration.
About this talk
SQLBits 2020
Microsoft are investing heavily in Notebook technologies (such as Jupyter and .NET Interactive) to provide an interactive environment for experimental and exploratory data inspection and analysis.
These kinds of environment are becoming increasingly important for a growing range of activities including data cleaning and normalization, data import, statistical analysis, insight generation, and testing hypotheses. And in some applications, it can make sense to take notebooks that started out as an interactively developed set of ad hoc operations and transform them into part of an automated workflow.
Jupyter notebooks are most commonly authored in Python or R. Microsoft has been working to add support for .NET languages, enabling the use of C# and F# in notebooks. They are also adding .NET support for Spark, enabling Spark clusters to be controlled from .NET, and also with a view to being able to run custom .NET code inside the cluster as part of the core processing.
Azure's growing support for notebooks enables this approach across a range of scales. You can work with datasets that fit easily in a single machine's memory, but if you need more firepower, with Azure's Databricks support you can spin you up a server farm to process your data in parallel, enabling you to perform complex computations across massive datasets.
Read the transcript
Introduction
Hi, I'm Ian Griffiths. I'm a Microsoft MVP and technical fellow at a UK consultancy called endjin. We're Microsoft Gold Partners for Cloud Platform, Data Analytics, Data Platform, DevOps and Power BI. This talk is about data exploration and experimentation with notebooks in Azure.
What is a notebook?
So to start with, I'm just going to look at the what and why of notebooks. Some of you will already be familiar with this, but for those of you who aren't, I think it's well worth taking a look at exactly what we mean by a notebook experience and why it might be interesting. So what exactly is a notebook? It's a very simple thing. It is a mixture of some text, some code and potentially some graphics, some visualizations.
So why is that interesting? Well, the point of notebooks, the thing that makes them important, is that they democratize access to data and the understanding of data. The successful analysis of data is at the heart of success for a lot of enterprises these days. We have more and more data in the world and being able to make sense of it is absolutely critical. And my view is that notebooks are as important today as spreadsheets were back in the early history of the personal computer revolution.
They both do the same thing. They unlock the potential of the technology that we have, and when they put it into the hands of a much wider range of people than ever had this before. So it enables you to bring to bear the power of modern computing platforms, and in particular cloud platforms and the huge scale at which they can operate, without necessarily needing to be a systems development expert. You can just be someone with reasonable technical skills operating a computer and you can do incredibly powerful things that would once have required a team of programmers. It's all about putting the power in the hands of the people who want to understand the data that they've got.
Notebooks in a real engagement
So this might look like a stock image carefully chosen from a library to represent what we're talking about. However, this is actually a photograph from my company. So I work for endjin, a consultancy firm, and this is us using a notebook, and this wasn't posed despite how it might look, although you can tell we're not models. So this is from an engagement we had with a client where we wanted to show them how much was in their data, to show them the potential that could be unlocked if they brought the right tools to their job.
So the people we've got at the table here are a domain expert over on the left, and you've also got the CTO of the company actually, and a developer on the right, and also one of our principals there on the far right. And what we're doing here is working together to see what we can find out by looking in the data that they've got. So we sat around a notebook just trying things out. You know, what data do we have? Okay, what happens if we try and visualize it this way? Oh, what if we just slice it that way?
And there was a very, very, very productive back and forth between the domain expert, who understood what the business needs were and what sorts of information might be in there and what kinds of insights would be valuable to get out of there, combining that with the development abilities of the technical people. It produced very, very quickly a great deal of extra visibility into information that they had. And actually this was such an epiphany for the CTO in this particular organization that he actually then chose to do a masters in data science, more or less straight after this, as soon as he could. He just found this such a powerful and helpful experience that he wanted to know more, wanted to get better at this stuff.
So this is powerful stuff. Notebooks are very important. They are really a useful tool in today's data environments.
Notebook example
So let's take a look at a real one. So to start with, I'm just going to show you a very simple notebook file. So I've got Visual Studio Code running here and I've got the Python extensions installed, which enables it to operate directly with notebooks. So if I open this file here with this .ipynb extension, that's the standard extension for a certain kind of notebook, and it opens the file. Let me just close that out the way so you can see the whole thing.
And you can see that, as I said, this is a mixture of text and code. This text here, if I double click it, you can see this is just Markdown, and it provides some information in this case about the file in question. So the example I've got here is your classic armchair epidemiologist. So maybe you've been following the news about coronavirus infections and want to know whether something that a politician said was quite right. And what you can do is you can just download the datasets produced by the government bodies. I've downloaded something from the UK's Office for National Statistics here, and I'm using a notebook just to dig around in it.
So I'm just going to run the whole thing, because you can see there's some code here. So this is Python code, and just to go straight to the punchline, I'm going to run the whole thing. So if I click this run all cells thing, you can see this little star start to appear. It's actually running the code, and if I scroll down to the bottom you can see that it has printed out some of the data.
So what I've done here, to go into a bit more detail now you can see where it's going, is I've pointed this at a path on my local file system where I've downloaded the data from the Office for National Statistics. They happen to provide it as an Excel spreadsheet, and that's fine because this library that I'm using in Python called pandas is able to read Excel files directly. I just open it up, say I'd like to open this particular sheet from the spreadsheet, I tell it where the headers are and so on. I'm renaming some of the columns because the column headings are slightly odd in the document, but basically reading the data and then plotting some of it.
So there's actually two different data series here, one of which is the estimate, that's the number of people in the UK testing positive, and the other of which is the estimated incidence of the disease, that's the number of new cases. So so far this is nothing that I couldn't equally have done in Excel, but because this is code I could, if I want to, start executing things like this average function, the geometric mean. So the data frame library, the pandas data frame library that I'm using, is able to run aggregate statistics over data. So I just said please calculate the mean across all the columns you've got here, and it has generated that data for me.
And this is all just code. I can do anything I like in here. So whatever Python libraries I can find, I can use them here. I've just used pandas, which is a very widely used choice for working with tabular data, but there are other options, and people do things like machine learning or image recognition, anything you can find a library for in your programming language of choice. Pandas here, could be C#, could be R, could be Scala. Anything you can find a library for, you can use inside a notebook.
By the way, the notebook experience that VS Code offers lets me poke around with things. If I click on this button here, I can take a look at all the variables that have been created. So here are the two data frame objects that I've used to load data out of the Excel spreadsheet. So if I click on this, I can actually see the raw data as it appears inside that, and this is what I'm operating on when I invoke those methods.
So like I said, it's just optional text for documentation, typically a Markdown format, and then little snippets of code which can then be executed. And if you want to, you can produce the output as graphs, or perhaps you can produce the output by executing functions that show you textual output.
Inside the notebook file format
And by the way, I want to show you what's actually in one of these files. So this is the display that Visual Studio Code has offered for this, but what does it look like if we actually just poke around inside this file? If you try and open it in source code mode, Visual Studio Code just reverts back to this notebook viewer, so I'm going to do this from the command line. If I just show the contents of this file, let me scroll back up there, you can see that it's just a JSON file.
So we have JSON, some information about which version of the file format this is, and then just a collection of cells. This is a Markdown cell, that's the text you saw at the top there. Then there's a code cell, this is where I import the libraries. Another code cell, this is where I load the data, and another code cell. So this is where it all goes. So you can just see it's a very simple format. It is just a bunch of cells expressed as JSON, and then we have a viewer app, in this case Visual Studio Code, but it could be something else, which shows the contents.
So the point I really just want to reiterate here is it's all just code in there. You can do anything you like. As long as you can find a way of doing it in code, you can do it inside a notebook. So that example I showed was deliberately very simple, because I'm just trying to show you the basics of how notebooks work there, but now we're going to go a bit further.
So lots of examples exist out there. There are examples of people using notebooks to automate the recoloring of photograph libraries, people applying machine learning libraries to automatically detect features. We've applied anomaly detection algorithms to work out when things have gone wrong in systems automatically, and to visualize that through notebooks, and then to operationalize the discoveries so we can respond to events going wrong. All sorts of things can be done here, but the key to notebooks is the exploratory collaborative nature, is the fact that people can work together. You can get domain experts and technical people together to extract the value that's inherent in the data.
Notebook architecture
But you might be thinking, well, isn't this supposed to be a talk about Azure? And so far I've shown you opening an Excel spreadsheet on my desktop. And yes, this is true. So now we're going to look in a little bit more detail at the architecture of how these things work, and then see how a cloud platform like Azure can fit in.
So what I just showed you was a fully local notebook. So there's various variations on this theme. I happen to open it in Visual Studio Code. Another popular approach is to actually use a web browser talking to some sort of local server. So there is a project called Jupyter that defines a lot of the technology behind the particular form of notebook I was using. It's not the only game in town, but most of what I'll show you today will be Jupyter notebooks, although not exclusively.
So Jupyter is an open source project that includes a viewer application that runs essentially as a web browser, that can either be up in the cloud or on your local machine. And in the fully local approach you've got files in your local hard disk. You've got the notebook itself, which was that JSON file I just showed you, and your data is local as well.
Kernels and multi-language notebooks
Now a critical thing to understand about how notebook technologies work is that all the widely used ones are multi-language. So the Jupyter notebook system, for example, the viewer app itself knows nothing about Python. It's not language specific. The name Jupyter, by the way, comes from several different languages. The Ju bit is for Julia, that's a programming language. The Py part is Python, and the R on the end is the R programming language. It's always been multi-language from the start.
And the way that this works is that when you open a notebook it connects to something that it calls a kernel, and this is the thing that knows how to interpret the code. And there are about 40 or so different kernels out there available for the Jupyter system alone. So I was just using the Python one. There's also a .NET one that lets you use C# or F#. There's also an R one, there's a Scala one, there's a Julia one. There are loads and loads of different ones out there.
So the language that your notebook says it's using determines which kernel the Jupyter viewer app is going to open, and then it's in that kernel process that your code runs, and that's the thing that's actually going to read the data itself. So this is a slight variation of what I just showed you in the demo. This is a fully local setup, everything's running locally on my machine, but it doesn't have to be this way.
Moving the data into the cloud
The next increment away from this would be to move the data somewhere else. So maybe you're happy with the local setup, maybe you've got all the libraries you want installed, you know how to use the local viewers and all that sort of stuff. But maybe for one reason or another your data doesn't really belong on your local hard drive. Maybe there's an enormous quantity of data and you're only going to inspect some subset of it, and you don't want to be downloading a terabyte data set onto your laptop. Or maybe you're collaborating with other people, maybe lots of different people all need access to the same data. There's lots of reasons you might not want to have the data stored locally.
So one variation on this theme is to push the data into the cloud. So in this picture I've shown it in an Azure Data Lake. It could be a blob store, it could be a SQL Server, it could be anything. There's a number of different options we'll look at in a moment. But everything else remains the same. In this case I've still got the notebook stored locally, I've still got the viewer app stored locally, even the Python code itself is still running locally in this setup. And that's absolutely a valid and useful mode of operation. A lot of people work this way, local execution of notebooks to communicate to data stored in the cloud.
Adding a Spark cluster
But it's not the only option. Here's a different form of use of notebooks. I've still got the notebook file stored locally on my computer, I'm still running the viewer app locally, I'm still running the Python kernel locally, and I've got the data up in some storage system in the cloud, Azure Data Lake again in this case. But notice the addition of a Spark cluster. This is a popular way of using notebooks if you are dealing with very large quantities of data. If you're dealing with hundreds of gigabytes or terabytes of data, then you might not necessarily want to try and process all of that with a single Python program running on your laptop. That might not scale very well.
So one thing people do is they set up Spark clusters. Now Spark, Apache Spark, is an open source project for doing distributed large-scale processing of data, and so you can spin up clusters with large numbers of machines in them. The smallest ones have got three, but you can have tens or even hundreds if you want in a cluster, and these are able to execute queries over large data sets in parallel. So you can take advantage of the way that modern cloud platforms make it really easy to spin up very large numbers of computers in a compute farm, do some work and then shut them down again. One of the nice things about the cloud is that you don't have to actually own all the servers that are running. You can just rent them for a few hours, or however long it is you need to run your processes, and then shut them down again.
And this is a very common notebook type scenario, and in this case the notebook is essentially acting as an orchestrator. So you've got the notebook running locally, the Python code running locally, or it could be C#, whatever, code running locally, but it's communicating with a Spark cluster which would be running in the cloud. And there's various options for doing that in Azure, as we'll see later. And one of the advantages of this is that Spark gives you this massive parallel processing ability, but another advantage is it just means you can put it near the data.
If you're storing your data in Azure, then it might make sense to process the data in Azure as well, to have the actual computations that perform your work done in the same basic geographic region, possibly the same data center, certainly nearby. That makes more sense than trying to get all the data down a domestic broadband connection if we're all working from home these days. So one of the benefits of this model here is simply just co-location of compute and data.
An all-cloud setup
And actually there's another variation on this in which the Python kernel itself moves into the cloud. So this shows kind of an all cloud setup. The only thing that's running locally now is my web browser. Everything else is running up in the cloud. So I've got an Azure Data Lake. I'm also using cloud storage of some kind to store the notebook. Might be Data Lake as well, it could be blob storage, as we're going to see there's loads of options here. And something is actually hosting the notebook application itself in the cloud, so the only local piece is the web browser that's looking at it all, and even the kernel itself runs in the cloud.
Now in this diagram I've shown a Spark cluster, but actually you might not need one. You might say, well, it's enough to have the kernel that's executing the code in the cells in my notebook to be running up in the data center, because there it's got the quick access, direct access and high bandwidth, low latency connection to my data store, and that's actually good enough. I don't necessarily need a Spark cluster. For some jobs you would. If you're processing multiple terabytes of data, then something like a Spark cluster might well be worthwhile, but if you're just dealing with, you know, gigabytes of data, then maybe that's overkill. Maybe just having the Python kernel, or whichever kernel you're using, nearby to your data store is good enough.
So the advantage of moving everything into the cloud, including all the compute, is that you get better performance and potentially better scalability. So that's all general purpose discussion. Everything I've just said there would apply whether you're running on Azure or AWS or any sort of distributed computing platform.
What Azure brings to notebooks
So what about Azure in particular? Where does Azure bring benefits to bear on use of notebooks? Well, there's three places. Azure offers numerous storage services. It offers loads of ways of buying compute, so basically the ability to run code on a CPU for a bit. And it offers various options for providing a notebook viewer experience for you.
Azure storage options
So let's start by looking at Azure storage. Now Azure storage is a huge set of options, and actually I've got another talk on this. If you go and find my other talk, it's about how to navigate the bewildering array of options that Azure offers by way of storage. But broadly speaking, if you're looking at notebooks, then we can break it down into three quite large categories.
One is blob like things, so things that are essentially just files of one kind or another. So there's variations on this theme. You've got actual file shares that expose things through a file share protocol, so you can actually mount them as though they were a file system. But you've also got more basic blob storage, which is just an HTTP based facility that enables you to store large binary files and to retrieve them again later. And there's also something called Data Lake, which really used to be a separate service in Azure, but now really it's some features you can enable on blob storage that give you hierarchical data structuring and the ability to apply different security rules to different levels of your data structure. But essentially it's all just storing blobs at the end of the day.
So this is actually very widely used in notebook scenarios, because quite often the data you will have might be Excel spreadsheets, as an example I just showed you, or it might be CSV files, comma separated value files, they're very widely used in data analysis, or it might be JSON files. And in all these cases you've got a bunch of files and you need to be able to process them, and so blob type storage is perfect for that, and that's very well supported in Azure. There's loads of options there. We'll look at an example of this in a minute.
But there are other options. It might be that for one reason or another you've got your data in a relational database, and Azure offers several different ways of doing that. There's Azure SQL, which is basically the hosted version of Microsoft's SQL Server running as a platform as a service offering up in the cloud. But they also support other systems, so they provide hosted MySQL or PostgreSQL, so those are options as well.
And another alternative is other kinds of structured storage. So it's database technologies that are not old-school relational databases, but which are a bit more structured than just plain file storage. So you have things like Cosmos DB, which you can upload JSON documents into, and it's able to dynamically infer the schema with these things and index over them. So you get a degree of the sorts of things you'd be used to with a relational database, but with the freedom that you get with just a plain JSON document store.
There are key value stores available. Actually Cosmos DB has a mode for working with that as well. There are table stores, so Azure storage offers a very simple but high volume and high bandwidth table storage mechanism. Actually Cosmos DB offers that as well. And there's Redis Cache. There's lots of ways you can store this, so there's various kinds of semi-structured storage options available in Azure.
Demo: reading Azure Blob Storage from a local notebook
So what I'm going to show you now is use of Azure Blob Storage. So this is just the first piece where we're going to move the data off the local computer and onto Azure storage, but everything else is going to remain local. So as with the previous example, I'm still using Visual Studio Code here. I've just opened up a Python based notebook on my local hard drive, but the difference this time is I am going to use the Azure Blob Storage client libraries.
So I've just got this first line here instructing the Python package manager to install this particular package. This is provided by Microsoft, it's the way to read blob storage in Python. And we've also imported pandas as before, this is the data frame based thing. I'm loading some credentials off disk so I can connect to the storage account without showing you what my password is. And we're just using the BlobServiceClient class from the Azure Blob Storage SDK. So I've got some code here which is going to iterate through all the blobs that are available.
And just to give you some context for this, the purpose of this notebook is part of a performance investigation that we were doing at endjin. We wanted to get a better understanding of some performance characteristics of an Azure function, and so we were using some diagnostic tools to export a bunch of timing telemetry into Azure storage, and then we were using this notebook to try and extract that and start asking it questions. So all I have to do is just use the library that's designed to enable Python to read this stuff, and then to connect to Azure storage with that and to iterate through the blobs.
And this is going to go through each of the blobs that it finds in this storage account and just download them, store them locally on disk as a temporary copy, and then read them in as JSON, and then I'm actually loading that into a pandas data frame. So I'm using the same pandas library to process my data here. I'm just reading data out of files downloaded from blob storage and then adding them into that data frame.
So if I run this, I don't need to re-run the install because I've already done that install step, that's already on my system. Let me just run these steps one after another. So if I run this one, oops, missed, here we go. You can see it's actually doing the download right now, so you can see the scroll bars occurring. This is it fetching blobs from Azure storage. And is it done yet? Nope, that's still running. So that is connecting to Azure storage, downloading lots of blobs and loading them into a data frame in memory.
Now this does actually illustrate one of the reasons why you might not necessarily want to do things this way around. There were quite a lot of blobs here, and it's taking a while to process these because they're all coming from Azure storage down through my domestic broadband connection home. So it does take quite a while for this to actually operate. So this explains why you don't necessarily want to do things this way with large volumes of data, but it illustrates that it's all just code. So if it makes sense architecturally, there's absolutely nothing stopping you from using the client SDK to download blobs and then work with them locally, just like you would any other data.
Hosting the notebook compute in the cloud
So what we've just seen highlights the need to think about where you want to locate the computation when you're using notebooks. Often running the notebook locally will be just fine, whether you're working with local copies of data sets or data up in the cloud. But if you're dealing with large volumes of data, or maybe even just large numbers of individual files with the data scattered across them, then the approach of running the kernel locally might not be such a good idea. So now we're going to look at some of the options that Azure offers for hosting the compute aspect of the notebook methodology in the cloud.
So there's several reasons you might want to do this, one of which might be simply to locate the execution of the code close to where the data is stored. That's exactly what I would want to do with the example I just showed you. It was taking forever to process through all the blobs in blob storage, just because of latency really. It's not even that there was a particularly high volume of data there, but the nature of the data generated was that it was scattered across lots of individual files, just because that's how the tooling we'll be using happened to create it. And so the ability to be close to the data store to minimize latency is going to make quite a big difference in how quickly I can process the data.
But there's other reasons. It might be that for certain kinds of applications you just need to throw a lot of compute power at it. If you're processing terabytes of data, then doing it on a single process is going to be slow in all likelihood, and so you might then want to bring technologies such as Apache Spark to bear, to give you a way of improving the throughput that you can achieve, so you can process larger volumes of data in a given length of time.
Or it might simply be that you just don't want to have to install all the necessary pieces on your machine. That's actually a perfectly valid reason for not wanting to use the local approach. If you can just have the notebook hosted in the cloud and have it run there, you don't need to install Python, you don't need to install the kernels that you need to use, you don't need to install the bits and pieces to get this running locally. You just open the web browser, go to the notebook and there it is. So for all these reasons you might want to use compute in the cloud rather than running your notebook locally.
Azure Notebooks and the alternatives
And because there are lots of different reasons, Azure offers several different options, and we're going to look at a few of these. So if you go to notebooks.azure.com, you might have heard of this, this is the obvious place to look. Unfortunately it's a service that's shutting down. It was done in preview, and it did provide a fully hosted notebook experience. However, what Microsoft realized is people want to host notebooks for lots of different reasons, and so one size fits all solution wasn't really the right way to go, and it points you to lots of other options.
So one of the options is something called Visual Studio Codespaces, and this is essentially a bolt-on to Visual Studio Code that provides compute in the cloud for situations where you need it. And one of the things that enables is a hosted experience for opening a notebook, and I will show you that in a moment. So that's sort of the most straightforward path for moving from local to the cloud. As you'll see, it's almost indistinguishable from the local approach. It's just that everything happens to be running up on a container somewhere in a data center. So that's the first option.
We're also going to look at Azure Synapse Analytics. So Azure Synapse is a fairly new offering from Microsoft. It actually covers a wide range of different features, so SQL data warehouses moved in there, but there's also a feature called analytics workspaces, and these provide hosted Spark clusters and an interactive notebook environment. So this actually solves the how do I get my notebook up and running, and also how do I run my compute at scale. It provides a complete out of the box solution for both of those things, so we'll take a look at that as well.
Also Microsoft offers a hosted Databricks service. So Azure Databricks is essentially a productized version of Apache Spark. So Databricks is more focused and more narrow than Synapse Analytics. Synapse Analytics does all sorts of things. It's got notebooks, Spark, but also SQL on-demand, SQL data warehouse and various other things all under one roof. If you just want to run a pure Spark based approach, then Databricks is that. So that's another option. It has its own notebook model, so Databricks also supports notebooks. They're not actually Jupyter notebooks, although they look very, very similar. It's a very similar idea but a different implementation.
Also Microsoft's Cosmos DB service, another Azure feature, has a built-in notebook facility, so you can just run notebooks directly inside of a Cosmos DB database. There's also Azure Machine Learning Studio. This is a much more specialized piece of functionality. Machine learning obviously is one possible application of notebooks, and Microsoft actually provides an environment, an interactive environment for doing machine learning tasks, which includes hosted notebooks.
And another option they suggest is just use virtual machines. So if you don't want to maintain all the local Python bits and pieces, or whichever language you're using, on your own machine, well, you could still have a virtual machine up in the cloud. And actually Microsoft offers a pre-configured data science virtual machine that has all the bits and pieces that you need pre-installed on it to do data science tasks. So that's another option. And they also suggest using Azure Lab Services as a means of making that available to a larger number of people. So that's a way you can push out the same basic VM image to a wide range of people. So if you've got a team that needs to collaborate, then that might be a solution as well.
Demo: Visual Studio Codespaces
So we've got all these options, so let's take a look at the first one of these, Visual Studio Codespaces. So I've got a GitHub repository here which contains a copy of the same notebook we were looking at earlier. So this is the one that's going to connect to Azure Blob Storage and retrieve some information produced by running some performance tools. So GitHub has a viewing experience for notebooks, but right now it can't run the things. However, Microsoft offers this service called Visual Studio Codespaces, and this is essentially online compute to support working in repositories in ways that actually require you to be able to run code as well, such as the ability to run a notebook.
So I've got a codespace set up here. You attach them to a repo. This is connected to the repository we were just looking at, and as you can see this is going to have a Linux box backing it. So if I just say I would like to connect to this, then the browser opens up the codespace, and what I'm actually going to see is something that looks pretty much just like Visual Studio Code does when it's running locally. So this is a browser-based version of VS Code. And if I open up that Python notebook, well, initially it shows the JSON, but that's very quickly replaced by the Python extension leaping into life, and this looks pretty much like it did when I ran locally.
So it's the same thing as before. I'm going to install the blob storage API, I'm going to bring in some namespace, some libraries, and just start running through this. So if I just run this, then you can see it starts to run. Now actually I've run this before in this codespace, so the storage library is already present, and it's already blasted through all of this and has got as far as the code that's actually downloading the blobs. And you can see that's already finished fetching all those ones to start with, which is noticeably quicker than it managed it when running locally, which is exactly what you'd expect, because the Python kernel is now running up in a container somewhere in Azure.
So you can see I've got a bash prompt here, so one of these processes is going to be the kernel that's actually running my code. I'm not going to dig into which, but basically this is how it all works. So this gives us some of the benefits of co-location. Now there's actually quite a lot of data in this particular example data set, so in this case it's not giving us instantaneous performance, because the second set of messages I'm churning through is actually quite a lot bigger, so maybe we need to do more.
But in cases where just being co-located with your data, having the Python kernel running up in the Azure data center and having your files stored up in Azure as well, this can be a pretty good way of doing things, because it's a very, very similar experience to what you get when you're running Visual Studio Code locally. It's just that the kernel is now running in the cloud rather than running on your desktop.
Azure Synapse Analytics workspaces
So there I'd moved all of the compute into the cloud, but I'm still basically just using a single Python kernel to run all of the work. But what if I want to do something that can scale out a bit more? What if I want to be able to run my queries on a Spark cluster, so that I can process data at a higher throughput by exploiting a larger number of cores? Well, we're going to look now at one way of doing that, which is to use Azure Synapse workspaces.
So I've got the Azure portal open here, and you can see here various resources. That's actually the Codespaces plan that backed up the compute for the previous demo, but down here we've got various things related to performance analysis using Azure Synapse, and that's what I'm going to show you. But before we look at Synapse itself, I just want to show you the actual data we're going to be looking at.
The Application Insights data in the data lake
So I've got a storage account here. So this is an Azure storage account configured with the Data Lake features switched on, and if we go into this I can look at the blob containers and look in there. And the way we're using this is that for each test run that we do when we're analyzing the performance of the system we're looking at here, we create a new container here. So the basic goal here is to try and understand how the performance characteristics of some services we're working on change as we adjust various aspects of the system, and for each different configuration we create a new container.
If I look inside one of these, you can see that within each container we have a folder for each of the service instances we spin up. So this actually had 10 services running, one folder for each, and if I look inside any of these you'll see a certain folder structure, and this is what Azure Application Insights produces for me. So with Application Insights, that's a monitoring and diagnostics system that Microsoft provides in Azure, and you can configure it to export all the data it collects into a blob store or a data lake like this.
And so it's broken it down by type of information, so we've got performance metrics, performance counters, requests. And if we look in here you can see it's broken down by date and then within that by hour. So this particular test spanned three hours on the clock, although this one started quite late towards the end of one hour, ran all the way through this middle hour, and there's some more at the end. And it spits out a variable number of blobs in here.
If we look at one of these, you can see that each blob is actually JSON. Some of them contain a single JSON object, some of these will actually contain more than one, and this contains all of the information that Application Insights has collected regarding a particular request in this case. So there's quite a lot of these, and sometimes there's more than one in the file, so they need a certain amount of massaging to get them into a uniform structure that we can start to process using the tools available to us in our notebook.
So I just want you to see that before we look at how the processing works, because it's this sort of structure that caused us the slowdown when trying to process the things with a single kernel. This is exactly the sort of data structure where it's useful to parallelize the processing. Now this is not big data. We're talking relatively small amounts of data, but even with this, just the number of files means that Spark actually has something to offer here. So Spark, Apache Spark, is a platform for doing parallelized data processing, and you can scale out across anything between three servers and, you know, as many as you care to spend on really.
Demo: a Spark notebook in Synapse
So let's take a look at that. We're going to use Apache Spark now through the Synapse workspace feature. So Azure Synapse is quite a broad set of features actually. It covers quite a lot of different bases. Several different things have been moved under this brand, but the particular bit we're going to look at is the analytics feature, which amongst other things gives us wrappers on top of Apache Spark.
So take a look at this. If I go to the settings of my workspace that I've created here, well, first of all let's look at the services we've got access to. So there's a connection to the SQL data warehouse. We're not going to use that in this particular demo, but there's also a connection to an Azure Data Lake Store, and this is the exact same storage account we were just looking at in the portal just a minute ago. So this is the default store that is being used by this workspace. You can attach as many different sources as you like, but the significance of the default one is it just works when you use it. You don't need to specify which store you want, you don't need to provide the credentials, that's all handled for you by the environment.
So we've got access to that data lake, and also there is an Apache Spark pool. So this is going to give me the compute capacity that I need, and you can configure different sized pools here. In this case, for this demo, I'm just using a small pool which has got three nodes, each with four CPUs and 32 gigabytes of memory. I could add larger pools, either with larger nodes in them, or more nodes, or a mixture of both. In this case, for the demo, I don't need anything particularly powerful, so this is going to do.
So if I now come over to this section here, I can see I've got some notebooks, and if I open up this one, I'm just going to kick this off while I talk, because this is going to start the Spark session for me, and actually my Spark cluster isn't running right now. But now let me talk about what you can see here. So this is a notebook. As with all the other ones we've looked at so far, it's a mixture of data and text, and it can contain graphics as well, with visualizations. So this is just Markdown once again. If I double click that, you can see the Markdown editor.
It's a slightly different user interface from the one we saw before. So this is not just a wrapper around the Jupyter notebook stuff, this is a UI specific to Azure Synapse, but it is essentially still basically a notebook style approach. Now with this, rather than having a Jupyter server and a straightforward kernel, everything runs in a Spark cluster. So this is going to be attached to the get started Spark cluster I just showed you, that's the small one with three nodes, each with four CPUs and 32 gigs of memory. You can say which particular cluster you'd like to run them. That's grayed out, by the way, because it's now starting the session. I've already told it that's the one to use, so until it's done starting this, that's going to remain grayed out.
So one of the nice things about analytics is it can start these things up on demand for you, and it will automatically shut them down as you stop using them, unless you tell it not to. You can tell it to leave the things running, but if you're doing ad hoc analysis and then you're desperate for lunch or go home and forget to shut the thing down, it doesn't cost you too much money, because it will auto close down once it's done.
And the cluster is going to be the place that hosts the Python kernel that's going to execute all the cells in this particular notebook, and it's also going to host the Spark elements that actually execute the query, and it's going to be able to parallelize that execution across the nodes in the cluster. So we should see somewhat better performance. It's not going to be massively better, because I've only got three machines in the cluster, and actually the way these things work is one of those gets configured to be the head node that orchestrates everything. So I should only see maybe a doubling in speed, maybe a bit more, because they're reasonably okay machines even though they're normally the small ones.
So once this has started up, you will see this start to run and it will show the execution time, and it's going to look very similar to the experience that we're used to. So I'm just going to use the magic of video editing to pause this for a moment, and we will come back once the thing's started up. And there it goes. So that took a whole two minutes and 57 seconds to run this very simple code, because most of that time was actually waiting for the Spark session to start. Running it again, a little bit more respectable, slightly over half a second.
So I'm going to run the whole lot now. So this will start to run, and this is basically doing similar work to the previous one. I'm using the Spark client library to read the JSON, and this actually knows how to read across multiple blobs. So you can see I've given it a wild card path here to say go find all the blobs that match this pattern. So when we call this passing in request, for example, it will find all the objects in all the request folders underneath all the root folders, digging down into the date and hour folders that we looked at, and will combine those all together into one big logical data set for me.
So this is then executing that, and actually we can see when it runs it's showing me how this is being distributed across the Spark cluster as multiple jobs. So it's kind of expanding out the work as it goes, because sometimes when it does some work that causes more work to be triggered as a result. And so this is the Spark cluster doing the job of executing this code for me. So in a minute that will be completed, and then if we look a little further down, you'll see what we're going to be doing is going to print out the amount of data that's in there, and eventually this is going to print out a couple of graphs, and I'll be able to show you what it is we're actually looking to analyze.
You can see now it's moved on to this next one. About that done, by the time it says, shows you from when you first started running all, so that didn't actually take a minute to run. That's the total accumulated time. You can see it keeps saying one minute 42 even though these are clearly faster. And eventually we actually get to the point of this, which is down here a little bit, is to render some graphs.
So what this is actually showing is how long a particular piece of code took to execute, plotted against the amount of time we waited in between invocations. So you can see if we wait a long time in between invocations, we get a slightly different pattern of call times compared to if we just do them one after another. And this shows just the left-hand edge of that same data, so this is the same information as in the previous graph. The point being that we've got all the same tools as we had before, but now this is able to run on a Spark cluster, and if I had enormous data sets I could configure a larger cluster and I would be able to take advantage of the scaling out.
Demo: a C# notebook in Synapse
Now another thing I want to show you here is that C# is not the only language. If I say I want to create a new notebook, then I have the option to say which language I'd like it to be in, which you can do from this drop down here. I can choose a C# notebook or a Scala one, and in fact I've already got a notebook that's written in this language. So if I discard that, this is basically the same work as you saw in Python, but done in C#, because, as I said, notebooks are a multi-language technology. The basic idea is you have different kernels that support different languages.
Summary
So there we have it. You've now seen the full range, from running with both the notebook file and the data files stored locally on my local hard drive, along with the UI being hosted on my machine, through to moving some of the data files out into Azure hosted storage, and then moving the kernel itself so the execution ran on Azure, all the way through to a completely hosted solution, where we had a Spark cluster hosting our kernel and also running our queries for us, and a hosted UI provided in this case by Azure Synapse.
There are similar approaches available. Cosmos DB can also give you a hosted notebook, and so can Databricks. So depending on exactly what your storage and compute requirements are, there's several different ways of doing this, but they all boil down to the same thing. Notebooks provide us with a great way of exploring data collaboratively and just poking around in things in an experimental fashion to see what your data has to offer. So thank you very much for listening.