Search code examples
javahadoopapache-kafkaconsumerproducer

Is it possible to write kafka consumer received output to a file using java


I have a Kafka Producer code written in java that writes kafka messages. And a consumer code that receives these messages.

Is it possible to write theses received messages by consumer to any text file in java.


Solution

  • Thanks Guys,

    I am able to achieve it. Once the data is received at the consumer side, then it's just a common java code you have to write.

    Below is the line in ode that prints the message to the console.

    System.out.println(String.valueOf(messageAndOffset.offset()) + ": " + new String(bytes, "UTF-8"));
    

    You can store all the message to the String and print all at a time to the file.

    System.out.println(String.valueOf(messageAndOffset.offset()) + ": " + new String(bytes, "UTF-8"));
    completMessage += new String(bytes, "UTF-8")+"\n";
    

    new String(bytes, "UTF-8")+"\n"; contains actual message.

    At last print all messages to file.

    writeDataToFile(completMessage);
    

    writeDataToFile contains simple java code to write a string to file.

    Thank you.