command_mq_topic_create.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package shell
  2. import (
  3. "context"
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. "github.com/seaweedfs/seaweedfs/weed/pb"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  9. "io"
  10. )
  11. func init() {
  12. Commands = append(Commands, &commandMqTopicCreate{})
  13. }
  14. type commandMqTopicCreate struct {
  15. }
  16. func (c *commandMqTopicCreate) Name() string {
  17. return "mq.topic.create"
  18. }
  19. func (c *commandMqTopicCreate) Help() string {
  20. return `create a topic with a given name
  21. Example:
  22. mq.topic.create -namespace <namespace> -topic <topic_name> -partition_count <partition_count>
  23. `
  24. }
  25. func (c *commandMqTopicCreate) Do(args []string, commandEnv *CommandEnv, writer io.Writer) error {
  26. // parse parameters
  27. mqCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  28. namespace := mqCommand.String("namespace", "", "namespace name")
  29. topicName := mqCommand.String("topic", "", "topic name")
  30. partitionCount := mqCommand.Int("partitionCount", 6, "partition count")
  31. if err := mqCommand.Parse(args); err != nil {
  32. return err
  33. }
  34. // find the broker balancer
  35. brokerBalancer, err := findBrokerBalancer(commandEnv)
  36. if err != nil {
  37. return err
  38. }
  39. fmt.Fprintf(writer, "current balancer: %s\n", brokerBalancer)
  40. // create topic
  41. return pb.WithBrokerGrpcClient(false, brokerBalancer, commandEnv.option.GrpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
  42. resp, err := client.CreateTopic(context.Background(), &mq_pb.CreateTopicRequest{
  43. Topic: &mq_pb.Topic{
  44. Namespace: *namespace,
  45. Name: *topicName,
  46. },
  47. PartitionCount: int32(*partitionCount),
  48. })
  49. if err != nil {
  50. return err
  51. }
  52. output, _ := json.MarshalIndent(resp, "", " ")
  53. fmt.Fprintf(writer, "response:\n%+v\n", string(output))
  54. return nil
  55. })
  56. }