This repository has been archived by the owner on May 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
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
518443a
commit debb4c7
Showing
5 changed files
with
78 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 |
---|---|---|
@@ -1,2 +1 @@ | ||
kafka | ||
stressproducer |
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 @@ | ||
consoleconsumer |
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,76 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/Shopify/sarama" | ||
) | ||
|
||
var ( | ||
brokerList = flag.String("brokers", "localhost:9092", "The comma separated list of brokers in the Kafka cluster") | ||
topic = flag.String("topic", "", "The topic to consume") | ||
partition = flag.Int("partition", 0, "The partition to consume") | ||
offset = flag.String("offset", "oldest", "The offset to start with. Can be `oldest`, `newest`, or an actual offset") | ||
verbose = flag.Bool("verbose", false, "Whether to turn on sarama logging") | ||
|
||
logger = log.New(os.Stderr, "", log.LstdFlags) | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if *verbose { | ||
sarama.Logger = logger | ||
} | ||
|
||
var ( | ||
initialOffset int64 | ||
offsetError error | ||
) | ||
switch *offset { | ||
case "oldest": | ||
initialOffset = sarama.OffsetOldest | ||
case "newest": | ||
initialOffset = sarama.OffsetNewest | ||
default: | ||
initialOffset, offsetError = strconv.ParseInt(*offset, 10, 64) | ||
} | ||
|
||
if offsetError != nil { | ||
logger.Fatalln("Invalid initial offset:", *offset) | ||
} | ||
|
||
c, err := sarama.NewConsumer(strings.Split(*brokerList, ","), nil) | ||
if err != nil { | ||
logger.Fatalln(err) | ||
} | ||
|
||
pc, err := c.ConsumePartition(*topic, int32(*partition), initialOffset) | ||
if err != nil { | ||
logger.Fatalln(err) | ||
} | ||
|
||
go func() { | ||
signals := make(chan os.Signal, 1) | ||
signal.Notify(signals, os.Kill, os.Interrupt) | ||
<-signals | ||
pc.AsyncClose() | ||
}() | ||
|
||
for msg := range pc.Messages() { | ||
fmt.Printf("Offset: %d\n", msg.Offset) | ||
fmt.Printf("Key: %s\n", string(msg.Key)) | ||
fmt.Printf("Value: %s\n", string(msg.Value)) | ||
fmt.Println() | ||
} | ||
|
||
if err := c.Close(); err != nil { | ||
fmt.Println("Failed to close consumer: ", err) | ||
} | ||
} |
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 @@ | ||
stressproducer |
File renamed without changes.