Last active
July 17, 2018 13:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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