kafka

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: MIT Imports: 3 Imported by: 0

README

go-utils Kafka

The go-utils Kafka package provides handy methods to produce and read messages in Kafka topics.

Installation

To use the log package you must import the package.

import "github.com/eliona-smart-building-assistant/go-utils/kafka"

Optionally you can define an environment variable named BROKERS which sets the Kafka bootstrap servers.

export BROKERS=10.10.100.1:29092,192.168.178.1:9092

Usage

After installation, you can produce messages in Kafka topics.

import "github.com/eliona-smart-building-assistant/go-utils/kafka"

For example, you can push a temperature object to the climate topic. You have to design a new Producer and produce the temperature to the topic.

type Temperature struct {
    Value int
    Unit  string
}
producer := kafka.NewProducer()
defer producer.Close()
temperature := Temperature{Value: 24, Unit: "Celsius"}
_ = kafka.Produce(producer, "climate", temperature)

To read messages from a topic have to define a new consumer and subscribe a topic. After this, you can read the temperatures through a channel.

consumer := kafka.NewConsumer()
defer consumer.Close()
kafka.Subscribe(consumer, "climate")
temperatures := make(chan Temperature)
go kafka.Read(consumer, temperatures)

for temperature := range temperatures {
    fmt.Printf("Temperature is: %d %s", temperature.Value, temperature.Unit)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Brokers

func Brokers() string

Brokers reads the kafka brokers from environment variable BROKERS

Types

This section is empty.

Jump to

Keyboard shortcuts

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