Skip to content

Instantly share code, notes, and snippets.

@yusufsyaifudin
Created August 11, 2018 14:04
Show Gist options
  • Save yusufsyaifudin/cd83ad96578ae75dee4733ec206da1fb to your computer and use it in GitHub Desktop.
Save yusufsyaifudin/cd83ad96578ae75dee4733ec206da1fb to your computer and use it in GitHub Desktop.
Create connection into Kafka using Golang
package kafka
import (
"time"
"github.com/segmentio/kafka-go"
"github.com/segmentio/kafka-go/snappy"
)
var writer *kafka.Writer
func Configure(kafkaBrokerUrls []string, clientId string, topic string) (w *kafka.Writer, err error) {
dialer := &kafka.Dialer{
Timeout: 10 * time.Second,
ClientID: clientId,
}
config := kafka.WriterConfig{
Brokers: kafkaBrokerUrls,
Topic: topic,
Balancer: &kafka.LeastBytes{},
Dialer: dialer,
WriteTimeout: 10 * time.Second,
ReadTimeout: 10 * time.Second,
CompressionCodec: snappy.NewCompressionCodec(),
}
w = kafka.NewWriter(config)
writer = w
return w, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment