Skip to content

Instantly share code, notes, and snippets.

@koushikmln
Created May 19, 2018 08:25
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 koushikmln/c71c55cc2af1719b0a01f63d35bc7cc8 to your computer and use it in GitHub Desktop.
Save koushikmln/c71c55cc2af1719b0a01f63d35bc7cc8 to your computer and use it in GitHub Desktop.
Kafka Producer Example using Scala
import java.util.Properties
import com.typesafe.config.ConfigFactory
import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord}
object KafkaProducerExample {
def main(args: Array[String]): Unit = {
val conf = ConfigFactory.load
val envProps = conf.getConfig(args(0))
val props = new Properties()
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, envProps.getString("bootstrap.server"))
props.put(ProducerConfig.CLIENT_ID_CONFIG, "ScalaProducerExample")
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer")
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer")
val producer = new KafkaProducer[String, String](props)
val data = new ProducerRecord[String, String]("Kafka-Testing", "Key", "Value")
producer.send(data)
producer.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment