diff --git a/README.md b/README.md index 4ce92ec..3928d20 100644 --- a/README.md +++ b/README.md @@ -6,21 +6,23 @@ Usage of kt: -brokers string Comma separated list of brokers. (default "localhost:9092") + -json + Print output in JSON format. -offset string Colon separated offsets where to start and end reading messages. -topic string Topic to consume. $ kt -topic kt-test - Partition=0 Offset=0 Key= Value=Hello, World 0 - Partition=0 Offset=1 Key= Value=Hallo, Welt - Partition=0 Offset=2 Key= Value=Bonjour, monde + Partition=0 Offset=0 Key= Message=Hello, World 0 + Partition=0 Offset=1 Key= Message=Hallo, Welt + Partition=0 Offset=2 Key= Message=Bonjour, monde ^C2016/01/26 06:29:19 Received interrupt - shutting down... $ kt -topic kt-test -offset 1: - Partition=0 Offset=1 Key= Value=Hallo, Welt - Partition=0 Offset=2 Key= Value=Bonjour, monde + Partition=0 Offset=1 Key= Message=Hallo, Welt + Partition=0 Offset=2 Key= Message=Bonjour, monde ^C2016/01/26 06:29:29 Received interrupt - shutting down... $ kt -topic kt-test -offset 1:1 - Partition=0 Offset=1 Key= Value=Hallo, Welt + Partition=0 Offset=1 Key= Message=Hallo, Welt $ ## Installation diff --git a/main.go b/main.go index c627766..d3a8cf8 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ var config struct { brokers []string startOffset int64 endOffset int64 + jsonOutput bool } func listenForInterrupt() chan struct{} { @@ -34,8 +35,22 @@ func listenForInterrupt() chan struct{} { } func print(msg *sarama.ConsumerMessage) { + + if config.jsonOutput { + fmt.Printf( + `{"partition":%v,"offset":%v,"key":%#v,"message":%#v} +`, + msg.Partition, + msg.Offset, + string(msg.Key), + string(msg.Value), + ) + + return + } + fmt.Printf( - "Partition=%v Offset=%v Key=%s Value=%s\n", + "Partition=%v Offset=%v Key=%s Message=%s\n", msg.Partition, msg.Offset, msg.Key, @@ -59,6 +74,7 @@ func parseArgs() { flag.StringVar(&config.topic, "topic", "", "Topic to consume.") flag.StringVar(&brokersString, "brokers", "localhost:9092", "Comma separated list of brokers.") flag.StringVar(&offset, "offset", "", "Colon separated offsets where to start and end reading messages.") + flag.BoolVar(&config.jsonOutput, "json", false, "Print output in JSON format.") flag.Parse()