func main() { var cfg Config cmd, err := config.ParseCommand(&cfg) if err != nil { log.Fatal(err) } c := cluster.New(broker.New, cfg.Brokers) switch cmd.Name { case "consume": err = cfg.Consume.Exec(c) case "offset": err = cfg.Offset.Exec(c) case "rollback": err = cfg.Rollback.Exec(c) case "tail": err = cfg.Tail.Exec(c) case "produce": err = cfg.Produce.Exec(c) case "meta": err = cfg.Meta.Exec(c) default: log.Fatal("unkown command " + cmd.Name) } if err != nil { log.Fatal(err) } }
func Dump(k *kafka.Cluster, topic string, newObj func() encoding.BinaryUnmarshaler) (string, error) { cl := cluster.New(broker.New, k.Brokers()) partitions, err := cl.Partitions(topic) if err != nil { return "", err } cr := consumer.New(cl) var lines []string for _, partition := range partitions { start, err := cr.FetchOffsetByTime(topic, partition, proto.Earliest) if err != nil { return "", err } end, err := cr.FetchOffsetByTime(topic, partition, proto.Latest) if err != nil { return "", err } for offset := start; offset < end; { messages, err := cr.Consume(topic, partition, offset) if err != nil { return "", err } if len(messages) == 0 { break } for _, message := range messages { obj := newObj() if err := obj.UnmarshalBinary(message.Value); err != nil { return "", err } jsonBuf, err := json.MarshalIndent(obj, "", "\t") if err != nil { return "", err } lines = append(lines, string(jsonBuf)) } offset += messages[len(messages)-1].Offset + 1 } } sort.Strings(lines) return strings.Join(lines, "\n"), nil }
func NewSender(brokers []string) Sender { return &simpleSender{p: producer.New(cluster.New(broker.NewDefault, brokers))} }