Skip to content

Instantly share code, notes, and snippets.

@samuelorji
Last active July 17, 2018 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelorji/b117631c2eda427a9d79c34af607840e to your computer and use it in GitHub Desktop.
Save samuelorji/b117631c2eda427a9d79c34af607840e to your computer and use it in GitHub Desktop.
A very simple Kafka producer that writes to a given topic without a key
package kafka
import java.util.Properties
import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}
import scala.io.StdIn
object producing extends App{
val properties = new Properties()
properties.setProperty("bootstrap.servers","localhost:9092")
properties.setProperty("key.serializer","org.apache.kafka.common.serialization.StringSerializer")
properties.setProperty("value.serializer","org.apache.kafka.common.serialization.StringSerializer")
properties.setProperty("acks","1")
properties.setProperty("retries","3")
val producer = new KafkaProducer[String,String](properties)
val TOPIC = "test"
var input = ""
while(input != ":q"){
input = StdIn.readLine()
val record = new ProducerRecord[String,String](TOPIC,input)
producer.send(record)
}
producer.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment