How can we write streaming micro-batches to multiple destination tables or perform Delta Lake MERGE operations?
foreachBatch(process_batch_fn) executes custom programmatic logic on each micro-batch DataFrame as standard batch data.
Upserting stream data into Lakehouse tables (MERGE INTO) and writing simultaneously to Redis cache and Parquet lake.
def process_batch(batch_df, batch_id):
batch_df.persist()
batch_df.write.mode("append").parquet("s3://lakehouse/silver/events/")
batch_df.unpersist()
query = streaming_df.writeStream.foreachBatch(process_batch).option("checkpointLocation", "/tmp/cp/").start()Practice typing production-grade PySpark code for Custom Micro-Batch Writing with foreachBatch.