kafka

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 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.1.2

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.

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

package kafka

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

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

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

	producer, err := do.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 := do.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"),
	}

	err = 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)
	go do.ConsumeChan(ctx, 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
		}
	}
}

TODOs

  • Add an e2e example repo.
  • Add priority usage support to consuming.

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 BytesSliceToConsumerMessages

func BytesSliceToConsumerMessages(inputs [][]byte) []sarama.ConsumerMessage

BytesSliceToConsumerMessages converts given bytes to consumer messages.

func BytesSliceToProducerMessages

func BytesSliceToProducerMessages(inputs [][]byte, topic string) []*sarama.ProducerMessage

BytesSliceToProducerMessages converts given bytes to producer messages.

func BytesToConsumerMessage

func BytesToConsumerMessage(i []byte) sarama.ConsumerMessage

BytesToConsumerMessage converts given bytes to consumer message.

func BytesToProducerMessage

func BytesToProducerMessage(i []byte, topic string) *sarama.ProducerMessage

BytesToProducerMessage converts given bytes to producer message.

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 ConsumeChan

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

ConsumeChan starts consuming messages and write messages to outChan.

func ConsumerMessageToProducerMessage

func ConsumerMessageToProducerMessage(m sarama.ConsumerMessage, topic string) *sarama.ProducerMessage

func ConsumerMessagesToBytesSlice

func ConsumerMessagesToBytesSlice(messages []sarama.ConsumerMessage) [][]byte

ConsumerMessagesToBytesSlice converts given messages to bytes.

func ConsumerMessagesToProducerMessages

func ConsumerMessagesToProducerMessages(messages []sarama.ConsumerMessage, topic string) []*sarama.ProducerMessage

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) 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.

func ProduceChan

func ProduceChan(ctx context.Context, 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