raghav tandon
04/23/2023, 7:18 AMDataStream[Event] -> Window -> ProcessWindowFunction -> Datastream[Event2] -> Kafka Sink
Everything works fine but i am seeing in kafka some value is getting shuffled, i.e some other value is coming which is not intended.
I have a log before out.collect() and things are getting printed correctly in that log line.
val userIds = events
.keyBy(e => e.client)
.window(TumblingProcessingTimeWindows.of(Time.seconds(1)))
.trigger(
PurgingTrigger.of(ProcessingTimeTrigger.create())
)
.process(userIdFetcher)
.setParallelism(1)
.uid(userIdFetcher.getClass().getSimpleName())
final class UserIdFetcher(config: UserIdFetcherConfig, failedOutputTag: OutputTag[ReadErrorEvent])
extends ProcessWindowFunction[WriteSuccessEvent, Diff, String, TimeWindow]
with Serializable{
elements.groupBy[String](update => update.command.id.get).foreach {
case (id, updates) =>
retryableProcess(
id,
context,
MongoBulkQuery(
key,
"User",
updates.map(update => update.command.body.identifiers).toList,
List(UserFields.Id)
),
out
)
}
def retryableProcess(
id: String,
context: Context,
query: MongoBulkQuery,
out: Collector[Diff],
attemptNumber: Int = 0
): Unit = {
mongoClient.client(query.client, query.collection).findUserIDs(query).onComplete {
case Success(values) =>
values
.map(user => user.getObjectId(MongoFields.Id).toHexString())
.grouped(config.maxBatchSize)
.foreach(userIdBatch => {
val diff = Diff(query.client, id, userIdBatch.toList)
out.collect(diff)
})
}
}
}
final case class Diff(
@JsonProperty("dbName") client: String,
@JsonProperty("id") collection: String,
@JsonProperty("add") add: List[String]
)
But in Kafka this value is getting changed randomly of a different id which is also getting processed concurrently.raghav tandon
04/23/2023, 3:12 PMSharon Xie
04/23/2023, 8:46 PMraghav tandon
04/24/2023, 12:00 AMraghav tandon
04/24/2023, 12:02 AMAkhlaq Malik
04/24/2023, 10:09 AMelements.groupBy call is it called just once from the instantiation of the class?raghav tandon
04/24/2023, 10:53 AMDiff(DB1,ID1,List('X'))','timestamp':'2023-04-20T12:39:08.044854445Z'}"
But in Kafka it is getting pushed as (see how ID1 is changing to ID2)
Diff(DB1,ID2,List('X'))','timestamp':'2023-04-20T12:39:08.044854445Z'}"
elements.groupBy it will be called everytime ProcessWindowFunction is called which is every TumblingWindowAkhlaq Malik
04/24/2023, 11:01 AMdiff before calling out.collect(diff) does it contain ID2 or ID1?raghav tandon
04/24/2023, 11:01 AMAkhlaq Malik
04/24/2023, 11:03 AM.process() function?raghav tandon
04/24/2023, 11:04 AMraghav tandon
04/24/2023, 11:05 AMout.collect(diff) , diff is correct but after that in kafka diff has a different id and only one field is getting changed and other fields are fineAkhlaq Malik
04/24/2023, 11:05 AMraghav tandon
04/24/2023, 11:05 AMraghav tandon
04/24/2023, 11:06 AMAkhlaq Malik
04/24/2023, 11:07 AMraghav tandon
04/24/2023, 12:04 PMnew ProducerRecord(topic, objectMapper.writeValueAsBytes(key), objectMapper.writeValueAsBytes(value))raghav tandon
04/24/2023, 12:05 PMids
.sinkTo(sinkFunction)
.setParallelism(1)Akhlaq Malik
04/24/2023, 12:21 PMAkhlaq Malik
04/24/2023, 12:23 PMids
.map(mapper.writeValueAsString_conversion_function)
.sink(sinkFunction)raghav tandon
04/24/2023, 1:19 PM