Giter Site home page Giter Site logo

go-kafka-avro's Introduction

go-kafka-avro

A wrapper for Confluent's libraries for Apache Kafka and Schema Registry.

Installation

First install dependencies:

go get github.com/confluentinc/confluent-kafka-go github.com/landoop/schema-registry

To install use go get:

go get github.com/mycujoo/go-kafka-avro/v2

Usage

By default this library would fetch configuration from environment variables. But you can customize everything using options.

Consumer

c, err := kafkaavro.NewConsumer(
    []string{"topic1"},
    func(topic string) interface{} {
        return val{}
    },
    kafkaavro.WithKafkaConfig(&kafka.ConfigMap{
        "bootstrap.servers":        "localhost:29092",
        "security.protocol":        "ssl",
        "socket.keepalive.enable":  true,
        "enable.auto.commit":       false,
        "ssl.key.location":         "/path/to/service.key",
        "ssl.certificate.location": "/path/to/service.cert",
        "ssl.ca.location":          "/path/to/ca.pem",
        "group.id":                 "some-group-id",
        "session.timeout.ms":       6000,
        "auto.offset.reset":        "earliest",
    }),
    kafkaavro.WithSchemaRegistryURL(srURL),
    kafkaavro.WithEventHandler(func(event kafka.Event) {
        log.Println(event)
    }),
)

for {
    msg, err := c.ReadMessage(5000)
    if err != nil {
        log.Println("Error", err)
        continue
    }
    if msg == nil {
        continue
    }
    switch v := msg.Value.(type) {
    case val:
        log.Println(v)
    }
}

Producer

producer, err := kafkaavro.NewProducer(
    "topic",
    `"string"`,
    `{"type": "record", "name": "test", "fields" : [{"name": "val", "type": "int", "default": 0}]}`,
)

Publish message using Produce method:

err = producer.Produce("key", "value", nil)

If you provide deliverChan then call will not be blocking until delivery.

Related

Some code for cached schema registry client was based on https://github.com/dangkaka/go-kafka-avro implementation.

go-kafka-avro's People

Contributors

firstrow avatar radev avatar rgalus avatar rgruchalski-klarrio avatar survivorbat avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.