-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add createTopic and listTopics functions
Add example script for creating a topic and listing topics on all partitions Update README
- Loading branch information
Showing
3 changed files
with
111 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
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,29 @@ | ||
/* | ||
This is a k6 test script that imports the xk6-kafka and | ||
list topics on all Kafka partitions and creates a topic. | ||
*/ | ||
|
||
import { | ||
createTopic, | ||
listTopics | ||
} from 'k6/x/kafka'; // import kafka extension | ||
|
||
const address = "localhost:9092"; | ||
const kafkaTopic = "xk6_kafka_test_topic"; | ||
|
||
const results = listTopics(address) | ||
const error = createTopic(address, kafkaTopic); | ||
|
||
export default function () { | ||
results.forEach(topic => console.log(topic)); | ||
|
||
if (error === undefined) { | ||
// If no error returns, it means that the topic | ||
// is successfully created or already exists | ||
console.log("Topic created successfully"); | ||
} else { | ||
console.log("Error while creating topic: ", error); | ||
} | ||
} |
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,79 @@ | ||
package kafka | ||
|
||
import ( | ||
"net" | ||
"strconv" | ||
|
||
"github.com/segmentio/kafka-go" | ||
kafkago "github.com/segmentio/kafka-go" | ||
) | ||
|
||
func (*Kafka) CreateTopic(address, topic string, partitions, replicationFactor int) error { | ||
conn, err := kafkago.Dial("tcp", address) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close() | ||
|
||
controller, err := conn.Controller() | ||
if err != nil { | ||
return err | ||
} | ||
var controllerConn *kafkago.Conn | ||
controllerConn, err = kafkago.Dial("tcp", net.JoinHostPort(controller.Host, strconv.Itoa(controller.Port))) | ||
if err != nil { | ||
return err | ||
} | ||
defer controllerConn.Close() | ||
|
||
if partitions <= 0 { | ||
partitions = 1 | ||
} | ||
|
||
if replicationFactor <= 0 { | ||
replicationFactor = 1 | ||
} | ||
|
||
topicConfigs := []kafkago.TopicConfig{ | ||
kafka.TopicConfig{ | ||
Topic: topic, | ||
NumPartitions: partitions, | ||
ReplicationFactor: replicationFactor, | ||
}, | ||
} | ||
|
||
err = controllerConn.CreateTopics(topicConfigs...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (*Kafka) ListTopics(address string) ([]string, error) { | ||
conn, err := kafkago.Dial("tcp", address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer conn.Close() | ||
|
||
partitions, err := conn.ReadPartitions() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// There should be a better way to return unique set of | ||
// topics instead of looping over them twice | ||
topicSet := map[string]struct{}{} | ||
|
||
for _, partition := range partitions { | ||
topicSet[partition.Topic] = struct{}{} | ||
} | ||
|
||
topics := make([]string, 0, len(topicSet)) | ||
for topic := range topicSet { | ||
topics = append(topics, topic) | ||
} | ||
|
||
return topics, nil | ||
} |