Created
August 11, 2018 14:04
-
-
Save yusufsyaifudin/cd83ad96578ae75dee4733ec206da1fb to your computer and use it in GitHub Desktop.
Create connection into Kafka using Golang
This file contains hidden or 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 ( | |
"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