github.com/confluentinc/confluent-kafka-go@v1.9.2/README.md (about)

     1  Confluent's Golang Client for Apache Kafka<sup>TM</sup>
     2  =====================================================
     3  
     4  **confluent-kafka-go** is Confluent's Golang client for [Apache Kafka](http://kafka.apache.org/) and the
     5  [Confluent Platform](https://www.confluent.io/product/compare/).
     6  
     7  
     8  Features:
     9  
    10  - **High performance** - confluent-kafka-go is a lightweight wrapper around
    11  [librdkafka](https://github.com/edenhill/librdkafka), a finely tuned C
    12  client.
    13  
    14  - **Reliability** - There are a lot of details to get right when writing an Apache Kafka
    15  client. We get them right in one place (librdkafka) and leverage this work
    16  across all of our clients (also [confluent-kafka-python](https://github.com/confluentinc/confluent-kafka-python)
    17  and [confluent-kafka-dotnet](https://github.com/confluentinc/confluent-kafka-dotnet)).
    18  
    19  - **Supported** - Commercial support is offered by
    20  [Confluent](https://confluent.io/).
    21  
    22  - **Future proof** - Confluent, founded by the
    23  creators of Kafka, is building a [streaming platform](https://www.confluent.io/product/compare/)
    24  with Apache Kafka at its core. It's high priority for us that client features keep
    25  pace with core Apache Kafka and components of the [Confluent Platform](https://www.confluent.io/product/compare/).
    26  
    27  
    28  The Golang bindings provides a high-level Producer and Consumer with support
    29  for the balanced consumer groups of Apache Kafka 0.9 and above.
    30  
    31  See the [API documentation](http://docs.confluent.io/current/clients/confluent-kafka-go/index.html) for more information.
    32  
    33  For a step-by-step guide on using the client see [Getting Started with Apache Kafka and Golang](https://developer.confluent.io/get-started/go/).
    34  
    35  
    36  
    37  Examples
    38  ========
    39  
    40  High-level balanced consumer
    41  
    42  ```golang
    43  import (
    44  	"fmt"
    45  	"github.com/confluentinc/confluent-kafka-go/kafka"
    46  )
    47  
    48  func main() {
    49  
    50  	c, err := kafka.NewConsumer(&kafka.ConfigMap{
    51  		"bootstrap.servers": "localhost",
    52  		"group.id":          "myGroup",
    53  		"auto.offset.reset": "earliest",
    54  	})
    55  
    56  	if err != nil {
    57  		panic(err)
    58  	}
    59  
    60  	c.SubscribeTopics([]string{"myTopic", "^aRegex.*[Tt]opic"}, nil)
    61  
    62  	for {
    63  		msg, err := c.ReadMessage(-1)
    64  		if err == nil {
    65  			fmt.Printf("Message on %s: %s\n", msg.TopicPartition, string(msg.Value))
    66  		} else {
    67  			// The client will automatically try to recover from all errors.
    68  			fmt.Printf("Consumer error: %v (%v)\n", err, msg)
    69  		}
    70  	}
    71  
    72  	c.Close()
    73  }
    74  ```
    75  
    76  Producer
    77  
    78  ```golang
    79  import (
    80  	"fmt"
    81  	"github.com/confluentinc/confluent-kafka-go/kafka"
    82  )
    83  
    84  func main() {
    85  
    86  	p, err := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": "localhost"})
    87  	if err != nil {
    88  		panic(err)
    89  	}
    90  
    91  	defer p.Close()
    92  
    93  	// Delivery report handler for produced messages
    94  	go func() {
    95  		for e := range p.Events() {
    96  			switch ev := e.(type) {
    97  			case *kafka.Message:
    98  				if ev.TopicPartition.Error != nil {
    99  					fmt.Printf("Delivery failed: %v\n", ev.TopicPartition)
   100  				} else {
   101  					fmt.Printf("Delivered message to %v\n", ev.TopicPartition)
   102  				}
   103  			}
   104  		}
   105  	}()
   106  
   107  	// Produce messages to topic (asynchronously)
   108  	topic := "myTopic"
   109  	for _, word := range []string{"Welcome", "to", "the", "Confluent", "Kafka", "Golang", "client"} {
   110  		p.Produce(&kafka.Message{
   111  			TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny},
   112  			Value:          []byte(word),
   113  		}, nil)
   114  	}
   115  
   116  	// Wait for message deliveries before shutting down
   117  	p.Flush(15 * 1000)
   118  }
   119  ```
   120  
   121  More elaborate examples are available in the [examples](examples) directory,
   122  including [how to configure](examples/confluent_cloud_example) the Go client
   123  for use with [Confluent Cloud](https://www.confluent.io/confluent-cloud/).
   124  
   125  
   126  Getting Started
   127  ===============
   128  
   129  Supports Go 1.11+ and librdkafka 1.9.0+.
   130  
   131  Using Go Modules
   132  ----------------
   133  
   134  Starting with Go 1.13, you can use [Go Modules](https://blog.golang.org/using-go-modules) to install
   135  confluent-kafka-go.
   136  
   137  Import the `kafka` package from GitHub in your code:
   138  
   139  ```golang
   140  import "github.com/confluentinc/confluent-kafka-go/kafka"
   141  ```
   142  
   143  Build your project:
   144  
   145  ```bash
   146  go build ./...
   147  ```
   148  
   149  If you are building for Alpine Linux (musl), `-tags musl` must be specified.
   150  
   151  ```bash
   152  go build -tags musl ./...
   153  ```
   154  
   155  A dependency to the latest stable version of confluent-kafka-go should be automatically added to
   156  your `go.mod` file.
   157  
   158  Install the client
   159  ------------------
   160  
   161  Manual install:
   162  ```bash
   163  go get -u github.com/confluentinc/confluent-kafka-go/kafka
   164  ```
   165  
   166  Golang import:
   167  ```golang
   168  import "github.com/confluentinc/confluent-kafka-go/kafka"
   169  ```
   170  
   171  librdkafka
   172  ----------
   173  
   174  Prebuilt librdkafka binaries are included with the Go client and librdkafka
   175  does not need to be installed separately on the build or target system.
   176  The following platforms are supported by the prebuilt librdkafka binaries:
   177  
   178   * Mac OSX x64
   179   * glibc-based Linux x64 (e.g., RedHat, Debian, CentOS, Ubuntu, etc) - without GSSAPI/Kerberos support
   180   - musl-based Linux 64 (Alpine) - without GSSAPI/Kerberos support
   181  
   182  When building your application for Alpine Linux (musl libc) you must pass
   183  `-tags musl` to `go get`, `go build`, etc.
   184  
   185  `CGO_ENABLED` must NOT be set to `0` since the Go client is based on the
   186  C library librdkafka.
   187  
   188  If GSSAPI/Kerberos authentication support is required you will need
   189  to install librdkafka separately, see the **Installing librdkafka** chapter
   190  below, and then build your Go application with `-tags dynamic`.
   191  
   192  Installing librdkafka
   193  ---------------------
   194  
   195  If the bundled librdkafka build is not supported on your platform, or you
   196  need a librdkafka with GSSAPI/Kerberos support, you must install librdkafka
   197  manually on the build and target system using one of the following alternatives:
   198  
   199  - For Debian and Ubuntu based distros, install `librdkafka-dev` from the standard
   200  repositories or using [Confluent's Deb repository](http://docs.confluent.io/current/installation.html#installation-apt).
   201  - For Redhat based distros, install `librdkafka-devel` using [Confluent's YUM repository](http://docs.confluent.io/current/installation.html#rpm-packages-via-yum).
   202  - For MacOS X, install `librdkafka` from Homebrew. You may also need to brew install pkg-config if you don't already have it: `brew install librdkafka pkg-config`.
   203  - For Alpine: `apk add librdkafka-dev pkgconf`
   204  - confluent-kafka-go is not supported on Windows.
   205  - For source builds, see instructions below.
   206  
   207  Build from source:
   208  
   209      git clone https://github.com/edenhill/librdkafka.git
   210      cd librdkafka
   211      ./configure
   212      make
   213      sudo make install
   214  
   215  After installing librdkafka you will need to build your Go application
   216  with `-tags dynamic`.
   217  
   218  **Note:** If you use the `master` branch of the Go client, then you need to use
   219            the `master` branch of librdkafka.
   220  
   221  **confluent-kafka-go requires librdkafka v1.9.0 or later.**
   222  
   223  
   224  API Strands
   225  ===========
   226  
   227  The recommended API strand is the Function-Based one,
   228  the Channel-Based one is documented in [examples/legacy](examples/legacy).
   229  
   230  Function-Based Consumer
   231  -----------------------
   232  
   233  Messages, errors and events are polled through the `consumer.Poll()` function.
   234  
   235  It has direct mapping to underlying librdkafka functionality.
   236  
   237  See [examples/consumer_example](examples/consumer_example)
   238  
   239  Function-Based Producer
   240  -----------------------
   241  
   242  Application calls `producer.Produce()` to produce messages.
   243  Delivery reports are emitted on the `producer.Events()` or specified private channel.
   244  
   245  _Warnings_
   246  
   247   * `Produce()` is a non-blocking call, if the internal librdkafka queue is full
   248     the call will fail and can be retried.
   249  
   250  See [examples/producer_example](examples/producer_example)
   251  
   252  License
   253  =======
   254  
   255  [Apache License v2.0](http://www.apache.org/licenses/LICENSE-2.0)
   256  
   257  KAFKA is a registered trademark of The Apache Software Foundation and has been licensed for use
   258  by confluent-kafka-go. confluent-kafka-go has no affiliation with and is not endorsed by The Apache
   259  Software Foundation.
   260  
   261  Developer Notes
   262  ===============
   263  
   264  See [kafka/README](kafka/README.md)
   265  
   266  Contributions to the code, examples, documentation, et.al, are very much appreciated.
   267  
   268  Make your changes, run `gofmt`, tests, etc, push your branch, create a PR, and [sign the CLA](http://clabot.confluent.io/cla).
   269  
   270  Confluent Cloud
   271  ===============
   272  
   273  For a step-by-step guide on using the Golang client with Confluent Cloud see [Getting Started with Apache Kafka and Golang](https://developer.confluent.io/get-started/go/) on [Confluent Developer](https://developer.confluent.io/).