Skip to content

Commit

Permalink
[apache#6419] improve(CLI): Add table command context CLI (apache#6428)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Add table command context CLI.

### Why are the changes needed?

Fix: apache#6419 

### Does this PR introduce _any_ user-facing change?

No

### How was this patch tested?

local test.
  • Loading branch information
Abyss-lord committed Feb 11, 2025
1 parent 062fcc0 commit 9cbe8eb
Show file tree
Hide file tree
Showing 19 changed files with 233 additions and 286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private void executeCommand() {
} else if (entity.equals(CommandEntities.COLUMN)) {
new ColumnCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.TABLE)) {
new TableCommandHandler(this, line, command, ignore).handle();
new TableCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.SCHEMA)) {
new SchemaCommandHandler(this, line, command, ignore).handle();
} else if (entity.equals(CommandEntities.CATALOG)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public class TableCommandHandler extends CommandHandler {
private final GravitinoCommandLine gravitinoCommandLine;
private final CommandLine line;
private final String command;
private final boolean ignore;
private final String url;
private final CommandContext context;
private final FullName name;
private final String metalake;
private final String catalog;
Expand All @@ -43,16 +42,19 @@ public class TableCommandHandler extends CommandHandler {
* @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.
* @param context The command context.
*/
public TableCommandHandler(
GravitinoCommandLine gravitinoCommandLine, CommandLine line, String command, boolean ignore) {
GravitinoCommandLine gravitinoCommandLine,
CommandLine line,
String command,
CommandContext context) {
this.gravitinoCommandLine = gravitinoCommandLine;
this.line = line;
this.command = command;
this.ignore = ignore;
this.context = context;

this.url = getUrl(line);
this.context.setUrl(getUrl(line));
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
this.catalog = name.getCatalogName();
Expand Down Expand Up @@ -129,32 +131,32 @@ private boolean executeCommand() {
private void handleDetailsCommand() {
if (line.hasOption(GravitinoOptions.AUDIT)) {
gravitinoCommandLine
.newTableAudit(url, ignore, metalake, catalog, schema, table)
.newTableAudit(context, metalake, catalog, schema, table)
.validate()
.handle();
} else if (line.hasOption(GravitinoOptions.INDEX)) {
gravitinoCommandLine
.newListIndexes(url, ignore, metalake, catalog, schema, table)
.newListIndexes(context, metalake, catalog, schema, table)
.validate()
.handle();
} else if (line.hasOption(GravitinoOptions.DISTRIBUTION)) {
gravitinoCommandLine
.newTableDistribution(url, ignore, metalake, catalog, schema, table)
.newTableDistribution(context, metalake, catalog, schema, table)
.validate()
.handle();
} else if (line.hasOption(GravitinoOptions.PARTITION)) {
gravitinoCommandLine
.newTablePartition(url, ignore, metalake, catalog, schema, table)
.newTablePartition(context, metalake, catalog, schema, table)
.validate()
.handle();
} else if (line.hasOption(GravitinoOptions.SORTORDER)) {
gravitinoCommandLine
.newTableSortOrder(url, ignore, metalake, catalog, schema, table)
.newTableSortOrder(context, metalake, catalog, schema, table)
.validate()
.handle();
} else {
gravitinoCommandLine
.newTableDetails(url, ignore, metalake, catalog, schema, table)
.newTableDetails(context, metalake, catalog, schema, table)
.validate()
.handle();
}
Expand All @@ -165,16 +167,15 @@ private void handleCreateCommand() {
String columnFile = line.getOptionValue(GravitinoOptions.COLUMNFILE);
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
gravitinoCommandLine
.newCreateTable(url, ignore, metalake, catalog, schema, table, columnFile, comment)
.newCreateTable(context, metalake, catalog, schema, table, columnFile, comment)
.validate()
.handle();
}

/** Handles the "DELETE" command. */
private void handleDeleteCommand() {
boolean force = line.hasOption(GravitinoOptions.FORCE);
gravitinoCommandLine
.newDeleteTable(url, ignore, force, metalake, catalog, schema, table)
.newDeleteTable(context, metalake, catalog, schema, table)
.validate()
.handle();
}
Expand All @@ -184,7 +185,7 @@ private void handleSetCommand() {
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
String value = line.getOptionValue(GravitinoOptions.VALUE);
gravitinoCommandLine
.newSetTableProperty(url, ignore, metalake, catalog, schema, table, property, value)
.newSetTableProperty(context, metalake, catalog, schema, table, property, value)
.validate()
.handle();
}
Expand All @@ -193,36 +194,36 @@ private void handleSetCommand() {
private void handleRemoveCommand() {
String property = line.getOptionValue(GravitinoOptions.PROPERTY);
gravitinoCommandLine
.newRemoveTableProperty(url, ignore, metalake, catalog, schema, table, property)
.newRemoveTableProperty(context, metalake, catalog, schema, table, property)
.validate()
.handle();
}
/** Handles the "PROPERTIES" command. */
private void handlePropertiesCommand() {
gravitinoCommandLine
.newListTableProperties(url, ignore, metalake, catalog, schema, table)
.newListTableProperties(context, metalake, catalog, schema, table)
.validate()
.handle();
}

/** Handles the "LIST" command. */
private void handleListCommand() {
gravitinoCommandLine.newListTables(url, ignore, metalake, catalog, schema).validate().handle();
gravitinoCommandLine.newListTables(context, metalake, catalog, schema).validate().handle();
}

/** Handles the "UPDATE" command. */
private void handleUpdateCommand() {
if (line.hasOption(GravitinoOptions.COMMENT)) {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
gravitinoCommandLine
.newUpdateTableComment(url, ignore, metalake, catalog, schema, table, comment)
.newUpdateTableComment(context, metalake, catalog, schema, table, comment)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.RENAME)) {
String newName = line.getOptionValue(GravitinoOptions.RENAME);
gravitinoCommandLine
.newUpdateTableName(url, ignore, metalake, catalog, schema, table, newName)
.newUpdateTableName(context, metalake, catalog, schema, table, newName)
.validate()
.handle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,99 +302,89 @@ protected ListSchemaProperties newListSchemaProperties(
}

protected TableAudit newTableAudit(
String url, boolean ignore, String metalake, String catalog, String schema, String table) {
return new TableAudit(url, ignore, metalake, catalog, schema, table);
CommandContext context, String metalake, String catalog, String schema, String table) {
return new TableAudit(context, metalake, catalog, schema, table);
}

protected TableDetails newTableDetails(
String url, boolean ignore, String metalake, String catalog, String schema, String table) {
return new TableDetails(url, ignore, metalake, catalog, schema, table);
CommandContext context, String metalake, String catalog, String schema, String table) {
return new TableDetails(context, metalake, catalog, schema, table);
}

protected ListTables newListTables(
String url, boolean ignore, String metalake, String catalog, String schema) {
return new ListTables(url, ignore, metalake, catalog, schema);
CommandContext context, String metalake, String catalog, String schema) {
return new ListTables(context, metalake, catalog, schema);
}

protected DeleteTable newDeleteTable(
String url,
boolean ignore,
boolean force,
String metalake,
String catalog,
String schema,
String table) {
return new DeleteTable(url, ignore, force, metalake, catalog, schema, table);
CommandContext context, String metalake, String catalog, String schema, String table) {
return new DeleteTable(context, metalake, catalog, schema, table);
}

protected ListIndexes newListIndexes(
String url, boolean ignore, String metalake, String catalog, String schema, String table) {
return new ListIndexes(url, ignore, metalake, catalog, schema, table);
CommandContext context, String metalake, String catalog, String schema, String table) {
return new ListIndexes(context, metalake, catalog, schema, table);
}

protected TablePartition newTablePartition(
String url, boolean ignore, String metalake, String catalog, String schema, String table) {
return new TablePartition(url, ignore, metalake, catalog, schema, table);
CommandContext context, String metalake, String catalog, String schema, String table) {
return new TablePartition(context, metalake, catalog, schema, table);
}

protected TableDistribution newTableDistribution(
String url, boolean ignore, String metalake, String catalog, String schema, String table) {
return new TableDistribution(url, ignore, metalake, catalog, schema, table);
CommandContext context, String metalake, String catalog, String schema, String table) {
return new TableDistribution(context, metalake, catalog, schema, table);
}

protected TableSortOrder newTableSortOrder(
String url, boolean ignore, String metalake, String catalog, String schema, String table) {
return new TableSortOrder(url, ignore, metalake, catalog, schema, table);
CommandContext context, String metalake, String catalog, String schema, String table) {
return new TableSortOrder(context, metalake, catalog, schema, table);
}

protected UpdateTableComment newUpdateTableComment(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
String table,
String comment) {
return new UpdateTableComment(url, ignore, metalake, catalog, schema, table, comment);
return new UpdateTableComment(context, metalake, catalog, schema, table, comment);
}

protected UpdateTableName newUpdateTableName(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
String table,
String rename) {
return new UpdateTableName(url, ignore, metalake, catalog, schema, table, rename);
return new UpdateTableName(context, metalake, catalog, schema, table, rename);
}

protected SetTableProperty newSetTableProperty(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
String table,
String property,
String value) {
return new SetTableProperty(url, ignore, metalake, catalog, schema, table, property, value);
return new SetTableProperty(context, metalake, catalog, schema, table, property, value);
}

protected RemoveTableProperty newRemoveTableProperty(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
String table,
String property) {
return new RemoveTableProperty(url, ignore, metalake, catalog, schema, table, property);
return new RemoveTableProperty(context, metalake, catalog, schema, table, property);
}

protected ListTableProperties newListTableProperties(
String url, boolean ignore, String metalake, String catalog, String schema, String table) {
return new ListTableProperties(url, ignore, metalake, catalog, schema, table);
CommandContext context, String metalake, String catalog, String schema, String table) {
return new ListTableProperties(context, metalake, catalog, schema, table);
}

protected UserDetails newUserDetails(String url, boolean ignore, String metalake, String user) {
Expand Down Expand Up @@ -866,15 +856,14 @@ protected UpdateColumnDefault newUpdateColumnDefault(
}

protected CreateTable newCreateTable(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
String table,
String columnFile,
String comment) {
return new CreateTable(url, ignore, metalake, catalog, schema, table, columnFile, comment);
return new CreateTable(context, metalake, catalog, schema, table, columnFile, comment);
}

protected GrantPrivilegesToRole newGrantPrivilegesToRole(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.cli.ReadTableCSV;
import org.apache.gravitino.client.GravitinoClient;
Expand All @@ -42,8 +43,7 @@ public class CreateTable extends Command {
/**
* Create a new table.
*
* @param url The URL of the Gravitino server.
* @param ignoreVersions If true don't check the client/server versions match.
* @param context The command context.
* @param metalake The name of the metalake.
* @param catalog The name of the catalog.
* @param schema The name of the schema.
Expand All @@ -52,15 +52,14 @@ public class CreateTable extends Command {
* @param comment The table's comment.
*/
public CreateTable(
String url,
boolean ignoreVersions,
CommandContext context,
String metalake,
String catalog,
String schema,
String table,
String columnFile,
String comment) {
super(url, ignoreVersions);
super(context);
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
Expand Down Expand Up @@ -106,7 +105,7 @@ public void handle() {
exitWithError(exp.getMessage());
}

System.out.println(table + " created");
printInformation(table + " created");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.cli.AreYouSure;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
Expand All @@ -39,24 +40,16 @@ public class DeleteTable extends Command {
/**
* Delete a table.
*
* @param url The URL of the Gravitino server.
* @param ignoreVersions If true don't check the client/server versions match.
* @param force Force operation.
* @param context The command context.
* @param metalake The name of the metalake.
* @param catalog The name of the catalog.
* @param schema The name of the schema.
* @param table The name of the table.
*/
public DeleteTable(
String url,
boolean ignoreVersions,
boolean force,
String metalake,
String catalog,
String schema,
String table) {
super(url, ignoreVersions);
this.force = force;
CommandContext context, String metalake, String catalog, String schema, String table) {
super(context);
this.force = context.force();
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
Expand Down Expand Up @@ -89,9 +82,9 @@ public void handle() {
}

if (deleted) {
System.out.println(table + " deleted.");
printInformation(table + " deleted.");
} else {
System.out.println(table + " not deleted.");
printInformation(table + " not deleted.");
}
}
}
Loading

0 comments on commit 9cbe8eb

Please sign in to comment.