Search code examples
apache-kafkajnetpcap

Pushing packets captured from an interface to kafka


import kafka.serializer.Encoder
import kafka.utils.VerifiableProperties
import org.jnetpcap.packet.PcapPacket
class PcapEncoder(verifiableProperties: VerifiableProperties) extends Encoder[PcapPacket]  { override def toBytes(customMessage: PcapPacket): Array[Byte] = customMessage.transferStateAndDataFrom(PcapPacket : Array[Byte]) }

`This is the encoder that I have written to encode packets captured using jnetpcap library to pass it to kafka consumer. But I have errors, is this the way to implement encoder ?


Solution

  • Assuming that you have Kafka already installed and running, you would establish a connection to Kafka to a particular topic using one of the producer interfaces (perhaps kafka.javaapi.producer.Producer). Then when you get captured packet, you pass it to the API. Kafka has not problems with raw bytes. When you are done, you close the connection via that API.