WEBVTT

00:00.400 --> 00:04.920
Let's talk about Spark Connect, the new client server architecture available with Apache Spark.

00:04.920 --> 00:11.240
No longer do you need to run your scripts on the same cluster as your entire spark server is running

00:11.240 --> 00:11.640
on.

00:11.840 --> 00:16.640
So with Spark Connect, you can have an architecture like this where there's an API sitting between

00:16.640 --> 00:22.480
your applications or your notebooks or your scripts, and that's going to connect those very thin clients

00:22.480 --> 00:28.080
that might be running somewhere else entirely to a centralized spark driver server that's running someplace.

00:28.080 --> 00:32.920
So that centralized server is going to be what's kicking off the larger spark cluster there.

00:33.160 --> 00:38.160
And it's going to be handling the analyzer, the optimizer, the scheduler, and the distributed execution

00:38.160 --> 00:42.360
engine that distributes that work out to a larger cluster of spark.

00:42.640 --> 00:46.160
So this is just a more client server way of approaching the problem.

00:46.160 --> 00:50.760
So it allows you to have these very thin clients instead of having everything baked into spark itself.

00:50.760 --> 00:53.040
And that makes things a little bit more flexible for you.

00:53.520 --> 01:00.290
Now the Spark Connect API is currently limited only to the DataFrames API, so RDDs are pretty much

01:00.290 --> 01:04.490
deprecated these days, so they didn't bother supporting them, and I kind of doubt they ever will.

01:04.490 --> 01:08.290
So you do need to use the DataFrames API when you're using Spark Connect.

01:09.250 --> 01:13.330
The spark driver, like I said, is going to be remote potentially and also centralized.

01:13.330 --> 01:15.170
And that can actually save you on resources.

01:15.170 --> 01:20.690
So instead of duplicating that spark driver over and over and over again and all of its overhead, you

01:20.690 --> 01:23.330
can have one of them and share that among many applications.

01:23.330 --> 01:26.170
So that's a more efficient use of your resources.

01:26.450 --> 01:28.930
And the clients again can be very thin and lightweight.

01:29.330 --> 01:32.770
So you can have a standalone Python script or go or R if you prefer.

01:33.050 --> 01:38.250
That just goes off and runs under Python without being part of the larger spark system.

01:38.250 --> 01:41.450
So you don't have to use Spark Submit to kick off your script anymore.

01:41.450 --> 01:46.130
Your driver script, you can just run it under Python, and if you just make some very small changes

01:46.130 --> 01:50.690
to your script there to allow it to talk to Spark Connect, it'll go through Spark Connect to talk to

01:50.690 --> 01:52.530
that spark driver remotely instead.

01:52.530 --> 01:57.500
So that saves you a lot of memory overhead and a lot of time and just spinning things up really.

01:57.500 --> 01:59.780
Also, it's very useful for Ides and notebooks.

01:59.780 --> 02:04.540
So if you're just playing around with spark in a Python notebook, prototyping some data science thing

02:04.580 --> 02:09.460
or, you know, prepping data for machine learning, whatever it is, you can avoid all the spin up

02:09.460 --> 02:13.740
costs of actually starting your own spark driver every time that you do that.

02:14.060 --> 02:18.820
And also for standalone applications, if you want to build your own little data applications that use

02:18.820 --> 02:24.180
spark, you can do that much more simply and distribute those more easily because they are thin and

02:24.180 --> 02:27.300
standalone and just talk to this remote spark server instead.

02:27.300 --> 02:33.100
Now, the way it works under the hood, the clients are sending these logical parse plans across through

02:33.100 --> 02:33.700
the API.

02:33.740 --> 02:38.300
So it's a very language agnostic, you know, that's why it doesn't really matter what language these

02:38.340 --> 02:39.100
apps are in.

02:39.260 --> 02:43.020
At the end of the day, what they're feeding through that API is a logical parse plan.

02:43.060 --> 02:46.220
Think of it as like not totally resolved SQL, right?

02:46.660 --> 02:51.900
The actual protocol for that is through gRPC and protobuf is that that's what goes across.

02:52.260 --> 02:58.310
And when you get the results back, that gets streamed back again through gRPC using the Apache Arrow

02:58.310 --> 02:59.030
format.

02:59.790 --> 03:00.630
Why would you use this?

03:00.630 --> 03:01.430
What's the point?

03:02.070 --> 03:04.350
Well, like I said, applications are going to be isolated.

03:04.350 --> 03:09.230
And one benefit of that is that it's less likely for an application to bring down your whole spark server.

03:09.750 --> 03:14.790
So if your application is just running out of memory, using too much application resources of its own,

03:14.870 --> 03:16.270
that's only going to affect itself.

03:16.270 --> 03:21.030
So you're a little bit less of risk of some rogue application or rogue script bringing down your whole

03:21.030 --> 03:22.750
spark environment, which is a good thing.

03:23.230 --> 03:27.390
Also, these applications, because they don't have to spin up spark from scratch every time they're

03:27.390 --> 03:30.470
going to require less memory and they're going to start a lot faster too.

03:30.630 --> 03:33.790
And that can be important when you're prototyping things or developing things, right?

03:33.830 --> 03:39.270
So if you're messing around trying to develop a new data processing application or data transformation

03:39.270 --> 03:43.590
application, this is going to make life a lot easier because you don't have to wait for that spin up

03:43.590 --> 03:48.030
every time your spark driver is just sitting there waiting for you at all times, ready to go at all

03:48.030 --> 03:48.630
times.

03:49.110 --> 03:53.760
This also makes it easier to upgrade spark going forward, so if you need to upgrade your spark version,

03:53.760 --> 03:54.800
you can do that in one place.

03:54.800 --> 04:00.280
You don't have to go off and upgrade every individual application again, because that underlying protocol

04:00.320 --> 04:06.040
between the client and server is pretty, um, you know, future proof, if you will.

04:06.080 --> 04:07.720
You know, it's forward compatible.

04:07.760 --> 04:11.880
You can usually get away with just upgrading the server, and that will automatically update the capabilities

04:11.880 --> 04:14.720
of all of your applications with one fell swoop.

04:14.920 --> 04:16.960
It makes a little bit easier to debug things as well.

04:16.960 --> 04:22.240
So because you're not wound into the whole spark system yourself, you can actually run these applications

04:22.240 --> 04:23.000
in an IDE.

04:23.120 --> 04:25.160
Step through them with a debugger like you normally would.

04:25.200 --> 04:28.920
Makes life a little bit easier for developing and debugging these applications themselves.

04:29.400 --> 04:32.640
Also, just centralizing spark in one place can save on resources.

04:32.640 --> 04:37.360
Of course, I don't have to duplicate all those spark driver servers over and over and over again,

04:37.760 --> 04:41.640
and within the internals of spark that can be used more efficiently as well.

04:41.640 --> 04:46.720
So because I have this centralized spark cluster, now, those executors can be better utilized because

04:46.720 --> 04:50.160
I can share those executors across multiple clients potentially.

04:50.160 --> 04:52.130
So that's a better use of my of my resources.

04:52.450 --> 04:57.930
However, if you have a really heavy duty application, you still might want to have a standalone separate

04:57.930 --> 04:58.810
spark application.

04:58.810 --> 05:04.170
In that case, you know, this client server architecture works best for lighter weight applications.

05:05.290 --> 05:06.130
How do I use it?

05:06.450 --> 05:08.770
Well, you just install spark as usual on your server.

05:08.810 --> 05:10.050
The spark driver server.

05:10.090 --> 05:11.490
There's nothing different about that at all.

05:11.490 --> 05:14.050
It's just like installing Spark on Linux normally.

05:14.330 --> 05:19.970
But now with the later versions of spark, there is a Start Connect server script under the spin directory

05:20.370 --> 05:21.810
and it is a shell script.

05:21.810 --> 05:26.090
So, you know, you can't really run this under windows easily unless you have like a little Posix environment

05:26.090 --> 05:26.890
going on there.

05:27.090 --> 05:28.010
Uh, it's easy to use.

05:28.010 --> 05:34.290
So you just invoke the Start Connect server script there and pass in a packages argument that just specifies

05:34.290 --> 05:37.290
the specific Spark Connect package you need there.

05:37.450 --> 05:42.290
And that's going to be specific to the version of Scala that your current version of spark requires,

05:42.610 --> 05:44.570
as well as the current version of spark you're using.

05:44.570 --> 05:47.770
So those numbers at the end will vary depending on what version you're using.

05:47.810 --> 05:49.860
Of course, on the On the client side.

05:49.860 --> 05:54.380
If it's in Python, you just got to make sure that you have the PySpark connect package installed through

05:54.380 --> 05:55.500
Pip or whatever it is.

05:55.940 --> 06:01.540
And then the only real change you usually need to make to your Python script is how you create the spark

06:01.540 --> 06:02.380
session object.

06:02.380 --> 06:03.900
So it's going to look more like this.

06:04.140 --> 06:11.060
You're going to say spark session, dot builder, dot remote and then pass in the URL to your remote

06:11.060 --> 06:11.700
server.

06:11.700 --> 06:14.220
So that's going to be C colon slash slash.

06:14.700 --> 06:17.820
And then whatever your host name is that you're running spark on.

06:17.820 --> 06:20.660
And the port which by default is 15 002.

06:20.900 --> 06:22.660
From there it's exactly the same.

06:22.660 --> 06:27.060
So you know, you just create that spark session with a given name, call Getorcreate.

06:27.060 --> 06:31.220
And then you can use that spark session, usually pretty much like you would have normally.

06:31.220 --> 06:37.500
And that's in many cases the only change you have to make to adapt your application to using Spark Connect.

06:38.180 --> 06:42.500
Uh, one thing though, again, you cannot use the RDD interface with Spark Connect and you probably

06:42.500 --> 06:43.460
wouldn't want to anyway.

06:43.460 --> 06:45.700
That's pretty much deprecated these days.

06:45.940 --> 06:49.430
Also the spark context APIs off limits as well.

06:49.430 --> 06:51.590
So if you're using that, you'll have to find ways around it.

06:51.870 --> 06:57.110
Currently, Session State is also not supported, but the specific list of what is not supported is

06:57.110 --> 06:58.230
changing over time.

06:58.590 --> 07:00.550
And of course, this makes more sense with an example.

07:00.550 --> 07:05.710
So let's dive into actually setting up Spark Connect on a little ubuntu instance that I have running

07:05.710 --> 07:07.430
here and see how it works.

07:09.430 --> 07:09.750
All right.

07:09.750 --> 07:13.150
So to use Spark Connect we need to run this on a server of some sort.

07:13.150 --> 07:16.390
And that's going to be kind of hard to do on a windows system on my windows desktop.

07:16.390 --> 07:22.110
So for this exercise or demonstration if you will, I'm going to be running this on an ubuntu instance

07:22.110 --> 07:24.070
that I have running in a virtual machine here.

07:24.350 --> 07:27.670
Now, if you don't have access to an ubuntu instance or a Linux machine, that's okay.

07:27.670 --> 07:28.710
You can just watch.

07:28.710 --> 07:29.630
Don't worry about it.

07:29.950 --> 07:33.910
Now, I'm not going to go through all the details of installing Spark on Linux.

07:33.910 --> 07:35.470
It's actually pretty straightforward though.

07:35.510 --> 07:41.350
So the short version though is I just decompress the current spark distribution prebuilt for Hadoop,

07:41.510 --> 07:44.790
and I extracted that into my apt spark folder here.

07:45.190 --> 07:51.080
And if you go into the S bin folder, you can see that there are a couple of handy scripts here.

07:51.080 --> 07:55.920
There's Start Connect server for starting up your connect server and Stop Connect server as well for

07:55.920 --> 07:56.720
shutting it down later.

07:56.720 --> 07:58.560
So we're going to be looking at that soon.

07:59.000 --> 08:04.880
Also, I installed my course materials under my home directory under Spark course.

08:06.080 --> 08:09.200
And also in there I installed the ML Dash 100 K data set.

08:09.200 --> 08:10.600
So I'm pretty much good to go.

08:10.920 --> 08:17.640
Now spark does have some dependencies, so I also installed the version 21 of the JDK just by doing

08:17.680 --> 08:18.560
apt install.

08:18.920 --> 08:20.680
Um, OpenJDK 21.

08:20.680 --> 08:24.000
Whatever it is, that's the only real prerequisite that you need there.

08:24.040 --> 08:26.560
Obviously I have Python installed as well.

08:26.680 --> 08:29.760
Uh, Python 3.12 I think at the time.

08:30.000 --> 08:30.640
Let's see.

08:31.360 --> 08:33.080
Python version.

08:33.800 --> 08:35.800
Actually it's Python three on ubuntu isn't it?

08:36.600 --> 08:37.800
3.12 at the moment.

08:37.800 --> 08:38.440
So whatever.

08:38.440 --> 08:41.680
You know, as long as you have a compatible set of technologies you're good.

08:42.400 --> 08:42.720
All right.

08:42.720 --> 08:44.730
So let's talk about actually using Spark Connect.

08:44.730 --> 08:49.730
So as a demonstration I'm going to run a Spark Connect server right here on this Linux machine.

08:49.970 --> 08:52.130
And that's going to be running in its own process.

08:52.130 --> 08:55.730
And then from the localhost I'm going to pretend that I'm an external client as well.

08:55.890 --> 09:00.010
And write a simple Python script that communicates with that spark server.

09:00.530 --> 09:05.490
Um, so first let's review the changes that I need to make to my Python script, if any.

09:05.650 --> 09:09.930
As an example, I'm going to go back to that movie similarity script again using DataFrames, but I'm

09:09.930 --> 09:12.290
going to modify that to use Spark Connect.

09:12.290 --> 09:16.330
So let's take a look at the movie similarities DataFrame connect Python file.

09:21.930 --> 09:25.650
And it's pretty much unchanged except for how we create the spark session.

09:25.650 --> 09:26.690
That's really it.

09:27.130 --> 09:32.850
So if we scroll down to where we do that right here, all that we did was we inserted this little builder

09:32.850 --> 09:36.890
dot remote call here, specifying the path to our remote server.

09:37.090 --> 09:41.370
In our case, that's going to be on the localhost on port 15 002 when we're done.

09:41.780 --> 09:43.700
And that's the only change I made in this entire thing.

09:43.700 --> 09:46.060
Everything else just works the same way it did before.

09:46.260 --> 09:51.940
Except that now when I run this script, I'm not going to be invoking the entire spark driver and system.

09:51.940 --> 09:57.100
It's just going to be a thin client that's communicating through Spark Connect to the actual spark driver

09:57.380 --> 09:59.220
running separately on its own server.

09:59.620 --> 10:00.740
Now that's really the only change here.

10:00.740 --> 10:01.340
That's it.

10:01.860 --> 10:04.660
Now, like we talked about, there are some limitations with Spark Connect.

10:04.660 --> 10:05.660
You can't use RDDs.

10:05.660 --> 10:08.780
And there's a few pieces of spark session that you can't use.

10:08.780 --> 10:13.500
But by and large, that's all you have to do to modify your script for a standalone script.

10:13.620 --> 10:16.020
I'm going to get out of this all right.

10:16.580 --> 10:20.700
Now in order to use Spark Connect as a client, of course, you do need to have the appropriate Python

10:20.700 --> 10:21.820
library installed.

10:22.380 --> 10:27.580
And for me, that's just going to be a matter of running pip install Pi spark.

10:29.620 --> 10:30.260
Connect.

10:31.980 --> 10:35.100
And then the version of spark that you're using.

10:35.100 --> 10:38.460
In my case it's going to be 4.0.0.dev two.

10:38.980 --> 10:39.390
For you it you.

10:39.430 --> 10:43.070
It will obviously be something different and I'll kick that off now.

10:43.070 --> 10:49.950
Ubuntu these days is pretty picky about letting you install Python packages into your system wide environment

10:49.950 --> 10:53.390
here, so it doesn't want you to create an environment specific to whatever you're doing.

10:53.430 --> 10:57.670
Since this is just a throwaway virtual image that I'm playing with, I can get around that by just saying

10:58.430 --> 11:01.590
this very scary sounding option break system packages.

11:01.590 --> 11:02.790
Except it really doesn't.

11:03.950 --> 11:07.350
To actually install that now I already did this, so it's not actually going to do anything, but I

11:07.350 --> 11:12.110
just want to illustrate that you do need to install some bit of PySpark for this to work.

11:12.350 --> 11:16.870
And as you run the actual script here for the client side, you'll find that there may be other dependencies

11:16.870 --> 11:21.630
that you need to install as well, like Pi Forge or Pandas and things like that.

11:21.670 --> 11:25.310
You'll get errors if they if that comes up, just install those using Pip as well.

11:25.670 --> 11:25.990
All right.

11:25.990 --> 11:29.750
So first thing we got to do is kick off the actual Spark Connect server.

11:30.030 --> 11:35.030
So to do that I'm going to head back to opt slash spark where I installed my version of spark.

11:35.070 --> 11:40.880
Again this is just straight up decompressed package that I downloaded from spark Apache.org for the

11:40.880 --> 11:43.840
prebuilt version for Hadoop 3.3, I think it was.

11:44.360 --> 11:46.640
And again, in sbin we have the script to kick that off.

11:46.640 --> 11:48.560
So all I need to do is start that.

11:49.320 --> 11:49.640
All right.

11:49.640 --> 11:53.760
So the syntax to kick off our server is going to be under the sbin directory.

11:53.760 --> 11:57.800
Again we're just going to invoke that start connect server script.

11:58.200 --> 12:02.440
And for a parameter we just need to give it the package name for the Spark Connect package that we need.

12:02.440 --> 12:06.920
And that's going to be specific to the version of Scala and Spark that I'm using.

12:07.240 --> 12:18.200
So dash dash packages org, Apache Spark, colon, Spark Connect and then underscore.

12:18.200 --> 12:21.440
And the Scala version that my current version of spark requires for.

12:21.480 --> 12:23.640
For me that's going to be 2.13.

12:25.000 --> 12:32.680
And then another colon and the version of spark that I'm using for me, that's 4.0.0 dash preview two

12:32.960 --> 12:33.480
for you.

12:33.480 --> 12:36.810
It's almost certainly something else because I'm kind of kind of on the bleeding edge here.

12:37.090 --> 12:38.330
Go ahead and kick that off.

12:39.490 --> 12:42.290
And it helpfully tells me that it's logging into the logs directory.

12:42.290 --> 12:46.170
Let's go ahead and open up another tab here and keep an eye on what's going on there.

12:46.170 --> 12:52.490
So I'm just going to say um, CD into logs, tail dash f whatever that file is.

12:53.170 --> 12:56.210
And we can see that that server is indeed spinning up.

12:56.410 --> 12:57.170
Way cool.

12:57.930 --> 12:58.210
All right.

12:58.210 --> 13:01.050
So while that's getting started, looks like it started already.

13:01.090 --> 13:03.730
Let's go back to our course materials on our other tab here.

13:03.850 --> 13:07.770
So I'm going to CD into my home directory and then into Spark Course where I put everything.

13:08.250 --> 13:10.930
And from here I'm just going to kick off that driver script.

13:10.930 --> 13:12.330
Except it's not a driver script anymore.

13:12.330 --> 13:14.090
It's just a standalone Python script.

13:14.090 --> 13:20.130
So rather than invoking spark submit and all the stuff that surrounds spark, this is just a standalone

13:20.130 --> 13:21.570
Python script and nothing more.

13:21.730 --> 13:23.850
And I'm going to start it just with Python three.

13:24.890 --> 13:26.610
It's just a Python script.

13:26.610 --> 13:30.770
And what do we call this thing, a movie similarities data frame.

13:31.130 --> 13:34.060
Because we do need to use the data frame API with Spark Connect.

13:34.460 --> 13:39.340
Connect, which had my little one line change to change how the spark session actually connects to the

13:39.340 --> 13:40.100
server there.

13:40.460 --> 13:44.940
And if I remember right, this takes a parameter of the movie ID that I want similarities for.

13:45.420 --> 13:48.340
In this case, 50 will be the ID for the movie Star Wars.

13:48.660 --> 13:53.260
Let's go ahead and kick that off and take a look at what's going on in the logs.

13:54.540 --> 13:56.900
Looks like stuff is happening, so that's a good sign.

13:57.340 --> 14:00.740
I did not remember to change the log level, but that's okay.

14:00.900 --> 14:03.500
It's showing me that there are signs of life here, which is helpful.

14:03.820 --> 14:07.820
And I'm also seeing there's only info messages and no error messages, which is also encouraging.

14:08.500 --> 14:11.260
So this is a pretty beefy script here.

14:11.260 --> 14:14.140
It's actually, you know, doing some pretty heavy duty machine learning.

14:14.340 --> 14:21.300
But, you know, in a few seconds this should complete and start to stream its results back to the client.

14:21.740 --> 14:25.420
So again, we're using gRPC protocol protobuf going across.

14:25.580 --> 14:32.710
And what's happening on the client side again is it's sort of distilling this down into a language agnostic

14:32.710 --> 14:35.150
representation of the underlying data frame queries.

14:35.310 --> 14:39.990
And what's coming back is going to be an Apache Arrow format when it streams the results back to me,

14:40.350 --> 14:43.070
which it's still chewing on, it's still going.

14:46.110 --> 14:47.310
But here come the results.

14:47.470 --> 14:48.310
And it works.

14:48.310 --> 14:49.310
How cool is that?

14:49.710 --> 14:54.470
So yeah, I mean, the top similarities for Star Wars are Empire Strikes Back and Return of the Jedi,

14:54.470 --> 14:55.590
just like you would expect.

14:55.870 --> 14:56.550
Very cool.

14:56.550 --> 14:58.830
That is Spark Connect in action.

14:58.950 --> 15:03.150
So again, we ran a Spark Connect server on this ubuntu host here.

15:03.430 --> 15:06.390
And on the same local host we set up a client to talk to that locally.

15:06.390 --> 15:08.270
Of course in the real world that could be anywhere.

15:08.270 --> 15:09.950
It could be some server outside.

15:10.230 --> 15:12.390
Um, it could be an application running out in the wild.

15:12.390 --> 15:12.910
Who knows?

15:13.070 --> 15:13.350
Right.

15:13.390 --> 15:16.270
So very powerful architecture here to clean things up.

15:16.270 --> 15:21.830
I'm just going to go back into, uh, spark and run the script to stop that.

15:21.830 --> 15:25.710
So instead of start to connect server, I'm just going to run stop connect server.

15:28.390 --> 15:29.270
And we're done.

15:29.350 --> 15:30.230
It's that simple.
