Skip to content

Instantly share code, notes, and snippets.

@abhirockzz
Created November 30, 2018 11:47
Show Gist options
  • Save abhirockzz/97b235dc8e0bbab7c45f0b226715548b to your computer and use it in GitHub Desktop.
Save abhirockzz/97b235dc8e0bbab7c45f0b226715548b to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/confluentinc/confluent-kafka-go/kafka"
)
func main() {
kafkaBroker := "localhost:9092"
p, _ := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": kafkaBroker})
topic := "bar"
partition := kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny}
msg := &kafka.Message{TopicPartition: partition, Key: []byte("hello"), Value: []byte("world")}
p.Produce(msg, nil)
kafkaEvt := <-p.Events()
switch kafkaEvt.(type) {
case kafka.Error:
e := kafkaEvt.(kafka.Error)
fmt.Println("error - " + e.String())
case *kafka.Message:
m := kafkaEvt.(*kafka.Message)
if m.TopicPartition.Error != nil {
fmt.Printf("Delivery failed: %v\n", m.TopicPartition.Error)
} else {
fmt.Printf("Delivered message to topic %s [%d] at offset %v\n",
*m.TopicPartition.Topic, m.TopicPartition.Partition, m.TopicPartition.Offset)
}
default:
fmt.Println("got event from kafka ", kafkaEvt.String())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment