kafka

module
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2025 License: MIT

README

Kafka wrapper on top of kafka-go

go get -u github.com/kazhuravlev/kafka

Consumer

Example with handling messages in json and protobuf format (depends on message header enc). You can write own algorithm to detect which decoder should used or use strict decoder like new(JSONDecoder).

package main

import (
	"context"
	"fmt"
	"github.com/kazhuravlev/kafka/consumer"
)

type Payload struct {
	UserID int64
}

func main() {
	cons, err := consumer.New[Payload](consumer.NewOptions(
		consumer.WithBrokers([]string{"127.0.0.1:9092"}),
		consumer.WithDecoder(&consumer.HeaderDependantDecoder{
			HeaderName: "enc",
			Decoders: map[string]consumer.IDecoder{
				"json":  new(consumer.JSONDecoder),
				"proto": new(consumer.ProtoJSONDecoder),
			},
			DefaultDecoder: new(consumer.AlwaysFailDecoder),
		}),
	))
	if err != nil {
		panic(err)
	}

	if err := cons.Run(context.TODO()); err != nil {
		panic(err)
	}

	for m := range cons.C() {
		fmt.Println(m)
	}
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL