Skip to content

Commit

Permalink
add docs on how to list topics (segmentio#521)
Browse files Browse the repository at this point in the history
* add docs on how to list topics

* alter titles

* rm dial leader and only store unique topics

Co-authored-by: Matthew Ault <matthew.ault@segment.com>
  • Loading branch information
aultimus and Matthew Ault committed Oct 2, 2020
1 parent c969b7d commit 76ece4c
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ if err := conn.Close(); err != nil {
}
```

### To Create Topics
```go
// to create topics
topic := "my-topic"
Expand All @@ -124,6 +125,7 @@ if err != nil {
}
```

### To Connect To Leader Via a Non-leader Connection
```go
// to connect to the kafka leader via an existing non-leader connection rather than using DialLeader
conn, err := kafka.Dial("tcp", "localhost:9092")
Expand All @@ -143,6 +145,29 @@ if err != nil {
defer connLeader.Close()
```

### To list topics
```go
conn, err := kafka.Dial("tcp", "localhost:9092")
if err != nil {
panic(err.Error())
}
defer conn.Close()

partitions, err := conn.ReadPartitions()
if err != nil {
panic(err.Error())
}

m := map[string]struct{}{}

for _, p := range partitions {
m[p.Topic] = struct{}{}
}
for k := range m {
fmt.Println(k)
}
```


Because it is low level, the `Conn` type turns out to be a great building block
for higher level abstractions, like the `Reader` for example.
Expand Down

0 comments on commit 76ece4c

Please sign in to comment.