From a7f4cca28041cb9c6b1fc6263ec59bb504b740a0 Mon Sep 17 00:00:00 2001 From: waukin Date: Tue, 24 Dec 2024 20:41:57 +0800 Subject: [PATCH 1/4] feat: support SchemaDetails table output format --- .../gravitino/cli/TestableCommandLine.java | 4 ++-- .../gravitino/cli/commands/SchemaDetails.java | 10 ++++------ .../gravitino/cli/outputs/PlainFormat.java | 10 ++++++++++ .../gravitino/cli/outputs/TableFormat.java | 17 +++++++++++++++++ .../gravitino/cli/TestSchemaCommands.java | 2 +- 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java b/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java index b229ef16aa3..e23378d5418 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java @@ -267,8 +267,8 @@ protected SchemaAudit newSchemaAudit( } protected SchemaDetails newSchemaDetails( - String url, boolean ignore, String metalake, String catalog, String schema) { - return new SchemaDetails(url, ignore, metalake, catalog, schema); + String url, boolean ignore, String outputFormat, String metalake, String catalog, String schema) { + return new SchemaDetails(url, ignore, outputFormat, metalake, catalog, schema); } protected ListSchema newListSchema(String url, boolean ignore, String metalake, String catalog) { diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SchemaDetails.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SchemaDetails.java index 7369c0d1b41..cf87296d37e 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SchemaDetails.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SchemaDetails.java @@ -38,13 +38,14 @@ public class SchemaDetails extends Command { * * @param url The URL of the Gravitino server. * @param ignoreVersions If true don't check the client/server versions match. + * @param outputFormat The output format. * @param metalake The name of the metalake. * @param catalog The name of the catalog. * @param schema The name of the schenma. */ public SchemaDetails( - String url, boolean ignoreVersions, String metalake, String catalog, String schema) { - super(url, ignoreVersions); + String url, boolean ignoreVersions, String outputFormat, String metalake, String catalog, String schema) { + super(url, ignoreVersions, outputFormat); this.metalake = metalake; this.catalog = catalog; this.schema = schema; @@ -58,6 +59,7 @@ public void handle() { try { GravitinoClient client = buildClient(metalake); result = client.loadCatalog(catalog).asSchemas().loadSchema(schema); + output(result); } catch (NoSuchMetalakeException err) { exitWithError(ErrorMessages.UNKNOWN_METALAKE); } catch (NoSuchCatalogException err) { @@ -67,9 +69,5 @@ public void handle() { } catch (Exception exp) { exitWithError(exp.getMessage()); } - - if (result != null) { - System.out.println(result.name() + "," + result.comment()); - } } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/PlainFormat.java b/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/PlainFormat.java index 66e616c4f78..ac3db657817 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/PlainFormat.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/PlainFormat.java @@ -23,6 +23,7 @@ import java.util.stream.Collectors; import org.apache.gravitino.Catalog; import org.apache.gravitino.Metalake; +import org.apache.gravitino.Schema; /** Plain format to print a pretty string to standard out. */ public class PlainFormat { @@ -35,6 +36,8 @@ public static void output(Object object) { new CatalogPlainFormat().output((Catalog) object); } else if (object instanceof Catalog[]) { new CatalogsPlainFormat().output((Catalog[]) object); + } else if (object instanceof Schema) { + new SchemaPlainFormat().output((Schema) object); } else { throw new IllegalArgumentException("Unsupported object type"); } @@ -88,4 +91,11 @@ public void output(Catalog[] catalogs) { } } } + + static final class SchemaPlainFormat implements OutputFormat { + @Override + public void output(Schema schema) { + System.out.println(schema.name() + "," + schema.comment()); + } + } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java b/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java index a3c99756524..074d18c4fef 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java @@ -25,6 +25,7 @@ import java.util.regex.Pattern; import org.apache.gravitino.Catalog; import org.apache.gravitino.Metalake; +import org.apache.gravitino.Schema; /** Table format to print a pretty table to standard out. */ public class TableFormat { @@ -37,6 +38,8 @@ public static void output(Object object) { new CatalogTableFormat().output((Catalog) object); } else if (object instanceof Catalog[]) { new CatalogsTableFormat().output((Catalog[]) object); + } else if (object instanceof Schema) { + new SchemaTableFormat().output((Schema) object); } else { throw new IllegalArgumentException("Unsupported object type"); } @@ -103,6 +106,20 @@ public void output(Catalog[] catalogs) { } } + static final class SchemaTableFormat implements OutputFormat { + @Override + public void output(Schema schema) { + List headers = Arrays.asList("schema", "comment"); + List> rows = new ArrayList<>(); + rows.add( + Arrays.asList( + schema.name(), + schema.comment())); + TableFormatImpl tableFormat = new TableFormatImpl(); + tableFormat.print(headers, rows); + } + } + static final class TableFormatImpl { private int[] maxElementLengths; // This expression is primarily used to match characters that have a display width of diff --git a/clients/cli/src/test/java/org/apache/gravitino/cli/TestSchemaCommands.java b/clients/cli/src/test/java/org/apache/gravitino/cli/TestSchemaCommands.java index 6b8770d8edf..2219f4f0f33 100644 --- a/clients/cli/src/test/java/org/apache/gravitino/cli/TestSchemaCommands.java +++ b/clients/cli/src/test/java/org/apache/gravitino/cli/TestSchemaCommands.java @@ -108,7 +108,7 @@ void testSchemaDetailsCommand() { doReturn(mockDetails) .when(commandLine) .newSchemaDetails( - GravitinoCommandLine.DEFAULT_URL, false, "metalake_demo", "catalog", "schema"); + GravitinoCommandLine.DEFAULT_URL, false, null, "metalake_demo", "catalog", "schema"); doReturn(mockDetails).when(mockDetails).validate(); commandLine.handleCommandLine(); verify(mockDetails).handle(); From a69ae8e7ec6a62786576500ee6c939cad5dac514 Mon Sep 17 00:00:00 2001 From: waukin Date: Tue, 24 Dec 2024 21:05:39 +0800 Subject: [PATCH 2/4] fix: adjust format --- .../java/org/apache/gravitino/cli/TestableCommandLine.java | 7 ++++++- .../org/apache/gravitino/cli/commands/SchemaDetails.java | 7 ++++++- .../java/org/apache/gravitino/cli/outputs/TableFormat.java | 5 +---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java b/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java index e23378d5418..8af6112413c 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java @@ -267,7 +267,12 @@ protected SchemaAudit newSchemaAudit( } protected SchemaDetails newSchemaDetails( - String url, boolean ignore, String outputFormat, String metalake, String catalog, String schema) { + String url, + boolean ignore, + String outputFormat, + String metalake, + String catalog, + String schema) { return new SchemaDetails(url, ignore, outputFormat, metalake, catalog, schema); } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SchemaDetails.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SchemaDetails.java index cf87296d37e..1f808df26d2 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SchemaDetails.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/SchemaDetails.java @@ -44,7 +44,12 @@ public class SchemaDetails extends Command { * @param schema The name of the schenma. */ public SchemaDetails( - String url, boolean ignoreVersions, String outputFormat, String metalake, String catalog, String schema) { + String url, + boolean ignoreVersions, + String outputFormat, + String metalake, + String catalog, + String schema) { super(url, ignoreVersions, outputFormat); this.metalake = metalake; this.catalog = catalog; diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java b/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java index 074d18c4fef..2be9820834d 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java @@ -111,10 +111,7 @@ static final class SchemaTableFormat implements OutputFormat { public void output(Schema schema) { List headers = Arrays.asList("schema", "comment"); List> rows = new ArrayList<>(); - rows.add( - Arrays.asList( - schema.name(), - schema.comment())); + rows.add(Arrays.asList(schema.name(), schema.comment() + "")); TableFormatImpl tableFormat = new TableFormatImpl(); tableFormat.print(headers, rows); } From 090709b3a5a5db386023c49ff06238ed3f7b3d32 Mon Sep 17 00:00:00 2001 From: waukin Date: Tue, 21 Jan 2025 20:58:41 +0800 Subject: [PATCH 3/4] feat: add outputFormat to SchemaCommandHandler --- .../java/org/apache/gravitino/cli/SchemaCommandHandler.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/SchemaCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/SchemaCommandHandler.java index 4a0cf919fb8..84e9070fb63 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/SchemaCommandHandler.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/SchemaCommandHandler.java @@ -35,6 +35,7 @@ public class SchemaCommandHandler extends CommandHandler { private final String metalake; private final String catalog; private String schema; + private final String outputFormat; /** * Constructs a {@link SchemaCommandHandler} instance. @@ -55,6 +56,7 @@ public SchemaCommandHandler( this.name = new FullName(line); this.metalake = name.getMetalakeName(); this.catalog = name.getCatalogName(); + this.outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT); } @Override @@ -132,7 +134,7 @@ private void handleDetailsCommand() { .handle(); } else { gravitinoCommandLine - .newSchemaDetails(url, ignore, metalake, catalog, schema) + .newSchemaDetails(url, ignore, outputFormat, metalake, catalog, schema) .validate() .handle(); } From 75af11f5d6405682d082394346a5ce9d19fedd4f Mon Sep 17 00:00:00 2001 From: waukin Date: Tue, 21 Jan 2025 21:15:37 +0800 Subject: [PATCH 4/4] fix: add missing null check --- .../apache/gravitino/cli/outputs/PlainFormat.java | 6 +++++- .../apache/gravitino/cli/outputs/TableFormat.java | 14 +++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/PlainFormat.java b/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/PlainFormat.java index ac3db657817..0b91315cfb7 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/PlainFormat.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/PlainFormat.java @@ -95,7 +95,11 @@ public void output(Catalog[] catalogs) { static final class SchemaPlainFormat implements OutputFormat { @Override public void output(Schema schema) { - System.out.println(schema.name() + "," + schema.comment()); + if (schema == null) { + System.out.println("No schemas exist."); + } else { + System.out.println(schema.name() + "," + schema.comment()); + } } } } diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java b/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java index 2be9820834d..ef87e79b531 100644 --- a/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java +++ b/clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java @@ -109,11 +109,15 @@ public void output(Catalog[] catalogs) { static final class SchemaTableFormat implements OutputFormat { @Override public void output(Schema schema) { - List headers = Arrays.asList("schema", "comment"); - List> rows = new ArrayList<>(); - rows.add(Arrays.asList(schema.name(), schema.comment() + "")); - TableFormatImpl tableFormat = new TableFormatImpl(); - tableFormat.print(headers, rows); + if (schema == null) { + System.out.println("No schemas exist."); + } else { + List headers = Arrays.asList("schema", "comment"); + List> rows = new ArrayList<>(); + rows.add(Arrays.asList(schema.name(), schema.comment() + "")); + TableFormatImpl tableFormat = new TableFormatImpl(); + tableFormat.print(headers, rows); + } } }