func main() { flag.Parse() if *fPeers == "" { flagbad("-peers is empty\n") } if *fTopic == "" { flagbad("-topic is empty\n") } kfk, err := kafka.New("kafka-producer", logger, strings.Split(*fPeers, ",")) if err != nil { logger.Panicf("Failed to start kafka: %v", err) } defer kfk.Close() br := bufio.NewReader(os.Stdin) for { line, err := br.ReadBytes('\n') if err == io.EOF && len(line) == 0 { break } if err != nil { logger.Panicf("Reading from stdin: %v", err) } line = bytes.TrimRight(line, "\n") part, offset, err := kfk.Send(nil, line, *fTopic) if err != nil { logger.Panicf("Sending message: %v", err) } if *fVerbose { fmt.Printf("send (len=%d, part=%d, offset=%d)\n", len(line), part, offset) } } }
func main() { flag.Parse() if *fPeers == "" { flagbad("-peers is empty\n") } if *fPartition == -1 { flagbad("-partition is empty\n") } if *fTopic == "" { flagbad("-topic is empty\n") } var offset int64 switch *fOffset { case "newest": offset = sarama.OffsetNewest case "oldest": offset = sarama.OffsetOldest case "manual": offset = *fManual if offset <= -1 { flagbad("-manual positive integer expected") } default: flagbad("-offset expects newest, oldest or time\n") } kfk, err := kafka.New("kafka-consumer", logger, strings.Split(*fPeers, ",")) if err != nil { logger.Panicf("Failed to create kafka client: %v", err) } defer kfk.Close() err = kfk.Listen(*fTopic, int32(*fPartition), offset) if err != nil { logger.Panicf("Failed to listen to partition %s:%d: %v", *fTopic, *fPartition, err) } sigchan := make(chan os.Signal) signal.Notify(sigchan, syscall.SIGINT) kc := killchan.New() go func() { <-sigchan kc.Kill() }() for { select { case <-kc.Chan(): logger.Printf("Interrupt\n") return case message := <-kfk.Incoming(): if *fVerbose { fmt.Printf("%s:%d (offset %d) %s\n", message.Topic, message.Partition, message.Offset, message.Val) } else { fmt.Printf("%s\n", message.Val) } } } }