Skip to main content

Creating a Custom PySpark Stream Reader with PySpark 4.0

Summary

  • Custom data source support: PySpark 4.0 introduces custom data sources capability (available in DBR 15.3+), enabling developers to read from unsupported systems like ActiveMQ without middleware workarounds.
  • Simplified integration: Previously, developers needed third-party libraries or complex RDD-based solutions; custom streaming readers eliminate the need for middle-man processing between source systems and Spark.
  • Databricks ecosystem benefits: Custom PySpark readers work with Delta Lake, Unity Catalog, and Databricks Workflows, enabling governed, orchestrated data pipelines from legacy systems.

Creating a Custom PySpark Stream Reader with PySpark 4.0

Watch: Creating a Custom PySpark Stream Reader with PySpark 4.0
Talk by Skyler Myers, Head of Data Engineering, Entrada
PySpark supports many data sources out of the box, such as Apache Kafka, JDBC, ODBC, Delta Lake, etc. However, some older systems, such as systems that use JMS protocol, are not supported by default and require considerable extra work for developers to read from them. One such example is ActiveMQ for streaming. Traditionally, users of ActiveMQ have to use a middle-man in order to read the stream with Spark (such as writing to a MySQL DB using Java code and reading that table with Spark JDBC). With PySpark 4.0’s custom data sources (supported in DBR 15.3+) we are able to cut out the middle-man processing using batch or Spark Streaming and consume the queues directly from PySpark, saving developers considerable time and complexity in getting source data into your Delta Lake and governed by Unity Catalog and orchestrated with Databricks Workflows.
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 problem does PySpark 4.0's custom data sources solve?

This video addresses the difficulty of reading from unsupported data sources like ActiveMQ, which previously required third-party libraries or complex custom code.

When is this feature available?

Custom streaming data sources are available in DBR 15.3 and later (custom writers in DBR 15.2), officially included in Spark 4.0 release.

How do I build a custom PySpark stream reader?

This video provides technical guidance on implementing custom stream readers in PySpark 4.0 for the Databricks Data and AI platform. Custom stream readers enable you to ingest data from sources not covered by standard connectors by implementing the streaming data source interface. The video demonstrates best practices for building robust, production-ready stream readers that handle errors, maintain state, and integrate seamlessly with Databricks structured streaming pipelines.

What are the key considerations for streaming data sources?

Building custom streaming sources on the Databricks Data and AI platform requires attention to fault tolerance, exactly-once semantics, and efficient resource usage. This video covers best practices including proper offset management to enable recovery from failures, efficient batch reading, and integration with Databricks' Catalyst optimizer. Understanding these considerations ensures your custom stream readers perform reliably and efficiently in production environments.

Full transcript

All right. Hey everyone, thank you for coming to my talk. Um, going to give a brief intro. It's a kind of a meaty uh presentation, so we're going to try to go through it as quickly as I can and leave some time and answer some questions. Um, so I'm uh based out of Miami, Florida. I work as the uh principal data architect at uh Intrada and we're a boutique uh data bricks implementation firm backed by data bricks ventures.
Um so yeah we we do a lot of data bricks implementation on a lot of their data bricks' biggest clients. So this talk is going to be about creating a custom uh data source with spark pispark. Uh it's going to be a streaming data source and this is one of the bigger new features of spark 4. 0 4. 0. Um it's been out in some data bricks runtimes for a few months now but officially part of the 4. 0 release. Um, you can create batch readers, you can create streaming readers.
In this case, we'll be doing a streaming reader. I'll be getting some tips for best practices around that around Spark streaming and that kind of thing. Um, so getting right into it. So the problem that the custom data source uh solves is as we know Spark has built-in data sources. Uh you can read from Delta, you can read from Kafka, you can read from JDBC, um CSV, you can read all these different formats. But what happens when you have a data source that isn't natively supported?
Uh typically it's not easy. Typically you've got to build you got to find a third-party library that's the easiest if you're lucky. Uh most of the time they're not maintained. Uh you might find like a 13y old stack overflow post or something like that and hope that it works you know on RDDs you know a spark 1. 3 or something trying to work that on Spark 3. 0. It's really tough. I think we probably all been there before.
And if you can't find a library then you have to just build your own. And most companies don't have the engineering manpower or to do that in the first place and then maintain it, keep it updated with new Spark versions can be pretty tough. So with this uh API that is launched by the launcher with 4. 0, it's a whole lot easier. Um so yeah, what this does is it available in DBR 15. 3 plus um that's for stream reader, stream writer 15.

  1. uh it it's gonna so Spark is now going to provide you with some some interfaces that you can then import into your code and then you can implement you can just override some of the built-in uh methods there of those interfaces with your own code and create your own custom reader. So once you do that it's as simple as spark readad. format and then you just pass your new custom reader as the argument to that. So like you would if you're reading a CSV file spark read. csv CSV that's just a data a data source that is already implemented within Pispark and now you're just going to override those methods just inherit those into your own classes create your own data source and then you can read it just that easily.

So getting into uh what we're going to actually be implementing here. So we're going to be using something called Java Messaging System. And for those that don't know think Kafka but much older um obviously written in Java. It's a it's a message service. So this is a service that sends out messages. So you can uh receive streams essentially from this. You can get notifications about changed applications like delta notifications, that kind of thing.
Um typically what you would read with Kafka, but in older enterprises and banks and whatnot that uh are slow moving, a lot of people still actually have this. Uh and there's a few protocols that can read from JMS, a few few uh message brokers, and the most popular is this Apache Active MQ um maintained by the Apache Software Foundation. So it's open source uh but it's uh you can't there active mq is not a supported data source in pisipart.
So if you want to read js with active mq you can't yeah there's uh I couldn't find a third party library. I mean this was something that in my uh my gig when I was implementing this we had to write in java I don't even know java. someone another team had to come in create a Java class essentially to read from a uh active NQ and then write to a MySQL table and then we had to then since my SQL it would just read from JDBC with spark we could then read that from spark so just a whole huge workaround a bunch of extra infrastructure that had to be spun up uh very tedious definitely not an ideal solution so when I saw this in the documentation for the custom data source the first thing that I tried to do was see if I could actually refactor that and cut out that middleman and read it straight with spark And I was actually able to do that.
So I'm going to walk through how that works and how you can kind of apply that to maybe your use cases or if you ever see it in the wild in the future, you you'll have an idea of how to actually uh handle it. Uh so what you can think so there there are ways to connect to active MQ. One way you can use Python. So there's a something called the stomp protocol uh which is a way that you can listen to these brokers. And you can think of active MQ as if if you think of the JMS as the mail and you as the receiver, you want to get that mail.
Active MQ is like the mailman. It's the person who deliver is the thing that delivers the messages to you. So we have to find a way to communicate with the mailman essentially to get those messages. So stomp is one way one protocol to do that. Uh it's written in Python and you can import that you can download it in data bricks notebook. You can just pip install it. You can add it as a cluster library. Um and then you can read this in Python and you can kind of think of this custom data source in Pispark as like a Python udf on steroids. um you know you write your code in Python.
You have a function you want to distribute it uh on a Spark cluster then you just wrap it in the in the Pispark UDF. It's kind of like that except it's a little more in-depth. It's uh much more customizable. Um so in this case we're going to still write our Python code. We're going to still use Stomp. We're going to still install that Python library. We're going to write basic Python methods and object oriented pro programming Python here uh to write our uh custom data source in Pispark.
And so to walk you through it in Python first. Uh so you could do this. I mean this works. Uh it's single threaded though. So if you have really low latency requirements or a bunch of messages are coming many messages a second. You have multiple topics or cues that you want to subscribe to. Python can't really handle that. It's not really fall tolerance. It's uh not as good observability. Um you all those things with pyro with spark structure streaming.
That's what we want. We want those metrics. We want the fall tolerance. Um, so if you're stuck with Python, it's not very it's not a very good solution there, especially in a enterprise grade data if you're dealing with that kind of volume. So first things first, you need to know how to implement it in Python though because we're going to follow that pattern within Pispark. So there's a connection listener class within uh within Stomp, which you just it just tells Stomp how to connect to your individual broker service, active MQ.
Um, and you just inherit you create your own class of it. You inherit that uh you create a child class. I just named it my listener as as a test here. Create your initialization which is going to create a connection whenever you create an object a connection object of this class. And then you do a few things. Uh so you have a on connected method on error on message. These are things that happen during the states of this job essentially.
So when you connect do something when when there's an error do something. Uh when you get a message do something. So these are all interfaces that you have to implement yourself. So stop when when you run this stomp knows what to do with those methods but it's up to you to put your own custom code in there to figure out how you actually want to handle that stuff. So like for instance uh the on message is very important as you might imagine.
Um you're going to get a frame back. That's why that's why you see uh the frame as a parameter for all these methods. So the frame is just the message itself. Uh usually what depending on what stomp protocol you're using, you get different parts of it's usually just the JS JSON payload. So you get things like the body is the actual message that you probably want to consume. But there's a lot of metadata around there.
There's headers. You can see there's there's a destination header which is the queue that you're subscribed to or the topic that you're subscribed to depending on your service. Um and then so you can you can do different things. You see I'm error handling here. Um obviously this is very basic because I'm just showing you how it works in Python. But we're going to take this code and we're going to uh you going to create an connection object with stomp.
And so all you have to do is pass stomp your your uh host and your ports. Uh so you can pass it a list. So if you have multiple host imports like backups, if one goes down, so stomp's going to go through that list and try to connect to the first one that it can. And if one fails, you have some uh fallback just in case. So it's always good to have multiple uh host imports in case one fails, you don't want to miss any messages or anything like that.
So Stomp can handle that. Uh then you uh with your connection object, you just set your listener. Um so I'm I'm instantiating my listener uh inside of that set listener um call right there. And then I'm connecting with my with a username and password. you know, just regular username and password uh connection to the uh active MQ service. And that's how I'm connecting. So now I've got this this connection object that's connected to my my broker.
And then I got to tell it what to listen to though. Uh there's there's different topics, different cues that are being delivered to these messages are being delivered to those cues, we'll call them. And you can subscribe to one, you can subscribe to many. Uh and in this case, I've got a a list of cues I want to subscribe to called Q's. And I iterate through that list. And each Q needs a needs a unique ID. So I just use enumerate to create a unique ID for each Q.
Assign that to the subscribe call here. And then you have to have uh acknowledgement style. So this is pretty important. This this has to deal with fault tolerance. So auto mode is the most basic. So the message system in our case JMSs has to know when the if the clients in our case us receive the message or not. Uh the client has to know some way. So with auto, it's the most basic to implement, but it's the least uh safe because there's it's gonna assume if the message was sent to the client, it was received.
So if in our case, if we're using Spark and we're using this message mode, let's say it gets to it gets to Spark and we're doing a right, we're doing some sort of data frame manipulation on it, driver fails, uh something happens, a network issue, whatever, something happens, we lose that message. So in a produ I'll show you as we get in into the meetup a little more but in a production grade setting you want something there's called client mode whereas the client actually tells the messaging system that we received this message.
So that's something so each message has unique ID. So the client once you process it you can take that unique ID out of that frame that I was talking about and then you can send it back to the message service and say hey we received this message take it out of the queue give us the next one. Uh so that's what we're going to be doing. And what we're going to do is take that Python code to connect to Stomp and connect to Pispark.
So kind of similarly, we created our own custom class there based on a Stomp class that we imported. We're going to do a similar thing here with uh with Pispark. So there's a new library that you can import from Pispark SQL data sources. And there's a few classes that you have to implement here. So one is the input partition class. So I'm creating my own class active MQ partitions based on uh inheriting from that. I'm creating a child class on it and I'm implementing its methods here.
So all these methods are are in this interface. And so for this one, this tells Spark what to do with all the data partitions. So you're going to obviously distribute this. You're going to put on different cores, different executives, uh and it's going to be doing a body of work. So in this case, what does each core do with that data with that micro batch? So that's what the input partition CL interface is for. And so it's pretty simple here.
Here I've got a start offset and end offset. So I know to I iterate through the messages on this read method. Um so on the initialization we have the earliest offset and the latest offset. So that's how we track our messages that we've read and that we've consumed. So each message is going to have a unique offset within each partition. And then we're going to start with the lowest and we're going to iterate through that all those frames until we get to the to the last one and we're going to finish with processing.
Um then of course we just have the message itself. Um in this case it's a it's a list of tupils. Uh the tupils have offset that we're dealing with the queue, the message, a time stamp that we received it, and an error if it had one. So that's a that's a custom kind of a uh object there, data object with that tupil. That's what I made. That's what I'm eventually turning into a row and eventually putting into my data frame, eventually writing into a delta table.
So I want to capture the offset. So if I look back at my bronze table I'm going to be writing to here, I want to see what offset this message belonged to. I want to see what queue it came from. I want to see when it happened. I want to see that time stamp. And then I want if there's an error, I want to know if there's an error. So if there's no error, we'll we'll see a little later the error handling how that works. Um but so we so whenever the uh input partition or in this case the active and Q partition implementation of it is called these methods are going to run.
Uh so one important thing that you'll see is this get state and set state. Uh well actually let's go back to the read real quick. So you see with self. lock um this is very important when when you're streaming. So you have multiple threads here with stomp. Stomp has a thread for reading new messages for understanding when they come and it has a thread for maintaining the messages that are already in the queue uh that you need to need to process.
So when you're actually processing these partitions you want to lock that message that message group. It's a it's a list that remember the list of these tupils you want to lock that so as you're doing that processing stomp is not adding more messages to it and you get state conflict essentially. So during this partition, that's why we're going to we're going to lock it here. And then we're going to uh for checkpointing, you can't you can't serialize lock.
So if your message is locked, and I'm using threading for locking, by the way, the threading module. So if if your message is locked, you can't serialize it. You can't write it to a checkpoint. So the first thing that's going to happen is we're going to copy that dictionary. We're going to remove the lock from that dictionary, and then we're going to return it. And then so whenever Spark is deciding to checkpoint this stuff, that's the state that it's going to use.
And then when it uh finishes that write, it's going to set a new state. We're going to take that uh dictionary back and we're going to relock it and we're going to rec create a new connection. Um so that's what that get state set state is and that's what read does. So that's what's going to happen for every partition. And another class that we have to implement is the data source in our case there's there's a data source reader and there's a data source stream reader that pispart provides for you.
Uh the stream reader is what we want to use for streaming. Um and as you saw before uh the connection listener that was from Stomp. So uh remember the Python code I'm I'm implementing both here. So these are both my I have the child classes of both these parent interfaces essentially and doing a lot of similar stuff here. I'm doing my initialization and in this case you you might see some familiar code here. Uh I'm taking uh those same parameters that I used to connect originally in Python except I'm now using the options dictionary in Spark.
So think of it this way. This is when you use spark readad. format. You supply your custom data source and then you do options. So it's like when you're reading CSV, you do header option true. So this is going to do that same parsing mechanism, but in our case, we want some custom options. We want what host we want to connect to. We want the port that we want to connect to. We want the password. We want the username.
And you can imagine there's there's multiple things you can you can do here. This is this is basic implementation of it. But if you really want to get uh good production grade code, you can set uh defaults. I mean, in my case here, I've got a default host, local host. I've got the default active MQ port. Uh just in case there's one that they forgot to pass it when they're connecting. Um of course you want to probably do like a notimplemented error or something like that if they don't pass a uh password or something like that.
You might want to say that just give a message to the user that hey uh you can't connect to this if you don't give a password or if you're using SSL that sort of thing that is all handled here. So this is just setting that connection up and then parallelizing it with the data data source stream reader. Okay. So more connecting here. Um I'm just adding random print statements. Uh in a if I was actually doing this in production, what I'd probably do I' I'd set up a Python logging and then I would probably write the logs to a uh materialized place like in a data volume or something.
So then you can go back and see the logs and I I would probably I probably definitely put logs when I connect, when I disconnect, uh when I um subscribe to a broker, uh when there's an error. So then you can always go back and read even in Spark you'd read that uh that log file and then go back and debug. Um that's probably what I would do. But here like I said it's a pretty basic implementation. Um I'm going to be doing the same the same concept here with subscribing to a host.
Uh in this case you in my commented out code here that's the iteration that you saw earlier where I connect to multiple hosts. For the sake of example I'm just connecting to a single host here. I'm using auto acknowledgement mode but again uh in a in a stronger implementation you don't want to use auto. you'd want to use client side in which case you send that message back to the messaging service when you receive it.
Uh we're also handling the get state and set state again in case there's checkpointing going on. We want to remove the lock from the from the checkpoint uh object that's being written. You want to remove the connection. Those are not serializable. The connection is it only persists within that run of the class. You can't write that to a to a checkpoint. Same with the lock. So that's the get state set state. Again, wrong button.
Uh so on connected uh again I mean this is I would write this to a log. Um I'm just printing it for the sake of this demo but maybe just a nice connection message. Okay we worked. Uh then the on message this is going to be the beef of the operations here. So this is what you want to do with the with the messages that you're sending to a so so in this case it's determining what micro batch like what web messages are part of what microbatch to send to that input partition interface that we implemented earlier.
So, I'm going to I'm going to uh append my to my internal class messages. I'm going to append this uh payload, the JSON payload that I got, the message itself, and I'm going to remember in the message tupil that we were looking at, we had an offset. We had uh uh I think a frame body, we had we had the queue, we had the time stamp. This is what I'm passing. So, I'm creating the current time stamp using basic Python daytime.
Uh setting that to uh Eastern time zone. It's what what I'm normally on. Uh and then I'm getting a Q name here. Uh I'm setting a uh for I'm getting that the Q name from the actual response. So I'm using I'm pulling getting a dictionary method here to get that Q name. If it doesn't have one, I'm just writing unknown. You can do some data data analysis later on that you just filter for unknown and see the issues there, what offset it came from, the time stamp, if there's an error, that kind of thing.
And as I'm appending these messages, I'm checking to see if there's an exception. Uh if there is exception, you can see the final piece of that tupil there. uh if it if it reads that message and appends it to the internal message uh object fine then there's it's none it's just going to be null uh but if there's an issue here uh we actually capture that exception uh convert it to a string and then we can go back and again do some more data analysis we can go back to our bronze table and filter for filter out the nullles for uh the exception and if there isn't any if there's if there's an exception we'll see that and then we can handle it from there and then we're going to iterate the offset so each one of these messages that we get is going to have a unique offset as I mentioned so that's how We're going to capture that again when we're when we're going back and error handling where where our states are being handled within the uh partitions.
It's going to understand that offset. Uh it's going to use the offset in checkpointing as well. The checkpoint is going to know the latest offset that it read from. So if Spark does fail, if something happens and it picks back up, it'll pick back up at that latest offset. It'll be captured in the checkpoint. And then uh we have those other you probably recognize these. This is are from that original stomp implementation.
These are the methods I'm overwriting here. So you can do something on error. Maybe again that would print that to a to a logging uh file somewhere on a volume most likely have a message on disconnect and yeah setting initial offsets and keeping track of the latest offset setting that initial offset keeping track of that latest offset. Um so we know what messages we've already seen. We don't double dip and process the same message twice and we don't we don't lose track of the of we don't miss a message.
We know the exact next one based on that offset. So you have to so partitions this is a method from uh the data source stream meter class. This is the pispar class method. Uh so in this case this is where you're going to be actually instantiating that active MQ partition interface that we implemented on the previous slides. So every time we're going to uh create a new active MQ partition with those messages. So we already filled up that message list with all those tupils.
So this is that's what's going to be returned from here. Um if if there is a new one. So if the end offset is is less than the start offset, we know there's no new messages. We don't need to create a new microbatch. And this has to do with your trigger intervals. So again, we're streaming. So you have you have trigger intervals here. Uh do you want to do you want to check for microbatches every 1 second, every 5 seconds, every 10 seconds?
You can kind of tune that. And if there was no new microbatches, the offset didn't increment. So you're not going to do anything. Uh in this case, uh if there is a new if there is a new message, there's going to be new offset. And then you're going to create the a group of active MQ partitions. And it's doing a lot. I mean that sounds like a lot for there's not many lines of code here. So it's pretty nice. I mean Spark handles the the batch sizes.
It handles all the checkpointing for you. Does all that for you. So you just have to know how to just basically basically implement these these uh this interface and then you're good. Spark handles the hard stuff. Uh and then you have your commit. So in this case my my commit is is pretty simple. Uh in the case because I'm using auto acknowledgement mode. In the case where you use client acknowledgement mode, this is where you would implement that system that I was talking about where you actually talk back to the messaging service and say, "Yeah, we got this message.
We got this offset. This is the message ID. This is the subscription ID that it came from uh from our host." Uh so that's where you would implement it in in the commit. So after commit happens, that's when the offset is standardized. This is the writing to the checkpoints. Um that so swag is just keeping track of the current state for commit. All right. Uh so you're going to set us okay so we're going to skip down to the bottom here.
Um active MQ data source. This is the last class that you have to implement. So last interface uh well my my implementation is active MQ data source but you're inheriting from the data source class. So as I mentioned there's a data source for CSV files there's data source for uh delta delta the delta format there files. Um so in our case my new data source you have this name method that you're that you're creating this class method.
I'm calling it active mq. So now what I can do I've implemented all these methods. So what I can do is spark readad. format active mq just natively as if it was always there and it's going to do everything that spark does all that fault tolerance all that logging all that monitoring all that keeping track of state is built into your program now just from those few lines of code I'm going to define a schema for my actual dataf frame for my pispark dataf frame here and it's similar to what you were seeing just with a python uh python objects there with that tupil.
So I've got my offset just an integer. We got a Q. Got a message. Got a receive time stamp. Uh and if there's an error, you can see that uh some of these are allow nulls. Of course, error allows nulls. Uh one thing that I did think of after I already created this presentation is uh for the message, it's a JSON string that you're getting back. Uh but for me, I usually I usually subscribe to multiple different Q's and each Q sometimes has a different JSON structure.
So you can't just define the you can't use a strct type here because it changes depending on what Q it came from. The message changes. So this is a perfect utilization for the variant type. I don't know how how much you guys have seen of that for Spark 4. 0, but it allows you to uh put put structured JSON without defining a schema. So you can have different JSON schemas within that column. So it's perfect use case for a variant type and then you can access that JSON string as if it was a defined strct type.
You can use dot notation and everything. Um so that would be a good use case for that. Uh and then for there's this reader method. Um so that's where I use a notimp implemented error. So this is what happens when you do spark readad. format active mq. Uh it's a stream. So you're not you can't read you can't read it in batch mode. So I just have this notimplemented error lets you know hey use the use the stream reader.
So that's when you do spark readstream. Active mq. So I'm implementing a stream reader here. You could actually do both. I mean you could technically I mean if you if you have like two minutes between uh your triggers or something like that then you could do this in batch mode. Uh I definitely prefer streaming because it keeps track of that checkpointing. It's much safer. it knows what it's much easier to implement to keep track of state with a stream than it is with batch.
You have to kind of do a lot of your own custom code if you're using batch to manage all that. So in our case, yep, we got the stream reader uh method uh filled in here. And then this is where we actually create that active MQ streamer class that we just implemented. So that's the class that actually knows how to do the uh the parsing of the message, knows how to connect to the broker, knows how to communicate back and forth with the broker, knows how to divvy out partitions and micro batches.
Um so that's when so you'll you see uh when you do sparre read stream pass this format this is when you're instantiating that active stream reader class and then when you're finally uh last step here so finally you've implemented uh all these interfaces uh at least three of them there's others that you can do uh this is this is again the most basic implementation of this then you can register that data source that we just created so we just created the active MQ data source from the data source uh interface and now we're registering with spark so this allows us to actually call spark. restream. form format active mq which you see on line four here.
So now I'm not doing anything. I mean if if you saw this and didn't see the presentation and started on line three, you might think that this is just the new data source with spark 4. 0. I mean there's there's no differences here. You're passing a options. Um you can pass a dictionary of options if you want. Um just same functionality as any other uh stream streaming source. I mean this could be this could be Kafka, right?
Uh pretty simple. Um so you can do a lot of you can after this you can do any data data frame manipulation that you want. So it's it's just it's a streaming data frame. Now you can do anything. So in this case I'm creating a temporary view. I call it active in view messages and I'm reading from that uh that view. So then I'm just reading that stream in uh with SQL and I've got my checkpoints. I' I've got saving my checkpoint to a volume.
Uh I'm having a very quick trigger processing time. Every 1 second it's going to check for my microbatches and I'm appending those microbatches to a table. So I'm writing this all to a delta table. I've got my table variable defined using uni catalog here. You have all your all your safety nets, all your protections, everything that data bricks gets you out of the box. You now have in your custom data source with just what was it 200 lines of code if that and then you can look at all the metrics.
Again, this is just a pure pure uh uh pispark stream. I mean, you can look at all the messages. You can look at what each microbatch processed. You can look at how long it took to process them. You can see your memory utilization. You can see everything here. Here's the schema as as we saw. We define that in our in our uh reader uh in the actual data source itself. The the offset, the queue, the message, all that good stuff.
Um yeah, I mean this probably looks very really familiar to anyone who's used spark structure streaming. It's the exact same thing. Here you can look at like I said you can look at the raw data, see which batch ID, um how long the batch took to process. This is where you can kind of uh tune your trigger interval here. I mean in this case I had one second trigger interval. I'm only processing two rows. I could probably uh lower that a little bit, but uh you can see all all the different offsets, what's getting committed to that checkpointing file in the volume um just as you could any other stream.
And there you go. That's that's the um that's the actual table that I'm reading from uh itself. So I I save that to that to a delta table. I'm literally just reading for that delta table. And then there you go. You got got your offsets freeze message. Uh the queue I just had this fake test Q here. uh in the message uh I don't know if you noticed in the in my message tupil I was parsing uh just a piece of that out just so there's no like data breach here.
So just looking at a time stamp here for my message but typically that would be a JSON string as I mentioned I'd probably make that a variant data type if I could go back and redo this and I got the time stamp for it. So you have an interactive time stamp. Uh most of these are not going to be the exact same. So you can do all sort of time series analysis on that. And then like I said with the error, if if there's an error there, that's you want to see null there obviously.
Um but if there was an error, it's caught in that exception block and it will uh put a string. It'll translate that error to a string and then you can uh do whatever you want with it from the table. All right. Uh I think we're right on time. So to summarize, uh this is a pispark data source uh custom data source implementation for reading a stream. You can actually do writers too. You can have your own writer. I haven't haven't done that yet, but this is uh for reading purposes.
Um we read an active MQ stream uh using JMSs protocol. Uh and we did in about 200 lines of code and we saved ourselves the headache of creating our own custom library of maintaining that uh yada yada yada. In this case, what I'd probably do, I'd probably just save this to source control somewhere, my version one. Uh once I do all those enhancements that I mentioned, uh I'd save that and then I'd probably just create a wheel file out of it.
And then I would just install that wheel file on my cluster on my job and then you could just use that in a notebook. That's probably in my if I actually productionize this, that's probably how I would handle it. So that probably my recommendation there. But yeah, now we got this seamless real-time data data integration from a source that Spark had no idea how to read, never heard of, created way before Spark was. And we got full observability, testability, scalability, everything that Spark gives you, autoscaling, whatever you need.
And we're using Delta format, perfect for our Delta Lake, for our lakehouse. Safe, the threads are safe. We we're walking. We're reading our microb badges, keeping track of state, keeping track of our um checkpoints. That easy. All right, guys. I guess do your surveys. Um yeah, thanks. That was the demo from Matra.

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.