Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream reading from database using spark streaming

I want to use spark streaming to read data from RDBMS database like mysql.

but I don't know how to do this using JavaStreamingContext

 JavaStreamingContext jssc = new JavaStreamingContext(conf, Durations.milliseconds(500));
DataFrame df = jssc. ??

I search in the internet but I didn't find anything

thank you in advance.

like image 779
h.zak Avatar asked May 09 '26 11:05

h.zak


2 Answers

You cannot do it like that without installing some third party piece of software.
What you CAN do is creating a personalized receiver which does what you want, using the SparkSQL package and the Streaming one combined.
Implement a class extending Receiver and inside do all the connections and querys needed to pull the data from the DB.
I am at work now, so I'll give you a link to see instead of producing the code, sorry:
http://spark.apache.org/docs/latest/streaming-custom-receivers.html
https://medium.com/@anicolaspp/spark-custom-streaming-sources-e7d52da72e80

like image 90
Vale Avatar answered May 12 '26 01:05

Vale


The best possible and reliable solution would be avoid using MySqL at all. when you insert your records to MySQl put them also into Kafka (Kafka producer) by a transaction and then use them in your streaming application.

like image 39
Mahdi Avatar answered May 12 '26 00:05

Mahdi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!