From af16cd94c6200634221950af1ac295f33dcb397d Mon Sep 17 00:00:00 2001 From: waukin Date: Tue, 24 Dec 2024 20:41:57 +0800 Subject: [PATCH 1/2] feat: support SchemaDetails table output format --- .../gravitino/cli/GravitinoCommandLine.java | 3 ++- .../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 +- 6 files changed, 36 insertions(+), 10 deletions(-) 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 48d97294350..88bc9289d1b 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 @@ -342,6 +342,7 @@ private void handleSchemaCommand() { FullName name = new FullName(line); String metalake = name.getMetalakeName(); String catalog = name.getCatalogName(); + String outputFormat = line.getOptionValue(GravitinoOptions.OUTPUT); Command.setAuthenticationMode(auth, userName); @@ -365,7 +366,7 @@ private void handleSchemaCommand() { if (line.hasOption(GravitinoOptions.AUDIT)) { newSchemaAudit(url, ignore, metalake, catalog, schema).handle(); } else { - newSchemaDetails(url, ignore, metalake, catalog, schema).handle(); + newSchemaDetails(url, ignore, outputFormat, metalake, catalog, schema).handle(); } break; 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 effe0da1f10..fb3e6b7a41d 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 @@ -259,8 +259,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 6160634db90..07ffc3cae6b 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"); } @@ -80,4 +83,11 @@ public void output(Catalog[] catalogs) { System.out.println(all); } } + + 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 6946ad13067..1831f0ff3db 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"); } @@ -95,6 +98,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 190e866355b..9b6ca77a1d2 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 @@ -105,7 +105,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"); commandLine.handleCommandLine(); verify(mockDetails).handle(); } From a1d05b62bd89215ede01fb37ef5e529121f6721d Mon Sep 17 00:00:00 2001 From: waukin Date: Tue, 24 Dec 2024 21:05:39 +0800 Subject: [PATCH 2/2] 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 fb3e6b7a41d..b6816784d94 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 @@ -259,7 +259,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 1831f0ff3db..e436c086b3e 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 @@ -103,10 +103,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); }