forked from twmb/franz-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (59 loc) · 2.03 KB
/
main.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
65
66
67
68
69
70
71
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"github.com/twmb/franz-go/pkg/kadm"
"github.com/twmb/franz-go/pkg/kgo"
)
var (
seedBrokers = flag.String("brokers", "localhost:9092", "comma delimited list of seed brokers")
topic = flag.String("topic", "", "topic to consume from")
group = flag.String("group", "", "group to consume within")
)
func die(msg string, args ...any) {
fmt.Fprintf(os.Stderr, msg, args...)
os.Exit(1)
}
func main() {
flag.Parse()
seeds := kgo.SeedBrokers(strings.Split(*seedBrokers, ",")...)
var adm *kadm.Client
{
cl, err := kgo.NewClient(seeds)
if err != nil {
die("unable to create admin client: %v", err)
}
adm = kadm.NewClient(cl)
}
// With the admin client, we can either FetchOffsets to fetch strictly
// the prior committed offsets, or FetchOffsetsForTopics to fetch
// offsets for topics and have -1 offset defaults for topics that are
// not yet committed. We use FetchOffsetsForTopics here.
os, err := adm.FetchOffsetsForTopics(context.Background(), *group, *topic)
if err != nil {
die("unable to fetch group offsets: %v", err)
}
cl, err := kgo.NewClient(seeds, kgo.ConsumePartitions(os.KOffsets()))
if err != nil {
die("unable to create client: %v", err)
}
defer cl.Close()
fmt.Println("Waiting for one record...")
fs := cl.PollRecords(context.Background(), 1)
// kadm has two offset committing functions: CommitOffsets and
// CommitAllOffsets. The former allows you to check per-offset error
// codes, the latter returns the first error of any in the offsets.
//
// We use the latter here because we are only committing one offset,
// and even if we committed more, we do not care about partial failed
// commits. Use the former if you care to check per-offset commit
// errors.
if err := adm.CommitAllOffsets(context.Background(), *group, kadm.OffsetsFromFetches(fs)); err != nil {
die("unable to commit offsets: %v", err)
}
r := fs.Records()[0]
fmt.Printf("Successfully committed record on partition %d at offset %d!\n", r.Partition, r.Offset)
}