Spark 4.0 and Delta 4.0 For Streaming Data
Summary
- Streaming data simplification: Spark 4.0 and Delta 4.0 introduce features that simplify the data model for streaming, including Python custom data sources and Spark Variant types for flexible data handling.
- Query optimization for real-time: Delta liquid clustering enables efficient clustering without partitioning overhead, making time-series data queries perform efficiently for needle-in-haystack search patterns.
- Modern real-time infrastructure: Data teams can leverage these latest features to build industry-leading real-time data products with significant performance and processing improvements in the real-time space.
Spark 4.0 and Delta 4.0 For Streaming Data

Talk by Bryce Bartmann, Chief Digital Technology Advisor, Shell
Real-time data is one of the most important datasets for any Data and AI Platform across any industry. Spark 4.0 and Delta 4.0 include new features that make ingestion and querying of real-time data better than ever before. Features such as: Python custom data sources for simple ingestion of streaming and batch time series data sources using Spark Variant types for managing variable data types and json payloads that are common in the real time domain Delta liquid clustering for simple data clustering without the overhead or complexity of partitioning In this presentation you will learn how data teams can leverage these latest features to build industry-leading, real-time data products using Spark and Delta and includes real world examples and metrics of the improvements they make in performance and processing of data in the real time space.
Here’s more to explore:
Production ready data pipelines for analytics and AI: https://www.databricks.com/solutions/data-engineering
The Big Book of Data Engineering: https://www.databricks.com/resources/ebook/big-book-data-engineering-2nd-edition
See all the product announcements from Data + AI Summit: https://www.databricks.com/events/dataaisummit-2025-announcements
FAQs
What are Python custom data sources in Spark 4.0?
Python custom data sources in Spark 4.0 enable simple ingestion of streaming and batch time-series data. This makes it easier for developers to build custom data ingestion pipelines without complex Scala code.
What is the Spark Variant type?
The Spark Variant type manages variable data types and JSON payloads common in real-time domains. It provides flexible data handling for streaming data that doesn't fit traditional structured schemas.
How can I combine historical and streaming data for real-time predictions?
Spark 4.0 and Delta 4.0 enable unified batch and streaming processing on the Databricks Data and AI platform. Ingest historical data and real-time streams into managed Delta tables using Python custom data sources, then train prediction models on the complete dataset. This unified approach ensures consistent feature engineering and reduces latency from data to predictions.
What is a real-time data warehouse architecture?
A real-time data warehouse combines low-latency streaming ingestion with fast analytical queries on the Databricks Data and AI platform. Spark 4.0 and Delta 4.0 deliver this through native streaming support, automatic data clustering, and optimized query performance on both fresh and historical data.
How does Apache Spark compare to Apache Flink for streaming?
Apache Spark in Databricks uses efficient microbatch processing with strong consistency guarantees and seamless batch integration. Flink provides record-at-a-time processing with different latency characteristics. Spark 4.0 on the Databricks Data and AI platform handles late-arriving data and complex stateful operations well-suited for most enterprise streaming use cases.
Full transcript
All right, thanks very much for attending today. It's really good to see all of you here. I'm the applied innovation manager at Shell. And I'm going to talk to you today about uh the latest releases of the open source versions of Apache Spark 4. 0 and Delta Spark 4. 0. Apache Spark 4. 0 was released around two weeks ago. uh and delta uh delta 4. 0 was actually re released yesterday just in time for this. So uh I hope that we can uh use this session to talk a little bit more about all of the new features in these two versions that are going to make streaming uh the ingestion and quering of streaming data better than ever before.
Um so some a quick one here's the forward-looking statement. Never quite know what to say about this slide but anyway we'll move on. Uh and then on complete your surveys um just in the app at the end if you have some time please go in there and just fill in a survey about about the presentation. So I'm I'm suspecting that a lot of you are here today because you probably have some streaming data use cases. You may be using Spark, you may be using Delta.
Um but how does it actually perform as a time series platform? uh there are a lot of options in the market in terms of what we can do with time series but um I want to try and convince you today that you can leverage spark uh the newer versions of it as a time series lakehouse you may be doing it already but what I've seen in using spark for the last six or seven years is that there's been a real step change in terms of our ability to process time series data using these latest versions um and hopefully by the end of the session if I've been successful you'll agree that we've got a comprehensive uh we've got comprehensive support for time series workloads and data using Apache Spark and Delta.
So there's three main topics that I want to talk about um and there's three goals that we're going to try and go through because I think if you've tried to store uh streaming data which is typically time series data into Delta there's been some some challenges uh which I think have been addressed. Um and the first is that we need to simplify the data model. Uh the way we define our delta tables uh can often be tricky because of certain constraints.
Uh the second is we're going to talk about optimizing the data layout. So time series is often or streaming data is often a big data ingestion problem. But the query side of it is often a needle in a haystack. You're often trying to the user is trying to find a very specific set of data in a massive table. And so uh there's been some updates that really help with that. is not particularly new to Spark 4, but um I'll just go through that in a bit.
And then the other one is uh that there's been updates around how do you actually ingest spark uh data sources, streaming data sources that aren't the typical data sources that you get if you're a Python developer. Um, we are normally familiar with ingesting streaming data from things like CFKA, uh, Kinesis, GCP, but what if you have other ones in in in all sorts of industries? There's actually a number of different protocols that you receive streaming data on.
Um, and Spark doesn't support them. So, I'm going to talk a little bit about uh the new options that you can write your own custom data sources in Spark. Uh, that makes a lot life a lot easier. So the first one I'm going to get into is how do we simplify the data model? There is a new update called variant types that has come out in Spark 4 and Delta 4. Um that it makes life a lot easier. So when we look at streaming data types, the workload, the payloads that we typically see are like on the screen.
This is just a simplification. And I'm sure you've got far more complex ones than this, but it's typically some sort of JSON semi-structured payload. It's got some sort of identifier that tells you the device or the measurement or the reading uh the time stamp that streaming data came from or when it happened. And then your data column, like the actual values that are linked to that streaming payload. And this is where the problem often comes in that these aren't strictly typed to one particular type.
So you can have data values that can be string, that can be numeric, that can be boolean, it can be a number of different things. And how do you cater for that type that is changing all within the same data model. Um, and so this is where uh we've been talking to the uh data bricks and spark communities around how do we solve this problem for a number of years now actually and variant types are finally the solution for it.
So before spark 4. 0 0 and delta 4. 0 you were limited by the data types in that Spark and Delta supported. Uh if you had numeric you could choose things like inflats, doubles or decimals. You had detrings, booleans. And if you've got complex data types, you would pick something like an array, a map, or a strruct. But that doesn't really help if you think about that payload that we had before where there was an array with strings and numeric all in the same column.
So how did people solve for this? If you look on the right hand side of the slide, uh there are some options that we've seen people try and choose. So first one is they always like kind of split out that measurement ID and the event time because they're probably going to index by it and search on the tables with it. And then they do things like let's store the that data payload in a string column that's added to the delta table.
So that's the first option that you see there. The second one is people sometimes say, "Okay, I've got mixed types. So what I'm going to do is I'm going to create two different value columns. One that stores numeric, one that stores strings." Uh, and so you get scenarios like that. And those are often actually hard for an end user to interpret because they don't know when they should go to the string column or when they should go to the float column.
And then the other one is some people try and say, okay, I'm going to decide on the exact same uh data schema for my table, but I'm just going to make the value column different. So, I'm going to create multiple delta tables that have the exact same column names, but I'm going to create one that the value column is a string and then one that is a is a decimal or a float or something along those lines to cater for the numeric.
While these solve the problem because they still uh maintain the strict integrity of the data types on those particular values, it's not particularly easy for end users to consume this data. They need to know which column to look at. They need to know which table to look at. Um and so and if we just put it all in a string, they also need to know how to pass that data. And at scale, passing strings can take time. So we've had to deal with that for a while.
But they've now introduced in Spark 4. 0 and Delta 4. 0 uh variant types. So these are best for semi-structured data that is common in real- time data sets like IoT, telemetry, adclick uh analysis and endpoint detection and response or any other type of payload that you get. Uh and you simply use the command pass JSON. uh you can pass in the whole JSON payload into pass JSON and what it will do is create a variant column for you in your data frame that can also be persisted into delta as a variant column or you can just pass single values.
So um anything that is JSON related can be passed into a variant type and then uh into a variant column and then you can query and get all of the benefits from variant types on top of that data. So now how do we use variant types? So we have the pass JSON command to actually convert JSON payloads or semiructured payloads into a variant column and once it's stored uh we can then do a number of things on it to actually get that data out.
So the first one is there is a new uh function called variant get and what variant get allows you to do is basically do JSON path uh expressions to get the data out of that complex type. So that makes it a lot simpler. So you can kind of you know just like you've done with any sort of type of TypeScript uh retrieval of variant of JSON payloads out of a string you can do the same thing here. The next one is you can also cast values.
So you can because it's in a variant type which is a bite encoding that you can see it's an open- source standard. Um it can also you can ask it to cast the values into a particular type. And then the last one that it can do is schema of variant which is actually really interesting. You can run a schema of variant function on a variant column and what it will return is actually the schema or the type of every single row in that table.
So I'll talk a little bit about that in a second. But let's have a look at now how do we architect or how do we how do we set up the delta design table design using varant types. Well, now what we can do is we can have our same ID and event time columns that are probably driving a lot of the querying that we're doing on our tables, but we can just have one column called values which is a variant val type and it can store anything.
So in this particular table, they've decided on the first option that they're going to just store that entire payload, the value, the data payload as one array in variant type. And we can then uh get that value out using variant get and basically we can even search in the index uh that is in that array. The other option which we use is we split those arrays out into individual rows um and we can store a numeric value in the same column as a string value but that value four is uh respected as a numeric field numeric type.
It's not actually converted into a string or anything. It actually behaves exactly the same as the type that we wanted it to. And the second uh row has got the on which is all going to be recognized as a string field. Um so it's it's by row it actually defines what the types are for each of those values. This is this is really great because now it starts to simplify how do we store all of our data. We don't have to have multiple columns in tables.
We don't have to have multiple delta tables. we can now have one table that can handle all of these different types. So this is a a really useful feature. Now what are the benefits? If you are today storing string payloads or your JSON payloads in a string column in Delta, um you now can convert that to a variant column, get all of the benefits that I've just talked about, have all of the functionality in terms of how you pass that JSON payload and use it, but you get eight times faster performance on reading data out of a variant column than if you had it as a JSON string.
So very little reason not to use it. Very powerful feature. The best thing of all uh is that it's open source and Nema from uh Goldman Sachs also agrees uh that this is a great addition to the open source ecosystem. You can use this in obviously data bricks, you can use it in open source spark uh and that is pretty much why I wanted to include it in this because I'm going to talk about all the open source features that make this really cool.
Okay, so that is variant types. So let's move on to the next uh topic. It's optimizing the data layout. Um, I'm not talking about a particular feature that's come out with Spark 4. 0 or Delta 4. 0, but I think it's really important to talk about this one because it is a significant uh benefit for those that are dealing with streaming data. And it's called liquid clustering. You may have heard about them talking about this in last year's Spark Summit or I should call it data and AI summit.
Um, and I'm just going to tell you a little bit more about why this really helps with streaming data. So before liquid clustering came about, the options that we had available to us as Spark developers was partitioning to uh improve performance on reading delta tables. So what we would have to do in streaming data scenarios is often have to pick a column that would help us define the partitioning logic. Now what does partitioning do?
Partitioning is basically providing Spark with a few more options that helps it find the exact data set that you need in your table to read and write to. Um, in streaming data, that partitioning column that we often choose is normally a time dimension, something like a year, a month, a day or a date. Uh, in this particular example, um, the delta table has been partitioned by date. And so what spark does when you write to that delta table is it actually persists each date's values under its uh partitioning which is actually just a folder in the underlying table and that then helps uh spark when you run queries like at the bottom there when you say I want to go and get everything where the event data is between these two dates it will look at the partitioning uh folders and actually work out exactly which files to go and read.
So this provides massive performance improvements if you do not partition your tables and if you're doing streaming data workloads you probably have run into huge performance problems in the past and this is one of the best levers that you can pull in terms of producing better performance. There is something else called Z orderering that was available as well as part of delta which also helped you once you were in the partition you could then navigate like which particular paret file you were you would try and help spark figure out exactly which parket file to read but that worked and it's it's actually still very successful and I'm sure a lot of you have partitioned delta tables today but liquid clustering has tried to alleviate us with some of the challenges with partitioning algod always says, I'm sure he's going to say it tomorrow, that they're going to try and figure out how to reduce the amount of dials and switches you have to use to get Spark to work as best as possible.
And liquid clustering is one of the steps towards that. So, liquid clustering uh is basically the Z ordering that you're probably familiar with uh on steroids. For those of you who don't know what I'm talking about at all, uh if you think about an index and a database table, clustering and liquid clustering provides very similar capability. You will create a delta table and say I want to cluster by certain columns and what what's then happens is the data is arranged in your delta table where it clusters on those columns and puts the data together in the in as close as possible paret files in your delta table so that when spark has to go and look and find the data in that table it has to read a lot less files than before.
So it's a combination. And so when you trigger a query and it goes to the query engine, it's a combination of looking at the delta log to find out which part parket files it should actually read in the underlying delta table, but it also uses statistics which helps it really narrow down the exact type of uh parket file to retrieve or which which of the park files it should retrieve. So again, we're going to run a similar query on it and I want to show you the performance improvements.
So I've created here a table that has 2 and a half terabytes of streaming data in it. There's about 200,000 different devices that are in this table. We are using data brick SQL extra small uh cluster. I'm trying to think of everything that I use so that you can uh see that it was all equally tested. The one is on the partition table and the other one is on the liquid cluster table. Now if we look at the partition table on the left hand side, you'll see that the partitioning did a really good job.
Uh when it took the 2 and a half terabyte table and we ran this query, it filtered down using the partitioning logic to only reading 30 GB of data, but the files it read were 370. The query that I ran was for a year of data. So the 370 kind of correlates to how many partitions are actually in that table. And that's still a good job, right? That is still a lot better than what we uh would expect if we ran it on a full scan on the table.
But now let's have a look at liquid clustered tables. In liquid clustered table, it was able to narrow down the amount of data it needed to go and read to just 6. 94 GB. Uh and only 43 files needed to be read. So now you can see that with liquid clustering, Spark needed to do a lot less work to retrieve the exact same amount of data. And this is where the problem lies with partitioning. You often have to decide on your partitioning logic purely to try and accommodate a more general view on how you want to uh lay out your data in the uh the underlying data storage.
With liquid clustering, you kind of don't need to worry about all of that complexity. You can just say put all arrange all of the data in my delta table according to these particular columns and it will do its best job to try and put that in as few parket files as possible so that when you read or write to that table through like merge commands or whatever that is uh you're going to do a lot less work. Now in this particular example that I set up, I just did this set up this slide so that I could explain the concept to you.
But I wanted to show you the difference in performance from a t a wall clock perspective. On the partition table, 28 seconds, just shorter 28 seconds to do that query for one year of data. On the liquid clustered table, just under seven seconds. All I did was change the same table from a partition table to a liquid clustered table. It literally is a a few uh pieces of additional uh commands you have to put on the delta table create uh and I got all of that benefit four times uh just on this particular example.
But as data bricks quoted me in the article that was released about liquid clustering, we actually saw up to 10 times performance improvement by moving partition tables to liquid clustering. And to be honest with you, it is the easiest thing that you can make a change on in your delta tables to get a really good bang for your buck if you're working uh with streaming data. All right. So the third goal that we I want to talk about is how do we in ingest from a diverse range of data sources.
Um, like I've said, uh, not all data sources that you read for in a streaming actually are coming on the typical brokers that Spark is very comfortable with processing. Hopefully, they are, but in an industry like Shells, they aren't. Uh, we have very old uh technology estate uh that does do streaming. Um, and we do have to cater for a whole wide array of different options of how uh people that help us run our assets uh provide us with the information about those assets.
And uh that's not a simple problem to solve. Um you can often get caught up if you're a Python Spark developer. You're probably doing things like writing all of your ingestion in native Python and then doing a Python pandas dataf frame to Spark dataf frame conversion and then writing it from there. you just get into this world of mess and it's it feels very hacky uh and you kind of wish that you know you could just write your own.
Now Spark has supported being able to write custom data sources that you want to build yourself in Scala and that's how you get uh the jars that you probably are installing for certain streaming data sources. they are written in Scola. But if you like shell, we don't have many Scola developers around. Uh but we have tons of Python developers around. Um and so what we wanted to do was really try and change the way that the this was working.
So we actually reached out to the Spark community and said, we would love it if you could write the API for us to be able to write data sources in Python instead of Scala. And weirdly, they called us up and said, hey, we're doing it. So uh it was really fun to see uh that they were listening to us and that they were very keen on building this. So what we have today in Spark 4. 0 is Python custom data sources. This was released in data bricks a little while ago.
I think it was in 15. 4. Um but the official version that is open source that you can use in any environment that you want to where you run Spark can support Python custom data sources. So before Spark 4. 0 zero. Uh the typical built-in sources that were supported were files, Delta, CFKA. Uh but then you could also if you were using cloud streaming uh services like Kinesis, GCP pubsub or or if you had Pulsar running, you could go and download jars that are written by those uh particular providers that you could install on your clusters that would give you the option to actually ingest data from those sources.
And often uh well the way that you would do it is when you wrote a streaming job, you would say spark. stream. format and then you choose the format that you wanted to use. Now just remember that format bit in your mind for now because this is where the trick comes. Um and then after that is options and options allowed you to specify exactly what criteria you you needed to use with that particular connector. So what your CFKA bootstrap servers were, which topic you needed to subscribe to, just all of the different options.
And so this is the standard way that you did it before Spark 4. 0. You still can do it in Spark 4. 0, but like I said, you were definitely limited by the options that were available. With Pispar custom data sources, we are now able to write in native Python our own functions that support batch reading, batch writing, stream reading, and stream writing. So the full capability is supported. Uh this makes it really cool.
So if you, you know, we're talking about streaming data, so you're probably more excited about the fact that it supports readstream and write stream. So you can read from custom data connectors and you can write to custom data connectors parallelized at scale in exactly the same way that you do any other connector. But you can also do it with batch. So how do we do that? In uh Spark 4. 0 there are some uh new classes that you can import.
So if you go to uh the pi. sql. data source there's a new class called uh import uh data source and data source reader. And you can just now have all you have to do is implement the data source class uh and uh define a few things for it. So in this particular example, we're looking at the weather API. We're going to build a connector that ingests data from some weather API. Um this is all generic. This is just boiler plate.
If you ran this, it probably wouldn't work. So I'm just trying to use this to explain the situation, but uh there's really great documentation on this. So we're going to create a class called weather API data source and it's going to implement the data source class. We're now going to give it a name. So you know when I said spark readstream. format uh we we put in a string piece there that defines what is the format that we're going to read from and this is where we can say what that format should be.
So when we use this and you'll see this in the next slide weather API is going to be the name of the uh the format that we will use. We also got to define the schema. So we got to tell Spark what is the what is the Spark data frame schema going to be that it returns. And here you can define it uh you know as it is. And then we've got a method called reader. And in there we need to implement a class called weather API data source reader that we give it the schema that it needs to return as well as all of the options that the user would have put in their code to help us configure that particular connector better.
So now you can actually see when you look at this and if you've written Spark streaming code before this all starts to look familiar doesn't it? Um and now you can do it yourself. So and you can do it in Python. So if you've done this in, uh you're obviously very hardcore and been around since the beginning of of Spark. But for those of us who are more into Python, this is really exciting news. Um now, how do I use that particular custom data source?
Uh so all I have to do now uh once I'm in my PiSpark code is I just have to do Spark. register and register that class that I've built. From there on in my code, I can then use spark readad. format weather API and I've got my connector up and running uh in that particular job. So it's really simple. Uh at shell we have done a lot to uh adopt this. Uh we have built um our own connectors for our own industry. I don't think it's going to be uh kind of familiar with all of you but uh if you are into industrial processes we have built one for OISoft PI we want have built one for OPCUA um and we have been building them for different skater systems that we use uh and the reason why I can do this talk today is because we actually started doing this a year ago when the first release candidates of Spark 4.
0 and Delta 4. 0 zero came out. Uh, and we've been really trying to push them to their limits because we also wanted to give the Spark community more information about how did this work, how useful was it, how simple was it for a Python developer to uh to get it this stood up, but also what was the performance like? Um, and we're really achieving results with this with the scale of this uh that matches anything else we've built before.
The best part about it that I find is as a developer often we need to do ingestion of data at scale and when we just use native Python there are ways to do this but to be able to distribute a workload um across your cluster and hit an API in parallel uh if it doesn't have too many throttling limits is not a simple thing to achieve but with the framework that we've got here with Python custom data sources it's obviously ly robust.
It's designed for scale. It's designed for parallel compute, but also I hope you agree with me that Spark structured streaming is probably one of the most reliable frameworks for streaming data out there. And so you get all of that plus you can inject your own capability to use it. And so that's really exciting. So let's then just bring it all together. So what is the Spark time series platform look like? Some of you are probably data bricks customers.
So, what I'm about to show you, you're probably going to go, "This guy's mad." I'm trying to This next slide is I'm kind of like worried about it because I'm sure there's architects that are going to come and tear me apart after this. Um, but I wanted to just show you like what what what are the options on the table now with all of this new functionality that is uh that has been released. So, if you are an open-source Apache Spark user, uh this hopefully makes some sense.
Um, we've got on the left hand side different data sources that we can ingest from SDKs, APIs, OPCUA. We can now use PISPAR custom data sources to read from all of these data sources natively into a data brick into a Spark data frame uh without any of the hoops that we've had to jump through before. We can have Apache Spark running on your on-remise Kubernetes uh cluster or your on-remise compute, whatever it is that you're doing.
Um, and it is now taking those payloads it's receiving from these different data sources and it's using the variant type to store it in a simplified data model in your delta lake. Um, your rake orchestration engine if you are running open source is likely going to be something like airflow or dagsta or prefect or something along those lines maybe your own thing. um and how and that orchestration engine can now execute Python code to your uh Spark clusters using something called Spark Connect.
I didn't have the time to unfortunately talk about Spark Connect. Hopefully, you're going to hear a little bit more about it in the summit this week. Uh it's probably one of the best things that have come out with Spark 4. 0 as well. Um but it allows you with a client server architecture to basically connect to your spark uh clusters much simpler than ever before using uh Python. So once you've uh got your orchestration that is running your streaming job, it can also spark uh you can also connect it to data bricks.
So spark connect also can read uh can connect to clusters set up in data bricks and it's all writing its data into delta lake. Unfortunately, the text at the bottom of the slide is hiding that Delta Lake, if you've set it up with liquid clustering, is obviously adopting all of those benefits. Now, all of this is available in the native Apache Spark um and Delta Spark open- source versions. Um, and it provides a lot of capability for you to really be able to process time series and streaming data better than ever before.
One of the things that you're probably still wondering if you know when I started this off was talk about what are we doing about being able to match uh time series database capabilities in spark like how do I do resampling how do I do uh interpolation um and that's a very good question um and so the at shell we have an option and there's there are I would say there's two options that I'd be happy to put my name behind uh the one is And data bricks labs have a package called tempo.
And tempo is a really good package. It's downloaded millions of times. It's it's very popular um that allows you to do um uh time series queries uh in pyth in pispark. So you can download and install it. Um the other one which is available here is an open source package that uh is available through the Linux foundation. It's the energy pro project. I'm from Shell. Uh so that's where we open sourced it. Um the RTDIP is open source.
Uh we're very proud of it. Um but it also provides you with a rich uh set of functions that you can use to queries time series data. So you get all of your queries that are like resampling, interpolation, time weighted averages, which I can tell you is not a simple thing to write. Uh circular averages, interpolation of time, a number of different queries. Um but what we did was we didn't want to only allow this this package to run in Python.
We wanted to support SQL as well. Um and so this package comes as a uh pi pie package. You can install it you can use it in your pispark jobs. Um but you can also just store install it in your own Python uh notebooks outside of Spark. And if you're using data brick SQL, you can configure it to just read the data through data brick SQL and perform all of these queries for you and return it in a pandas data frame. So please check it out.
Uh there is a lot of us that are working on it. Uh it's gone a long way. Um we are not going to be stopping anytime soon pushing behind this. This is one of the best things that we've done in the open source uh world from Shell. Uh and hopefully uh you we'll get some of you to use it as well. So with that, I'd like to say thank you very much for attending the session today. It's great to see an audience. Hopefully I've given you some insights as to what are the best things that are coming out in Apache Spark 4.
0 and Delta Spark 4 uh Delta 4. 0.
Learn more about the Databricks Data and AI platform.
The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.