Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#6146] improve(CLI): Refactor topic commands in Gavitino CLI (#6174)

Lord of Abyss2a1729273972
diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java b/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java
index 49c8b8e7c5..fbd4d5b0c8 100644
--- a/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java
+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java
@@ -139,7 +139,7 @@ public class GravitinoCommandLine extends TestableCommandLine {
} else if (entity.equals(CommandEntities.METALAKE)) {
handleMetalakeCommand();
} else if (entity.equals(CommandEntities.TOPIC)) {
- handleTopicCommand();
+ new TopicCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.FILESET)) {
handleFilesetCommand();
} else if (entity.equals(CommandEntities.USER)) {
@@ -798,95 +798,6 @@ public class GravitinoCommandLine extends TestableCommandLine {
}
}
- /**
- * Handles the command execution for topics based on command type and the command line options.
- */
- private void handleTopicCommand() {
- String url = getUrl();
- String auth = getAuth();
- String userName = line.getOptionValue(GravitinoOptions.LOGIN);
- FullName name = new FullName(line);
- String metalake = name.getMetalakeName();
- String catalog = name.getCatalogName();
- String schema = name.getSchemaName();
-
- Command.setAuthenticationMode(auth, userName);
-
- List<String> missingEntities = Lists.newArrayList();
- if (catalog == null) missingEntities.add(CommandEntities.CATALOG);
- if (schema == null) missingEntities.add(CommandEntities.SCHEMA);
-
- if (CommandActions.LIST.equals(command)) {
- checkEntities(missingEntities);
- newListTopics(url, ignore, metalake, catalog, schema).validate().handle();
- return;
- }
-
- String topic = name.getTopicName();
- if (topic == null) missingEntities.add(CommandEntities.TOPIC);
- checkEntities(missingEntities);
-
- switch (command) {
- case CommandActions.DETAILS:
- newTopicDetails(url, ignore, metalake, catalog, schema, topic).validate().handle();
- break;
-
- case CommandActions.CREATE:
- {
- String comment = line.getOptionValue(GravitinoOptions.COMMENT);
- newCreateTopic(url, ignore, metalake, catalog, schema, topic, comment)
- .validate()
- .handle();
- break;
- }
-
- case CommandActions.DELETE:
- {
- boolean force = line.hasOption(GravitinoOptions.FORCE);
- newDeleteTopic(url, ignore, force, metalake, catalog, schema, topic).validate().handle();
- break;
- }
-
- case CommandActions.UPDATE:
- {
- if (line.hasOption(GravitinoOptions.COMMENT)) {
- String comment = line.getOptionValue(GravitinoOptions.COMMENT);
- newUpdateTopicComment(url, ignore, metalake, catalog, schema, topic, comment)
- .validate()
- .handle();
- }
- break;
- }
-
- case CommandActions.SET:
- {
- String property = line.getOptionValue(GravitinoOptions.PROPERTY);
- String value = line.getOptionValue(GravitinoOptions.VALUE);
- newSetTopicProperty(url, ignore, metalake, catalog, schema, topic, property, value)
- .validate()
- .handle();
- break;
- }
-
- case CommandActions.REMOVE:
- {
- String property = line.getOptionValue(GravitinoOptions.PROPERTY);
- newRemoveTopicProperty(url, ignore, metalake, catalog, schema, topic, property)
- .validate()
- .handle();
- break;
- }
-
- case CommandActions.PROPERTIES:
- newListTopicProperties(url, ignore, metalake, catalog, schema, topic).validate().handle();
- break;
-
- default:
- System.err.println(ErrorMessages.UNSUPPORTED_ACTION);
- break;
- }
- }
-
/**
* Handles the command execution for filesets based on command type and the command line options.
*/
diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/TopicCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/TopicCommandHandler.java
new file mode 100644
index 0000000000..7c2a75db91
--- /dev/null
+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/TopicCommandHandler.java
@@ -0,0 +1,196 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.cli;
+
+import com.google.common.collect.Lists;
+import java.util.List;
+import org.apache.commons.cli.CommandLine;
+import org.apache.gravitino.cli.commands.Command;
+
+/** Handles the command execution for Topics based on command type and the command line options. */
+public class TopicCommandHandler extends CommandHandler {
+ private final GravitinoCommandLine gravitinoCommandLine;
+ private final CommandLine line;
+ private final String command;
+ private final boolean ignore;
+ private final String url;
+ private final FullName name;
+ private final String metalake;
+ private final String catalog;
+ private final String schema;
+ private String topic;
+
+ /**
+ * Constructs a {@link TopicCommandHandler} instance.
+ *
+ * @param gravitinoCommandLine The Gravitino command line instance.
+ * @param line The command line arguments.
+ * @param command The command to execute.
+ * @param ignore Ignore server version mismatch.
+ */
+ public TopicCommandHandler(
+ GravitinoCommandLine gravitinoCommandLine, CommandLine line, String command, boolean ignore) {
+ this.gravitinoCommandLine = gravitinoCommandLine;
+ this.line = line;
+ this.command = command;
+ this.ignore = ignore;
+
+ this.url = getUrl(line);
+ this.name = new FullName(line);
+ this.metalake = name.getMetalakeName();
+ this.catalog = name.getCatalogName();
+ this.schema = name.getSchemaName();
+ }
+
+ /** Handles the command execution logic based on the provided command. */
+ @Override
+ protected void handle() {
+ String userName = line.getOptionValue(GravitinoOptions.LOGIN);
+ Command.setAuthenticationMode(getAuth(line), userName);
+
+ List<String> missingEntities = Lists.newArrayList();
+ if (catalog == null) missingEntities.add(CommandEntities.CATALOG);
+ if (schema == null) missingEntities.add(CommandEntities.SCHEMA);
+
+ if (CommandActions.LIST.equals(command)) {
+ checkEntities(missingEntities);
+ handleListCommand();
+ return;
+ }
+
+ topic = name.getTopicName();
+ if (topic == null) missingEntities.add(CommandEntities.TOPIC);
+ checkEntities(missingEntities);
+
+ if (!executeCommand()) {
+ System.err.println(ErrorMessages.UNSUPPORTED_COMMAND);
+ Main.exit(-1);
+ }
+ }
+
+ /**
+ * Executes the specific command based on the command type.
+ *
+ * @return true if the command is supported, false otherwise
+ */
+ private boolean executeCommand() {
+ switch (command) {
+ case CommandActions.DETAILS:
+ handleDetailsCommand();
+ return true;
+
+ case CommandActions.CREATE:
+ handleCreateCommand();
+ return true;
+
+ case CommandActions.DELETE:
+ handleDeleteCommand();
+ return true;
+
+ case CommandActions.UPDATE:
+ handleUpdateCommand();
+ return true;
+
+ case CommandActions.SET:
+ handleSetCommand();
+ return true;
+
+ case CommandActions.REMOVE:
+ handleRemoveCommand();
+ return true;
+
+ case CommandActions.PROPERTIES:
+ handlePropertiesCommand();
+ return true;
+
+ default:
+ return false;
+ }
+ }
+
+ /** Handles the "DETAILS" command. */
+ private void handleDetailsCommand() {
+ gravitinoCommandLine
+ .newTopicDetails(url, ignore, metalake, catalog, schema, topic)
+ .validate()
+ .handle();
+ }
+
+ /** Handles the "CREATE" command. */
+ private void handleCreateCommand() {
+ String comment = line.getOptionValue(GravitinoOptions.COMMENT);
+ gravitinoCommandLine
+ .newCreateTopic(url, ignore, metalake, catalog, schema, topic, comment)
+ .validate()
+ .handle();
+ }
+
+ /** Handles the "DELETE" command. */
+ private void handleDeleteCommand() {
+ boolean force = line.hasOption(GravitinoOptions.FORCE);
+ gravitinoCommandLine
+ .newDeleteTopic(url, ignore, force, metalake, catalog, schema, topic)
+ .validate()
+ .handle();
+ }
+
+ /** Handles the "UPDATE" command. */
+ private void handleUpdateCommand() {
+ if (line.hasOption(GravitinoOptions.COMMENT)) {
+ String comment = line.getOptionValue(GravitinoOptions.COMMENT);
+ gravitinoCommandLine
+ .newUpdateTopicComment(url, ignore, metalake, catalog, schema, topic, comment)
+ .validate()
+ .handle();
+ }
+ }
+
+ /** Handles the "SET" command. */
+ private void handleSetCommand() {
+ String property = line.getOptionValue(GravitinoOptions.PROPERTY);
+ String value = line.getOptionValue(GravitinoOptions.VALUE);
+ gravitinoCommandLine
+ .newSetTopicProperty(url, ignore, metalake, catalog, schema, topic, property, value)
+ .validate()
+ .handle();
+ }
+
+ /** Handles the "REMOVE" command. */
+ private void handleRemoveCommand() {
+ String property = line.getOptionValue(GravitinoOptions.PROPERTY);
+ gravitinoCommandLine
+ .newRemoveTopicProperty(url, ignore, metalake, catalog, schema, topic, property)
+ .validate()
+ .handle();
+ }
+
+ /** Handles the "PROPERTIES" command. */
+ private void handlePropertiesCommand() {
+ gravitinoCommandLine
+ .newListTopicProperties(url, ignore, metalake, catalog, schema, topic)
+ .validate()
+ .handle();
+ }
+
+ /** Handles the "LIST" command. */
+ private void handleListCommand() {
+ gravitinoCommandLine.newListTopics(url, ignore, metalake, catalog, schema).validate().handle();
+ }
+}