Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spark Structure Streaming - add a batch column with value from currentBatchId

Currently, we are trying to add a new column to the incoming streaming dataframe with value from currentBatchId of StreamExecution. This is required by our use case.

Is that possible?

like image 910
Hiu Avatar asked Aug 30 '26 07:08

Hiu


1 Answers

You can use the following UDF:

val batchUDF = udf { () =>
  TaskContext.get().getLocalProperty("streaming.sql.batchId").toInt
}

Then: df.withColumn("batch", batchUDF())

like image 167
Aliaksei Avatar answered Sep 02 '26 06:09

Aliaksei