-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.go
64 lines (52 loc) · 1.84 KB
/
consumer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"context"
"fmt"
"log"
"github.com/IBM/sarama"
)
func Consumer() {
// Kafka broker address
broker := "localhost:9093" // Change to your Kafka broker address
topic := "test_topic" // Topic to consume messages from
// Set up the Kafka consumer configuration
config := sarama.NewConfig()
config.Consumer.Return.Errors = true
// Create a new consumer group
consumerGroup, err := sarama.NewConsumerGroup([]string{broker}, "consumer-group1", config)
if err != nil {
log.Fatalf("Error creating consumer group: %v", err)
}
defer consumerGroup.Close()
// Create a new consumer handler
handler := &ConsumerGroupHandler{}
// Start consuming messages
for {
if err := consumerGroup.Consume(context.Background(), []string{topic}, handler); err != nil {
log.Fatalf("Error in consumer group: %v", err)
}
}
}
// ConsumerGroupHandler implements the sarama.ConsumerGroupHandler interface
type ConsumerGroupHandler struct{}
// Setup is called before consuming messages (can be used for setup tasks)
func (ConsumerGroupHandler) Setup(sarama.ConsumerGroupSession) error {
fmt.Println("Consumer group session setup.")
return nil
}
// Cleanup is called after consuming messages (can be used for cleanup tasks)
func (ConsumerGroupHandler) Cleanup(sarama.ConsumerGroupSession) error {
fmt.Println("Consumer group session cleanup.")
return nil
}
// ConsumeClaim is called when a new message is consumed
func (ConsumerGroupHandler) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
// Iterate over messages in the claim
for message := range claim.Messages() {
// Print the received message
fmt.Printf("Consumed message: %s\n, Partition: %d, Offset: %d", string(message.Value), message.Partition, message.Offset)
// Mark the message as processed
session.MarkMessage(message, "")
}
return nil
}