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 Dec 24, 2024
1 parent a58b7f8 commit 3ba1cd2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,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);
List<String> missingEntities = Lists.newArrayList();
Expand Down Expand Up @@ -372,7 +373,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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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 @@ -80,4 +83,11 @@ public void output(Catalog[] catalogs) {
System.out.println(all);
}
}

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 @@ -95,6 +98,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 @@ -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();
}
Expand Down

0 comments on commit 3ba1cd2

Please sign in to comment.