zenbook migration
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
module kafka
|
||||
|
||||
go 1.23.1
|
||||
|
||||
require (
|
||||
github.com/twmb/franz-go v1.17.1
|
||||
github.com/twmb/franz-go/pkg/kadm v1.13.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/klauspost/compress v1.17.8 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.21 // indirect
|
||||
github.com/twmb/franz-go/pkg/kmsg v1.8.0 // indirect
|
||||
golang.org/x/crypto v0.23.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
|
||||
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
||||
github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ=
|
||||
github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||
github.com/twmb/franz-go v1.17.1 h1:0LwPsbbJeJ9R91DPUHSEd4su82WJWcTY1Zzbgbg4CeQ=
|
||||
github.com/twmb/franz-go v1.17.1/go.mod h1:NreRdJ2F7dziDY/m6VyspWd6sNxHKXdMZI42UfQ3GXM=
|
||||
github.com/twmb/franz-go/pkg/kadm v1.13.0 h1:bJq4C2ZikUE2jh/wl9MtMTQ/kpmnBgVFh8XMQBEC+60=
|
||||
github.com/twmb/franz-go/pkg/kadm v1.13.0/go.mod h1:VMvpfjz/szpH9WB+vGM+rteTzVv0djyHFimci9qm2C0=
|
||||
github.com/twmb/franz-go/pkg/kmsg v1.8.0 h1:lAQB9Z3aMrIP9qF9288XcFf/ccaSxEitNA1CDTEIeTA=
|
||||
github.com/twmb/franz-go/pkg/kmsg v1.8.0/go.mod h1:HzYEb8G3uu5XevZbtU0dVbkphaKTHk0X68N5ka4q6mU=
|
||||
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||
@@ -0,0 +1,92 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/twmb/franz-go/pkg/kadm"
|
||||
"github.com/twmb/franz-go/pkg/kgo"
|
||||
"github.com/twmb/franz-go/pkg/sasl/plain"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
var password, hostname string
|
||||
var replication int
|
||||
flag.StringVar(&password, "p", "", "password to connect to kafka")
|
||||
flag.StringVar(&hostname, "h", "kafka", "hostname to connect to kafka")
|
||||
flag.IntVar(&replication, "r", 3, "number of replication")
|
||||
flag.Parse()
|
||||
|
||||
seeds := []string{fmt.Sprintf("%s.zerops:9092", hostname)}
|
||||
cl, err := kgo.NewClient(
|
||||
kgo.SeedBrokers(seeds...),
|
||||
kgo.SASL(plain.Auth{
|
||||
User: "zerops",
|
||||
Pass: password,
|
||||
}.AsMechanism()),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer cl.Close()
|
||||
|
||||
adminClient := kadm.NewClient(cl)
|
||||
topic, err := adminClient.ListTopics(ctx, "foo")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// if topic.Error() != nil {
|
||||
// panic(topic.Error())
|
||||
// }
|
||||
|
||||
if !topic.Has("foo") {
|
||||
partitions := int32(6)
|
||||
if replication == 1 {
|
||||
partitions = 1
|
||||
}
|
||||
resp, err := adminClient.CreateTopic(ctx, partitions, int16(replication), nil, "foo")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if resp.Err != nil {
|
||||
panic(resp.Err)
|
||||
}
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
message := time.Now().Format(time.TimeOnly)
|
||||
fmt.Printf("%s ==>\n", message)
|
||||
// Alternatively, ProduceSync exists to synchronously produce a batch of records.
|
||||
record := &kgo.Record{Topic: "foo", Value: []byte(message)}
|
||||
if err := cl.ProduceSync(ctx, record).FirstErr(); err != nil {
|
||||
fmt.Printf("record had a produce error while synchronously producing: %v\n", err)
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}()
|
||||
|
||||
cl.AddConsumeTopics("foo")
|
||||
for {
|
||||
fetches := cl.PollFetches(ctx)
|
||||
if fetches.IsClientClosed() {
|
||||
fmt.Println("closed ")
|
||||
return
|
||||
}
|
||||
if errs := fetches.Errors(); len(errs) > 0 {
|
||||
// All errors are retried internally when fetching, but non-retriable errors are
|
||||
// returned from polls so that users can notice and take action.
|
||||
panic(fmt.Sprint(errs))
|
||||
}
|
||||
// We can iterate through a record iterator...
|
||||
iter := fetches.RecordIter()
|
||||
for !iter.Done() {
|
||||
record := iter.Next()
|
||||
fmt.Printf("==> %s\n", string(record.Value))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user