github.com/rbisecke/kafka-go@v0.4.27/example_consumergroup_test.go (about) 1 package kafka_test 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 8 "github.com/rbisecke/kafka-go" 9 ) 10 11 func ExampleGeneration_Start_consumerGroupParallelReaders() { 12 group, err := kafka.NewConsumerGroup(kafka.ConsumerGroupConfig{ 13 ID: "my-group", 14 Brokers: []string{"kafka:9092"}, 15 Topics: []string{"my-topic"}, 16 }) 17 if err != nil { 18 fmt.Printf("error creating consumer group: %+v\n", err) 19 os.Exit(1) 20 } 21 defer group.Close() 22 23 for { 24 gen, err := group.Next(context.TODO()) 25 if err != nil { 26 break 27 } 28 29 assignments := gen.Assignments["my-topic"] 30 for _, assignment := range assignments { 31 partition, offset := assignment.ID, assignment.Offset 32 gen.Start(func(ctx context.Context) { 33 // create reader for this partition. 34 reader := kafka.NewReader(kafka.ReaderConfig{ 35 Brokers: []string{"127.0.0.1:9092"}, 36 Topic: "my-topic", 37 Partition: partition, 38 }) 39 defer reader.Close() 40 41 // seek to the last committed offset for this partition. 42 reader.SetOffset(offset) 43 for { 44 msg, err := reader.ReadMessage(ctx) 45 switch err { 46 case kafka.ErrGenerationEnded: 47 // generation has ended. commit offsets. in a real app, 48 // offsets would be committed periodically. 49 gen.CommitOffsets(map[string]map[int]int64{"my-topic": {partition: offset + 1}}) 50 return 51 case nil: 52 fmt.Printf("received message %s/%d/%d : %s\n", msg.Topic, msg.Partition, msg.Offset, string(msg.Value)) 53 offset = msg.Offset 54 default: 55 fmt.Printf("error reading message: %+v\n", err) 56 } 57 } 58 }) 59 } 60 } 61 } 62 63 func ExampleGeneration_CommitOffsets_overwriteOffsets() { 64 group, err := kafka.NewConsumerGroup(kafka.ConsumerGroupConfig{ 65 ID: "my-group", 66 Brokers: []string{"kafka:9092"}, 67 Topics: []string{"my-topic"}, 68 }) 69 if err != nil { 70 fmt.Printf("error creating consumer group: %+v\n", err) 71 os.Exit(1) 72 } 73 defer group.Close() 74 75 gen, err := group.Next(context.TODO()) 76 if err != nil { 77 fmt.Printf("error getting next generation: %+v\n", err) 78 os.Exit(1) 79 } 80 err = gen.CommitOffsets(map[string]map[int]int64{ 81 "my-topic": { 82 0: 123, 83 1: 456, 84 3: 789, 85 }, 86 }) 87 if err != nil { 88 fmt.Printf("error committing offsets next generation: %+v\n", err) 89 os.Exit(1) 90 } 91 }