Hi there! I need to do preloading from a Kafka top...
# troubleshooting
k
Hi there! I need to do preloading from a Kafka topic before starting main flink app and pass the result to it. Could you advise how to do it the best way?
l
Hey Konstantin! I think the language you are using is irrelevant, let's get to the concept itself. You could think about writing your code for pre-loading phase (in pseudo-code):
Copy code
kafka = KafkaConsumer(...)
msgs: List<Message> = kafka.consumeTopic(...)
then, obtain a data stream from it with Flink (now, in Java):
Copy code
ds = env.fromCollection(msgs)
Clearly all messages would be in memory πŸ‘ Does this answer your question?
k
Yes, it does. Thank you Iorenzo!
l
No problem Konstantin flink
k
thank you for you answer. I have a further question about it: I would like to use the preloading value as
broadcast value
in
BroadcastProcessFunction
. How can I grantee that broadcast state will be in place before the first element in
processElement
came? I don't see any mention of this
is it possible or not? @lorenzo affetti
I thought about default value of
MapStateDescriptor
, but it can't support default value unfortunally
l
Sorry for the latency @Konstantin - I was away for a week πŸ‘ Sure, you can load your state and broadcast, you can follow this doc. However, thinking about your use case, you could simply:
Copy code
kafka = KafkaConsumer(...)
msgs: ListMessage = kafka.consumeTopic(...)

class MyProcessFunction extends ProcessFunction {
   private ListMessage msgs;

   public MyProcessFunction(ListMessage messages) {
     msgs = messages
   }

   @Override
   void processElement(...) {
     // your logic
   }
}

...

someStream.process(new MyProcessFunction(msgs))

...
At this point you would be 100% sure the process function object gets initialized with the preloaded values before the first element comes. However, you must ensure
Message
is
Serializable
, and you will find a higher initialization time for the serde of the messages.
k
thank you for you answer lorenzo, I really appreciate for it. I also thought about this case, but I little worried about checkpointing/savepointing. If I use private field, will it be restored correctly?
πŸ™ 1
l
Nope, it won't, you need to implement `ChekpointedFunction` and define the behavior you want πŸ‘
πŸ™ 1