Skip to content

Commit fb3d900

Browse files
authored
[#6493] feat(CLI): Support table format output for Schema and Table command (#6495)
### What changes were proposed in this pull request? Support table format output for Schema and Table command. ### Why are the changes needed? Fix: #6493 ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? local test. ```bash gcli schema list -m demo_metalake --name Hive_catalog --output table -i +---------------------------+ | Schema | +---------------------------+ | convert_example | | default | | hive_best_performance | | test_hive_convert_example | | test_multiple_data_source | +---------------------------+ gcli schema details -m demo_metalake --name Hive_catalog.default --output table -i +---------+-----------------------+ | Schema | Comment | +---------+-----------------------+ | default | Default Hive database | +---------+-----------------------+ gcli table list -m demo_metalake --name Hive_catalog.default -i --output table +---------------------------------------+ | Table | +---------------------------------------+ | test_create_txt_tbl_without_delimited | | test_jinjia_tbl1 | | test_jinjia_tbl2 | | test | | test1 | | example_table | | example_table2 | +---------------------------------------+ gcli table details -m demo_metalake --name Hive_catalog.default.test1 -i --output table +----------+---------+---------------+----------+---------+ | Name | Type | AutoIncrement | Nullable | Comment | +----------+---------+---------------+----------+---------+ | id | integer | false | true | N/A | | name | string | false | true | N/A | | standard | long | false | true | N/A | | dt | string | false | true | N/A | +----------+---------+---------------+----------+---------+ ```
1 parent ee296fa commit fb3d900

File tree

7 files changed

+290
-26
lines changed

7 files changed

+290
-26
lines changed

clients/cli/src/main/java/org/apache/gravitino/cli/commands/Command.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ protected <T extends GravitinoClientBase> Builder<T> constructClient(Builder<T>
235235
* @param entity The entity to output.
236236
* @param <T> The type of entity.
237237
*/
238-
protected <T> void output(T entity) {
238+
private <T> void output(T entity) {
239239
if (outputFormat == null) {
240240
PlainFormat.output(entity, context);
241241
return;

clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListSchema.java

+28-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
package org.apache.gravitino.cli.commands;
2121

22-
import com.google.common.base.Joiner;
22+
import org.apache.gravitino.Audit;
23+
import org.apache.gravitino.Schema;
2324
import org.apache.gravitino.cli.CommandContext;
2425
import org.apache.gravitino.cli.ErrorMessages;
2526
import org.apache.gravitino.client.GravitinoClient;
@@ -49,8 +50,10 @@ public ListSchema(CommandContext context, String metalake, String catalog) {
4950
@Override
5051
public void handle() {
5152
String[] schemas = new String[0];
53+
GravitinoClient client;
54+
5255
try {
53-
GravitinoClient client = buildClient(metalake);
56+
client = buildClient(metalake);
5457
schemas = client.loadCatalog(catalog).asSchemas().listSchemas();
5558
} catch (NoSuchMetalakeException err) {
5659
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
@@ -60,8 +63,29 @@ public void handle() {
6063
exitWithError(exp.getMessage());
6164
}
6265

63-
String all = schemas.length == 0 ? "No schemas exist." : Joiner.on(",").join(schemas);
66+
if (schemas.length == 0) {
67+
printInformation("No schemas exist.");
68+
return;
69+
}
70+
71+
Schema[] schemaObjects = new Schema[schemas.length];
72+
for (int i = 0; i < schemas.length; i++) {
73+
String schemaName = schemas[i];
74+
Schema gSchema =
75+
new Schema() {
76+
@Override
77+
public String name() {
78+
return schemaName;
79+
}
80+
81+
@Override
82+
public Audit auditInfo() {
83+
return null;
84+
}
85+
};
86+
schemaObjects[i] = gSchema;
87+
}
6488

65-
printInformation(all);
89+
printResults(schemaObjects);
6690
}
6791
}

clients/cli/src/main/java/org/apache/gravitino/cli/commands/ListTables.java

+30-11
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
package org.apache.gravitino.cli.commands;
2121

22-
import com.google.common.base.Joiner;
23-
import java.util.ArrayList;
24-
import java.util.List;
22+
import org.apache.gravitino.Audit;
2523
import org.apache.gravitino.NameIdentifier;
2624
import org.apache.gravitino.Namespace;
2725
import org.apache.gravitino.cli.CommandContext;
26+
import org.apache.gravitino.rel.Column;
27+
import org.apache.gravitino.rel.Table;
2828

2929
/** List the names of all tables in a schema. */
3030
public class ListTables extends TableCommand {
@@ -49,22 +49,41 @@ public ListTables(CommandContext context, String metalake, String catalog, Strin
4949
public void handle() {
5050
NameIdentifier[] tables = null;
5151
Namespace name = Namespace.of(schema);
52+
5253
try {
5354
tables = tableCatalog().listTables(name);
5455
} catch (Exception exp) {
5556
exitWithError(exp.getMessage());
5657
}
5758

58-
List<String> tableNames = new ArrayList<>();
59-
for (int i = 0; i < tables.length; i++) {
60-
tableNames.add(tables[i].name());
59+
if (tables.length == 0) {
60+
printInformation("No tables exist.");
61+
return;
6162
}
6263

63-
String all =
64-
tableNames.isEmpty()
65-
? "No tables exist."
66-
: Joiner.on(System.lineSeparator()).join(tableNames);
64+
Table[] gTables = new Table[tables.length];
65+
for (int i = 0; i < tables.length; i++) {
66+
String tableName = tables[i].name();
67+
gTables[i] =
68+
new Table() {
69+
70+
@Override
71+
public String name() {
72+
return tableName;
73+
}
74+
75+
@Override
76+
public Column[] columns() {
77+
return new Column[0];
78+
}
79+
80+
@Override
81+
public Audit auditInfo() {
82+
return null;
83+
}
84+
};
85+
}
6786

68-
printResults(all);
87+
printResults(gTables);
6988
}
7089
}

clients/cli/src/main/java/org/apache/gravitino/cli/commands/SchemaDetails.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void handle() {
6868
}
6969

7070
if (result != null) {
71-
printInformation(result.name() + "," + result.comment());
71+
printResults(result);
7272
}
7373
}
7474
}

clients/cli/src/main/java/org/apache/gravitino/cli/commands/TableDetails.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public void handle() {
5757
exitWithError(exp.getMessage());
5858
}
5959

60-
printInformation(gTable.name() + "," + gTable.comment());
60+
if (gTable != null) {
61+
printResults(gTable);
62+
}
6163
}
6264
}

clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java

+103
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
import java.util.Objects;
4848
import org.apache.gravitino.Catalog;
4949
import org.apache.gravitino.Metalake;
50+
import org.apache.gravitino.Schema;
5051
import org.apache.gravitino.cli.CommandContext;
52+
import org.apache.gravitino.rel.Table;
5153

5254
/**
5355
* Abstract base class for formatting entity information into ASCII-art tables. Provides
@@ -75,6 +77,14 @@ public static void output(Object entity, CommandContext context) {
7577
new CatalogTableFormat(context).output((Catalog) entity);
7678
} else if (entity instanceof Catalog[]) {
7779
new CatalogListTableFormat(context).output((Catalog[]) entity);
80+
} else if (entity instanceof Schema) {
81+
new SchemaTableFormat(context).output((Schema) entity);
82+
} else if (entity instanceof Schema[]) {
83+
new SchemaListTableFormat(context).output((Schema[]) entity);
84+
} else if (entity instanceof Table) {
85+
new TableDetailsTableFormat(context).output((Table) entity);
86+
} else if (entity instanceof Table[]) {
87+
new TableListTableFormat(context).output((Table[]) entity);
7888
} else {
7989
throw new IllegalArgumentException("Unsupported object type");
8090
}
@@ -540,4 +550,97 @@ public String getOutput(Catalog[] catalogs) {
540550
return getTableFormat(columnName);
541551
}
542552
}
553+
554+
/**
555+
* Formats a single {@link Schema} instance into a two-column table display. Displays catalog
556+
* details including name and comment information.
557+
*/
558+
static final class SchemaTableFormat extends TableFormat<Schema> {
559+
public SchemaTableFormat(CommandContext context) {
560+
super(context);
561+
}
562+
563+
/** {@inheritDoc} */
564+
@Override
565+
public String getOutput(Schema schema) {
566+
Column columnName = new Column(context, "schema");
567+
Column columnComment = new Column(context, "comment");
568+
569+
columnName.addCell(schema.name());
570+
columnComment.addCell(schema.comment());
571+
572+
return getTableFormat(columnName, columnComment);
573+
}
574+
}
575+
576+
/**
577+
* Formats an array of Schemas into a single-column table display. Lists all schema names in a
578+
* vertical format.
579+
*/
580+
static final class SchemaListTableFormat extends TableFormat<Schema[]> {
581+
public SchemaListTableFormat(CommandContext context) {
582+
super(context);
583+
}
584+
585+
/** {@inheritDoc} */
586+
@Override
587+
public String getOutput(Schema[] schemas) {
588+
Column column = new Column(context, "schema");
589+
Arrays.stream(schemas).forEach(schema -> column.addCell(schema.name()));
590+
591+
return getTableFormat(column);
592+
}
593+
}
594+
595+
/**
596+
* Formats a single {@link Table} instance into a two-column table display. Displays table details
597+
* including name and comment information.
598+
*/
599+
static final class TableDetailsTableFormat extends TableFormat<Table> {
600+
public TableDetailsTableFormat(CommandContext context) {
601+
super(context);
602+
}
603+
604+
/** {@inheritDoc} */
605+
@Override
606+
public String getOutput(Table table) {
607+
Column columnName = new Column(context, "name");
608+
Column columnType = new Column(context, "type");
609+
Column columnAutoIncrement = new Column(context, "AutoIncrement");
610+
Column columnNullable = new Column(context, "nullable");
611+
Column columnComment = new Column(context, "comment");
612+
613+
org.apache.gravitino.rel.Column[] columns = table.columns();
614+
for (org.apache.gravitino.rel.Column column : columns) {
615+
columnName.addCell(column.name());
616+
columnType.addCell(column.dataType().simpleString());
617+
columnAutoIncrement.addCell(column.autoIncrement());
618+
columnNullable.addCell(column.nullable());
619+
columnComment.addCell(
620+
column.comment() == null || column.comment().isEmpty() ? "N/A" : column.comment());
621+
}
622+
623+
return getTableFormat(
624+
columnName, columnType, columnAutoIncrement, columnNullable, columnComment);
625+
}
626+
}
627+
628+
/**
629+
* Formats an array of {@link Table} into a single-column table display. Lists all table names in
630+
* a vertical format.
631+
*/
632+
static final class TableListTableFormat extends TableFormat<Table[]> {
633+
public TableListTableFormat(CommandContext context) {
634+
super(context);
635+
}
636+
637+
/** {@inheritDoc} */
638+
@Override
639+
public String getOutput(Table[] tables) {
640+
Column column = new Column(context, "table");
641+
Arrays.stream(tables).forEach(table -> column.addCell(table.name()));
642+
643+
return getTableFormat(column);
644+
}
645+
}
543646
}

0 commit comments

Comments
 (0)