Created
January 8, 2021 00:37
-
-
Save mancubus77/3ef962534626700a7c214e1114445f4b to your computer and use it in GitHub Desktop.
kafka influx message generator
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"time" | |
"math/rand" | |
"gopkg.in/confluentinc/confluent-kafka-go.v1/kafka" | |
) | |
func makeTimestamp() int64 { | |
return time.Now().UnixNano() | |
} | |
func main() { | |
p, err := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": "my-cluster-kafka-brokers.kafka.svc.cluster.local:9092"}) | |
if err != nil { | |
panic(err) | |
} | |
defer p.Close() | |
// Delivery report handler for produced messages | |
go func() { | |
for e := range p.Events() { | |
switch ev := e.(type) { | |
case *kafka.Message: | |
if ev.TopicPartition.Error != nil { | |
// fmt.Printf("Delivery failed: %v\n", ev.TopicPartition) | |
} | |
} | |
} | |
}() | |
// Produce messages to topic (asynchronously) | |
topic := "topic" | |
for { | |
word := fmt.Sprintf("fffff,go_version=1.15.6,version=1.17.0 metrics_gathered=%vi,gather_errors=0i,metrics_written=39368i,metrics_dropped=0i %v", rand.Intn(10000000), makeTimestamp()) | |
p.Produce(&kafka.Message{ | |
TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny}, | |
Value: []byte(word), | |
}, nil) | |
time.Sleep(500 * time.Millisecond) | |
} | |
p.Flush(15 * 1000) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment