You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.6 KiB

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