kafka

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2021 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

kafka-do

v0.2.0

kafka-do

Go Reference

What

Higher level abstraction for Sarama.

Why

We want to be able to write our kafka applications without making the same things over and over.

Batch Consume
Consume messages as much as you defined.

Batch Consume Priorty
Consume messages as much as you defined by using priority structure.

Chan Consume
Consume messages and streams them to a channel.

Batch Produce
Produce messages as a batch to a topic.

Chan Produce
Read from a channel and produce them to a topic.

Example

For e2e example, check here.

package kafka

import (
	"context"
	"fmt"
	"log"
	"sync"
	"time"

	"github.com/Shopify/sarama"
	kafka "github.com/teamseodo/kafka-do"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	topicName := "kafka-do-testing"

	producer, err := kafka.NewProducer([]string{"127.0.0.1:9094"}, 5)
	if err != nil {
		log.Fatalf("error while creating consumer group, error: %s", err)
	}
	defer producer.Close()

	consumer, err := kafka.NewConsumerGroup([]string{"127.0.0.1:9094"}, topicName)
	if err != nil {
		log.Fatalf("error while creating consumer group, error: %s", err)
	}
	defer consumer.Close()

	messages := [][]byte{ // for testing.
		[]byte("message 1"), []byte("message 2"), []byte("message 3"),
		[]byte("message 1"), []byte("message 2"), []byte("message 3"),
		[]byte("message 1"), []byte("message 2"), []byte("message 3"),
		[]byte("message 1"), []byte("message 2"), []byte("message 3"),
	}

	err = kafka.ProduceBatch(ctx, producer, messages, topicName) // produce messages as a batch.
	if err != nil {
		log.Fatalf("error while writin to Kafka, error: %s", err)
	}

	outChan := make(chan sarama.ConsumerMessage, 1)
	defer close(outChan)

	var wg sync.WaitGroup
	for i := 0; i < 3; i++ {
		wg.Add(1)
		go kafka.ConsumeChan(ctx, &wg, consumer, []string{topicName}, outChan) // consume messages as a chan.
	}

out:
	for {
		select {
		case msg := <-outChan:
			fmt.Printf("message: %s, %s", msg.Timestamp, msg.Value)
		case <-time.After(15 * time.Second): // maximum wait time.
			break out
		}
	}

	cancel()
	wg.Wait()
}

Development

To run tests, start a kafka that runs on ":9094".

go test ./... -v -cover -count=1 -race

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCantProduce = errors.New("cant produce the message")
View Source
var ErrNoBrokers = errors.New("brokers is not defined")
View Source
var ErrNoGroupID = errors.New("groupID is not defined")
View Source
var ErrNotAllowed = errors.New("input is not allowed")
View Source
var ErrNotAllowedType = errors.New("not allowed type")

Functions

func ConsumeBatch

func ConsumeBatch(ctx context.Context, client sarama.ConsumerGroup, topics []string, batchCount int) []sarama.ConsumerMessage

ConsumeBatch takes messages as much as batchCount by using given sarama.ConsumerGroup. Re-tries every 5 seconds until the success. Returns as sarama.ConsumerMessage slice. Runs 1 minutes at most. (if there is no message, time will may be exceed.)

func ConsumeBatchPriority added in v0.1.5

func ConsumeBatchPriority(ctx context.Context, client sarama.ConsumerGroup, topics []string, batchCount int) ([]sarama.ConsumerMessage, string)

ConsumeBatchPriority takes messages by implementing Priority structure. Loop over topics and tries to take message. If there is any message consumed The first item in the "topics" slice is considered the highest topic. Returns the messages when as much as batchCount is arrived or time exceed, (if there is no message on any priority stage, time will may be exceed.)

func ConsumeChan

func ConsumeChan(ctx context.Context, wg *sync.WaitGroup, client sarama.ConsumerGroup, topics []string, outChan chan sarama.ConsumerMessage)

ConsumeChan starts consuming messages and write messages to outChan.

func NewConsumerGroup

func NewConsumerGroup(brokers []string, groupId string) (sarama.ConsumerGroup, error)

NewConsumerGroup returns a sarama.ConsumerGroup.

func NewProducer

func NewProducer(brokers []string, maxMegabytes int) (sarama.SyncProducer, error)

NewProducer returns a sarama.SyncProducer.

func ProduceBatch

func ProduceBatch(ctx context.Context, client sarama.SyncProducer, messages interface{}, topic string) (sarama.ProducerErrors, error)

ProduceBatch produces given data with the client. It supports multi-type. Supported Types:

  • bytesSlice: [][]byte, topic must be set
  • consumerMessages: []sarama.ConsumerMessage, topic must be set
  • producerMessages: []*sarama.ProducerMessage, topic's not needed. Set your topic in the messages.
  • producerErrors: sarama.ProducerErrors, topic isn't needed. we expect topic to be already set in producerError.Msg

func ProduceChan

func ProduceChan(ctx context.Context, wg *sync.WaitGroup, client sarama.SyncProducer, inChan chan interface{}, errChan chan error, topic string)

ProduceChan produces by reading messages from inChan. It supports multi-type. Supported Types:

  • bytesSlice: []byte, topic must be set
  • consumerMessage: sarama.ConsumerMessage, topic must be set
  • producerMessage: *sarama.ProducerMessage, topic's not needed. Set your topic in the messages.

Types

This section is empty.

Jump to

Keyboard shortcuts

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