在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:confluentinc/confluent-kafka-go开源软件地址:https://github.com/confluentinc/confluent-kafka-go开源编程语言:Go 43.0%开源软件介绍:Confluent's Golang Client for Apache KafkaTMconfluent-kafka-go is Confluent's Golang client for Apache Kafka and the Confluent Platform. Features:
The Golang bindings provides a high-level Producer and Consumer with support for the balanced consumer groups of Apache Kafka 0.9 and above. See the API documentation for more information. For a step-by-step guide on using the client see Getting Started with Apache Kafka and Golang. ExamplesHigh-level balanced consumer import (
"fmt"
"github.com/confluentinc/confluent-kafka-go/kafka"
)
func main() {
c, err := kafka.NewConsumer(&kafka.ConfigMap{
"bootstrap.servers": "localhost",
"group.id": "myGroup",
"auto.offset.reset": "earliest",
})
if err != nil {
panic(err)
}
c.SubscribeTopics([]string{"myTopic", "^aRegex.*[Tt]opic"}, nil)
for {
msg, err := c.ReadMessage(-1)
if err == nil {
fmt.Printf("Message on %s: %s\n", msg.TopicPartition, string(msg.Value))
} else {
// The client will automatically try to recover from all errors.
fmt.Printf("Consumer error: %v (%v)\n", err, msg)
}
}
c.Close()
} Producer import (
"fmt"
"github.com/confluentinc/confluent-kafka-go/kafka"
)
func main() {
p, err := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": "localhost"})
if err != nil {
panic(err)
}
defer p.Close()
// Delivery report handler for produced messages
go func() {
for e := range p.Events() {
switch ev := e.(type) {
case *kafka.Message:
if ev.TopicPartition.Error != nil {
fmt.Printf("Delivery failed: %v\n", ev.TopicPartition)
} else {
fmt.Printf("Delivered message to %v\n", ev.TopicPartition)
}
}
}
}()
// Produce messages to topic (asynchronously)
topic := "myTopic"
for _, word := range []string{"Welcome", "to", "the", "Confluent", "Kafka", "Golang", "client"} {
p.Produce(&kafka.Message{
TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny},
Value: []byte(word),
}, nil)
}
// Wait for message deliveries before shutting down
p.Flush(15 * 1000)
} More elaborate examples are available in the examples directory, including how to configure the Go client for use with Confluent Cloud. Getting StartedSupports Go 1.11+ and librdkafka 1.6.0+. Using Go ModulesStarting with Go 1.13, you can use Go Modules to install confluent-kafka-go. Import the import "github.com/confluentinc/confluent-kafka-go/kafka" Build your project: go build ./... If you are building for Alpine Linux (musl), go build -tags musl ./... A dependency to the latest stable version of confluent-kafka-go should be automatically added to
your Install the clientManual install: go get -u github.com/confluentinc/confluent-kafka-go/kafka Golang import: import "github.com/confluentinc/confluent-kafka-go/kafka" librdkafkaPrebuilt librdkafka binaries are included with the Go client and librdkafka does not need to be installed separately on the build or target system. The following platforms are supported by the prebuilt librdkafka binaries:
When building your application for Alpine Linux (musl libc) you must pass
If GSSAPI/Kerberos authentication support is required you will need
to install librdkafka separately, see the Installing librdkafka chapter
below, and then build your Go application with Installing librdkafkaIf the bundled librdkafka build is not supported on your platform, or you need a librdkafka with GSSAPI/Kerberos support, you must install librdkafka manually on the build and target system using one of the following alternatives:
Build from source:
After installing librdkafka you will need to build your Go application
with Note: If you use the confluent-kafka-go requires librdkafka v1.6.0 or later. API StrandsThere are two main API strands: function and channel-based. Function-Based ConsumerMessages, errors and events are polled through the Pros:
Cons:
Channel-Based Consumer (deprecated)Deprecated: The channel-based consumer is deprecated due to the channel issues mentioned below. Use the function-based consumer. Messages, errors and events are posted on the Pros:
Cons:
See examples/consumer_channel_example Channel-Based ProducerApplication writes messages to the Pros:
Cons:
See examples/producer_channel_example Function-Based ProducerApplication calls Pros:
Cons:
LicenseKAFKA is a registered trademark of The Apache Software Foundation and has been licensed for use by confluent-kafka-go. confluent-kafka-go has no affiliation with and is not endorsed by The Apache Software Foundation. Developer NotesSee kafka/README Contributions to the code, examples, documentation, et.al, are very much appreciated. Make your changes, run Confluent CloudFor a step-by-step guide on using the Golang client with Confluent Cloud see Getting Started with Apache Kafka and Golang on Confluent Developer. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论