kafka

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2021 License: MIT Imports: 5 Imported by: 0

README

Kafka

Streamer

package main

import (
	"context"
	"log"

	"github.com/spy16/pkg/kafka"
)

func main() {
	ks := kafka.Streamer{
		Workers:       10,
		Topic:         "events",
		Servers:       "localhost:9092",
		ConsumerGroup: "pkg-simple-consumer",
		Apply: func(ctx context.Context, key, val []byte) error {
			log.Printf("%s = %s", string(key), string(val))
			return nil
		},
	}

	if err := ks.Run(context.Background()); err != nil {
		log.Fatalf("streamer exited: %v", err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApplyFn

type ApplyFn func(ctx context.Context, key, val []byte) error

ApplyFn can be set on a streamer to process each message.

type Streamer

type Streamer struct {
	log.Logger

	Apply         ApplyFn `json:"-"`
	Servers       string  `json:"servers"`
	Topic         string  `json:"topic"`
	Workers       int     `json:"workers"`
	StartOffset   string  `json:"start_offset"`
	ConsumerGroup string  `json:"consumer_group"`
}

Streamer streams kafka messages, decodes and applies some function to it.

func (*Streamer) Run

func (st *Streamer) Run(ctx context.Context) error

Run spans the workers that consume from kafka and apply the configured function to each message.

Jump to

Keyboard shortcuts

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