-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a88e0f3
commit edf5c53
Showing
4 changed files
with
378 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Copyright 2023 Confluent Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// List Offsets example | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/confluentinc/confluent-kafka-go/v2/kafka" | ||
) | ||
|
||
func main() { | ||
|
||
if len(os.Args) < 2 { | ||
fmt.Fprintf(os.Stderr, | ||
"Usage: %s <bootstrap-servers> <topicname> <partition> <EARLIEST/LATEST/MAXTIMESTAMP/TIMESTAMP t1> ..\n", os.Args[0]) | ||
os.Exit(1) | ||
} | ||
|
||
bootstrapServers := os.Args[1] | ||
|
||
args := len(os.Args) | ||
i := 2 | ||
topicPartitionOffsets := make(map[kafka.TopicPartition]kafka.OffsetSpec) | ||
for i < args { | ||
topicName := os.Args[i] | ||
partition, err := strconv.Atoi(os.Args[i+1]) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Invalid partition: %s\n", err) | ||
os.Exit(1) | ||
} | ||
tp := kafka.TopicPartition{Topic: &topicName, Partition: int32(partition)} | ||
if os.Args[i+2] == "EARLIEST" { | ||
topicPartitionOffsets[tp] = kafka.EarliestOffsetSpec | ||
} else if os.Args[i+2] == "LATEST" { | ||
topicPartitionOffsets[tp] = kafka.LatestOffsetSpec | ||
} else if os.Args[i+2] == "MAXTIMESTAMP" { | ||
topicPartitionOffsets[tp] = kafka.MaxTimestampOffsetSpec | ||
} else if os.Args[i+2] == "TIMESTAMP" { | ||
timestamp, timestampErr := strconv.Atoi(os.Args[i+3]) | ||
if timestampErr != nil { | ||
fmt.Fprintf(os.Stderr, "Invalid timestamp: %s\n", timestampErr) | ||
os.Exit(1) | ||
} | ||
topicPartitionOffsets[tp] = kafka.NewOffsetSpecOfTimestamp(int64(timestamp)) | ||
i = i + 1 | ||
} else { | ||
fmt.Fprintf(os.Stderr, "Invalid OffsetSpec.\n") | ||
os.Exit(1) | ||
} | ||
i = i + 3 | ||
} | ||
|
||
// Create a new AdminClient. | ||
// AdminClient can also be instantiated using an existing | ||
// Producer or Consumer instance, see NewAdminClientFromProducer and | ||
// NewAdminClientFromConsumer. | ||
a, err := kafka.NewAdminClient(&kafka.ConfigMap{"bootstrap.servers": bootstrapServers}) | ||
if err != nil { | ||
fmt.Printf("Failed to create Admin client: %s\n", err) | ||
os.Exit(1) | ||
} | ||
defer a.Close() | ||
|
||
// Contexts are used to abort or limit the amount of time | ||
// the Admin call blocks waiting for a result. | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
results, err := a.ListOffsets(ctx, topicPartitionOffsets, kafka.SetAdminIsolationLevel(kafka.ReadCommitted)) | ||
if err != nil { | ||
fmt.Printf("Failed to List offsets: %v\n", err) | ||
os.Exit(1) | ||
} | ||
// map[TopicPartition]ListOffsetsResultInfo | ||
// Print results | ||
for tp, info := range results.Results { | ||
fmt.Printf("Topic: %s Partition_Index : %d\n", *tp.Topic, tp.Partition) | ||
if info.Error.Code() != kafka.ErrNoError { | ||
fmt.Printf(" ErrorCode : %d ErrorMessage : %s\n\n", info.Error.Code(), info.Error.String()) | ||
} else { | ||
fmt.Printf(" Offset : %d Timestamp : %d\n\n", info.Offset, info.Timestamp) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.