Skip to content

Commit

Permalink
feat: support SchemaDetails table output format
Browse files Browse the repository at this point in the history
  • Loading branch information
waukin committed Jan 17, 2025
1 parent 75ef522 commit 4f37aff
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -67,9 +69,5 @@ public void handle() {
} catch (Exception exp) {
exitWithError(exp.getMessage());
}

if (result != null) {
System.out.println(result.name() + "," + result.comment());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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");
}
Expand Down Expand Up @@ -88,4 +91,11 @@ public void output(Catalog[] catalogs) {
}
}
}

static final class SchemaPlainFormat implements OutputFormat<Schema> {
@Override
public void output(Schema schema) {
System.out.println(schema.name() + "," + schema.comment());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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");
}
Expand Down Expand Up @@ -103,6 +106,20 @@ public void output(Catalog[] catalogs) {
}
}

static final class SchemaTableFormat implements OutputFormat<Schema> {
@Override
public void output(Schema schema) {
List<String> headers = Arrays.asList("schema", "comment");
List<List<String>> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 4f37aff

Please sign in to comment.