Custom Scala Spark Jobs in Azure Synapse
Learn to run a Scala custom Spark job in Azure Synapse: create a workspace, add a Spark pool, configure storage, write code in VS Code, and execute.
About this talk
Tutorial
Azure Synapse workspaces can host a Spark cluster. In addition to providing the execution environment for certain Synapse features such as Notebooks, you can also write custom code that runs as a job inside Synapse hosted Spark cluster.
This video walks through the process of running a Scala custom Spark job in Azure Synapse. It shows how to create the Synapse workspace in the Azure portal, how to add a Spark pool, and how to configure a suitable storage account. It also shows how to write the custom job in Scala, using Visual Studio Code's support for Docker-based development to target the exact JDK and Scala version used in Synapse. You will see how to upload the built output to Azure, and then how to configure Azure Synapse to execute the resulting JAR as a custom job.
Read the transcript
Creating an Azure Synapse Analytics workspace
Hi, I'm Ian Griffiths, endjin technical fellow and Microsoft MVP. In this video, I'm going to show you how to write a custom Spark job in Scala and run it inside of an Azure Synapse Spark cluster.
So to begin with, we're going to need to create the relevant resources in Azure. So I've come into the Azure portal and I'm typing in Azure Synapse in the new resource entry. And I want to select the workspaces preview, because that's the one that's going to let me create things in Spark. So I'm going to set up a new resource group just for this, which I'm going to call Synapse custom Spark job, obviously enough. I'm also going to create the workspace, and this is the point of this. This is a new Azure Synapse workspace, and this is going to be the thing that will host the Spark environment that my custom job will run in.
I'm going to create this in North Europe because that's a data center fairly near to me. I'm also going to create a new Azure Data Lake storage account just for this illustration. I'm going to give that a similar name to the workspace. And I'm going to create a file system inside of there, because we're going to need one of those to hold the files that make up my custom job.
Configuring security on the storage account
So it's going to take a little while for Azure to create the resources. So while we're waiting for that to happen, I'm going to go and configure security on the newly created storage account, so that I have the necessary permissions to upload the custom job once I've built the thing. So just to make this easier to access in the future, I'm going to add the new resource group to my dashboard. So that way all these things will be right there each time I come back to the home page.
So then I'm going to go into my storage account. I'm going to go into containers, and in there I'm going to find the file system I asked it to create for me during the setup. And I'm going to select that and then go to the access control settings. I'm going to add a new role assignment here. And the role I'm going to add is the storage blob data contributor. And I'm going to add myself in this role, so I will now have permission to upload data.
Exploring the workspace
So, that's now done. So, now all we need to do is use the magic of video editing to make the creation of the workspace happen a little bit faster. And there it is. So, now my workspace is ready. I can open it up in the Azure portal, and if I click on this workspace web URL link, that will open the workspace for me. I'm just going to close the introductory screen here, cuz I've got a job to do.
Creating an Apache Spark pool
To be able to run a Spark job, I'm going to need a Spark cluster set up. And by default, it doesn't actually create one of those for you, so I've got to go into this manage section and select Apache Spark pools and add a new pool. I'm going to create the smallest one it's going to let me create here, so I'm going to call it small. I'm going to disable auto scaling. I'm going to select the smallest VM size and then leave it at the lowest number of nodes, that's three nodes.
I'm going to click create, and once this is done, I will have an Apache Spark pool ready for use. It won't start that until I try and do something with it, so it's just sitting there as a configuration setting right now. But as soon as I try and use that, it will automatically spin the thing up. And the default settings mean it will shut down after 15 minutes of inaction. So, that means it won't cost me too much money if I forget about it.
Creating a container for Scala development
So, now that we've got our Apache Spark pool, we need some code to upload. So, let's go and build the custom job. So, we need certain versions of Java and Scala to build a JAR file that Synapse is going to be happy with. And you might not want to install those particular versions as the main ones on your system. So, arguably the best way to do this is to use Docker.
So, I've got a folder here with a Dockerfile in it, and I'm going to open that up in Visual Studio Code. And as you can see, this is a pretty straightforward Dockerfile. I've asked for an image with Maven and the JDK version 8 installed, which is the one that I'm going to need to work with Synapse. And I've also asked it to install the Scala build tools.
So, with this in place, I can use the Docker support built in to Visual Studio Code. So, if I click on the little green icon at the bottom left, it gives me the option to connect to container. So, I'm going to open a folder, this folder in fact, as a container-based workspace. And it's noticed there's a Dockerfile in here, and it's asking me, do I want to use that one or something else as the Dockerfile? And I'm going to choose the Dockerfile I've got. So, when I select that, it starts up an instance of that image, and you can see here in the terminal window, I've got a prompt.
Creating the Scala application
So, I'm able to start using the Scala build tools. I'm going to use the template for Hello World, cuz I don't need anything very complicated here. So, I'm just going to ask for the simplest Scala project I can possibly have. And I'm running inside a container here, and it's done what I needed to. And it's asking me for a name. I'm going to call this Synapse Spark Scala. And, you can see that's created a folder called Synapse Spark Scala. I'm going to go in there.
And if we go and take a look at the build file, we can see it's selected the latest version of Scala for me. Now, actually, we don't want that, because if you look at this web page here, which tells us which versions of various platforms and technologies are pre-installed in Synapse, you can see the version of Scala that's pre-installed is actually 2.11.12, which is slightly older than the one that the Scala build tools have created for me from the Hello World template. So, I need to go back into my SBT file and change the version of Scala to match the one that's listed here in the web page. So, instead of 2.13.1, we've got 2.11.12.
Adding a Spark SQL dependency
And we don't actually need most of what's left in this generated file. We could set some properties, but actually we don't need them. The only remaining thing of interest is the library dependencies setting. And we don't need the particular stuff that's listed here in the default file, but I am going to need to add a reference to the Apache Spark components. So, in particular, this code that I'm going to use will be using the Spark SQL support. So, here I've asked it for a reference to the relevant version of that component.
So, now, if I type sbt run, it's going to download all the various pieces it needs to build that. So, it's going to make sure it's got the right versions of everything as determined by my SBT file. And then it's going to compile my project, and then it's going to run. And you can see there it's displayed Hello World, because that's what the template I asked for gives me.
Adding the Spark code to the Scala application
So, now, let's go and take a look at the code. So, this obviously is what the template provided. This isn't quite what I want to run inside of the Spark cluster. So, I am going to paste in some code I wrote earlier. Now, this is based on one of the standard samples from the Spark source code. This estimates pi's value using a technique that happens to be runnable in Spark. Now, you can see I'm using the SparkSession type here. That's available because of the edits that I made to my SBT file. And it's using the Spark context object's parallelize operation to run an algorithm that will produce an approximation to pi.
Obviously, for this to work, I'm going to need to run it inside a Spark cluster. So, I could type sbt run again, but if I do so, it's going to fail, because when it tries to create the Spark session object, it's going to go looking for connection details, and they're not going to be there. So, if I run this, we're going to get an error. Now, I could in principle set this up with an environment variable that points to a Spark cluster, at which point it might actually be able to work. But because I haven't done that, you can see that immediately after printing out hello world, we get an exception, which is exactly what I expect. So, this is just failing because it can't find a Spark instance to talk to. It's saying, "A master URL must be set in your configuration." But that's fine, because this is not where we're going to run this.
Building the JAR package
Remember, the whole point of this exercise is to show how we can run custom code inside a Spark cluster hosted by Azure Synapse. So, instead of running it, what we're actually going to do is build a package that we can upload to Azure Synapse. And once this is finished, you can see that it has created this JAR file that is the output of the build process. And that's something I can upload into Azure Synapse.
Uploading the JAR to Azure Synapse
So, back in my Azure Synapse workspace, what I've now got to do is upload that JAR file into storage. So, if I go into this storage tab and look at the linked accounts, I can see the storage account that I asked the Azure portal to create when setting up this workspace. And in here is the file system I asked it to create. So, I'm going to go into there and create a new folder to hold the JAR file that I just created. So, let's rename that something more sensible.
And now if I go into that, I can click the upload button. And then go and find the file that we just built with the Scala tools. There it is. Going to click upload. Shouldn't take long, cuz it's pretty small. And now I'm going to need the URL of that particular file. So, let's copy that out of here with this copy ABFS path.
Using the JAR in a custom Spark job definition
And now that I've got that, I can create a job definition based on this JAR. So, if I go into the develop tab and click the add button and create a new Spark job definition. It defaults to a Scala based job, so I can just paste in the path to the JAR that I just copied. And I need to tell it the full name of the class that defines my entry point. And I also need to tell it which Spark pool it's going to run on. So, I'll select the small pool I created earlier. This is only going to need one executor to run.
Testing and monitoring the job
And now that I've done this, I can test the job by clicking on the submit button. So, this will send it off to the Spark pool. Now, the Spark pool wasn't actually running. I created it earlier, but I hadn't yet given it any work to do. This will cause it to start running that cluster for me. And I can go and look at the progress. If I go into the monitor tab over here, and I look at the Apache Spark application section, there's the job that I just submitted. So, this is the custom job that I've just defined, running cuz I clicked the button.
So, if we take a look at it, it's still in a submitting state. So, right now there's nothing to see, because the Spark cluster hasn't got as far as doing anything. So, there's no log output, there's no activity output. But, if through the magic of video editing we speed this up a little bit, then we can skip straight to the end, and you can see that we've got some representation of the work that was done here in the top half, and then the bottom half shows me my log output.
Inspecting the results
Let's just resize things a little bit, and now let's take a look at the standard output from my job. And you can see that there's the Hello, World message that it prints immediately after it runs, and then the output of its approximation, a very rough approximation, to pi that it created by running an interesting algorithm inside of Spark. And you can see at the top half here, the portal has shown me some information about the work that was done. Obviously, this wasn't a terribly complicated or onerous task, so it's not done a huge amount of work, but for a more complex job, you'd see more information here.
But, this shows that I was able to write a class in Scala, upload the JAR into this Azure Synapse environment, and run it on a hosted Spark pool. And there we go. I hope you found that useful.