kafka

package module
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: Unlicense Imports: 3 Imported by: 0

README

kafka-go

This project is a simple kafka go client that wraps the github.com/segmentio/kafka-go library. It aims to provide a simplified interface for producing and consuming messages using kafka go.

Mapping Producer and Consumer Topics

To effectively produce and consume messages using kafka, it's important to correctly map producer and consumer topics.

Follow these steps to map topics:

  1. Create a new kafka topic for your producer using the kafka command-line tool or an admin library like kafka.apache.org/quickstart or set auto_create_topic to true in .confg.toml.

Check github.com/0xanonymeow/kafka-go/.config.toml.example

  1. Update your KAFKA_GO_CONFIG_PATH in your env to point to the config.toml
  2. In your producer code, specify the key to get the topic name then send a message to kafka along with the value.
k, err := kafka.NewKafka(config)

if err != nil {
	return err
}

c, err := client.NewClient(k, config)

if err != nil {
	return err
}

t, err := utils.GetTopicByKey(config.Kafka.Producer.Topics, "example")

if err != nil {
	return err
}

m := message.Message{
	Topic: t,
	Value: []byte("test"),
}

err = c.Produce(m)

Check github.com/0xanonymeow/kafka-go/example/producer.go

  1. Ensure that, in your consumer code, you create a struct method for your handler, commencing with an uppercase letter, and embed client into your struct. Additional attributes can be added as needed. Always adhere to the function signature, as the reader loop will pass the message through this parameter. Your handler method name should also conform to a specific naming pattern; it must begin with an uppercase letter and end with Handler. Ensure that your method name matches the topic name. For example, if your topic is example, your handler name should be ExampleHandler. If the topic contains a hyphen -, remove the hyphen and concatenate the words. If it contains a dot ., only consider the latter part.
type Consumer struct {
	kafka Client
	handlerSpecificProps map[string]interface{}
}

func (h *Handler) ExampleHandler([]byte) error {}

Check github.com/0xanonymeow/kafka-go/example/consumer.go


By following these steps, you can effectively map producer and consumer topics in your kafka application.

Contributing

Contributions are welcome! If you have any suggestions, bug reports, or feature requests, please open an issue on the GitHub repository.

License

This project is released under the The Unlicense.

Acknowledgements

This project is built on top of the github.com/segmentio/kafka-go library. Special thanks to the authors and contributors of that library for their excellent work.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Produce(message.Message) error
}

type Kafka

type Kafka interface {
	DialContext(context.Context, string, string) (*kafka.Conn, error)
	NewReader([]string) *kafka.Reader
	WriteMessages(context.Context, kafka.Message) error
	ReadMessage(context.Context) (kafka.Message, error)
	FetchMessage(context.Context) (kafka.Message, error)
	CommitMessages(context.Context, kafka.Message) error
	Close() error
}

Directories

Path Synopsis
testings
mock_client
Code generated by MockGen.
Code generated by MockGen.
mock_kafka
Code generated by MockGen.
Code generated by MockGen.

Jump to

Keyboard shortcuts

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