tengqm commented on code in PR #5717: URL: https://github.com/apache/gravitino/pull/5717#discussion_r1868533351
########## clients/cli/src/main/java/org/apache/gravitino/cli/ReadTableCSV.java: ########## @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.cli; + +import java.io.BufferedReader; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; +import org.apache.commons.csv.CSVRecord; +import org.apache.gravitino.rel.Column; + +public class ReadTableCSV { + + private enum ExpectedColumns { + NAME("Name"), + DATATYPE("Datatype"), + COMMENT("Comment"), + NULLABLE("Nullable"), + AUTOINCREMENT("AutoIncrement"), + DEFAULTVALUE("DefaultValue"), + DEFAULTTYPE("DefaultType"); + + private final String name; + + ExpectedColumns(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } + + public Column[] columns(Map<String, List<String>> tableData) { + List<String> names = tableData.get(ExpectedColumns.NAME.getName()); + List<String> datatypes = tableData.get(ExpectedColumns.DATATYPE.getName()); + List<String> comments = tableData.get(ExpectedColumns.COMMENT.getName()); + List<String> nullables = tableData.get(ExpectedColumns.NULLABLE.getName()); + List<String> autos = tableData.get(ExpectedColumns.AUTOINCREMENT.getName()); Review Comment: ```suggestion List<String> autoIncs = tableData.get(ExpectedColumns.AUTOINCREMENT.getName()); ``` ########## clients/cli/src/main/java/org/apache/gravitino/cli/ReadTableCSV.java: ########## @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.cli; + +import java.io.BufferedReader; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; +import org.apache.commons.csv.CSVRecord; +import org.apache.gravitino.rel.Column; + +public class ReadTableCSV { + + private enum ExpectedColumns { + NAME("Name"), + DATATYPE("Datatype"), + COMMENT("Comment"), + NULLABLE("Nullable"), + AUTOINCREMENT("AutoIncrement"), + DEFAULTVALUE("DefaultValue"), + DEFAULTTYPE("DefaultType"); + + private final String name; + + ExpectedColumns(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } + + public Column[] columns(Map<String, List<String>> tableData) { + List<String> names = tableData.get(ExpectedColumns.NAME.getName()); + List<String> datatypes = tableData.get(ExpectedColumns.DATATYPE.getName()); + List<String> comments = tableData.get(ExpectedColumns.COMMENT.getName()); + List<String> nullables = tableData.get(ExpectedColumns.NULLABLE.getName()); + List<String> autos = tableData.get(ExpectedColumns.AUTOINCREMENT.getName()); + List<String> defaultTypes = tableData.get(ExpectedColumns.DEFAULTTYPE.getName()); + List<String> defaulValues = tableData.get(ExpectedColumns.DEFAULTVALUE.getName()); + int size = names.size(); + Column[] columns = new Column[size]; + + for (int i = 0; i < size; i++) { + String columnName = names.get(i); + String datatype = datatypes.get(i); + String columnComment = comments.get(i); Review Comment: ```suggestion String comment = comments.get(i); ``` ? ########## clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoOptions.java: ########## @@ -50,6 +50,7 @@ public class GravitinoOptions { public static final String ROLE = "role"; public static final String AUDIT = "audit"; public static final String FORCE = "force"; + public static final String COLUMNFILE = "csv"; Review Comment: ```suggestion public static final String COLUMNFILE = "columnfile"; ``` ########## clients/cli/src/main/java/org/apache/gravitino/cli/ReadTableCSV.java: ########## @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.cli; + +import java.io.BufferedReader; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; +import org.apache.commons.csv.CSVRecord; +import org.apache.gravitino.rel.Column; + +public class ReadTableCSV { + + private enum ExpectedColumns { + NAME("Name"), + DATATYPE("Datatype"), + COMMENT("Comment"), + NULLABLE("Nullable"), + AUTOINCREMENT("AutoIncrement"), + DEFAULTVALUE("DefaultValue"), + DEFAULTTYPE("DefaultType"); + + private final String name; + + ExpectedColumns(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } + + public Column[] columns(Map<String, List<String>> tableData) { + List<String> names = tableData.get(ExpectedColumns.NAME.getName()); + List<String> datatypes = tableData.get(ExpectedColumns.DATATYPE.getName()); + List<String> comments = tableData.get(ExpectedColumns.COMMENT.getName()); + List<String> nullables = tableData.get(ExpectedColumns.NULLABLE.getName()); + List<String> autos = tableData.get(ExpectedColumns.AUTOINCREMENT.getName()); + List<String> defaultTypes = tableData.get(ExpectedColumns.DEFAULTTYPE.getName()); + List<String> defaulValues = tableData.get(ExpectedColumns.DEFAULTVALUE.getName()); + int size = names.size(); + Column[] columns = new Column[size]; + + for (int i = 0; i < size; i++) { + String columnName = names.get(i); + String datatype = datatypes.get(i); + String columnComment = comments.get(i); + boolean nullable = nullables.get(i).equals("true"); + boolean auto = autos.get(i).equals("true"); + String defaultValue = defaulValues.get(i); + String defaultType = defaultTypes.get(i); + + if (defaultType == null || defaultType.isEmpty()) { + defaultType = datatype; + } Review Comment: Please double confirm if this is the correct logic. ########## clients/cli/src/main/java/org/apache/gravitino/cli/commands/CreateTable.java: ########## @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.cli.commands; + +import java.util.List; +import java.util.Map; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.cli.ErrorMessages; +import org.apache.gravitino.cli.ReadTableCSV; +import org.apache.gravitino.client.GravitinoClient; +import org.apache.gravitino.exceptions.NoSuchCatalogException; +import org.apache.gravitino.exceptions.NoSuchMetalakeException; +import org.apache.gravitino.exceptions.NoSuchSchemaException; +import org.apache.gravitino.exceptions.TableAlreadyExistsException; +import org.apache.gravitino.rel.Column; + +public class CreateTable extends Command { + protected final String metalake; + protected final String catalog; + protected final String schema; + protected final String table; + protected final String columnFile; + protected final String comment; + + /** + * Create a new table. + * + * @param url The URL of the Gravitino server. + * @param ignoreVersions If true don't check the client/server versions match. + * @param metalake The name of the metalake. + * @param catalog The name of the catalog. + * @param schema The name of the schema. + * @param table The name of the table. + * @param columnFile The file name containing the CSV column info. + * @param comment The table's comment. + */ + public CreateTable( + String url, + boolean ignoreVersions, + String metalake, + String catalog, + String schema, + String table, + String columnFile, + String comment) { + super(url, ignoreVersions); + this.metalake = metalake; + this.catalog = catalog; + this.schema = schema; + this.table = table; + this.columnFile = columnFile; + this.comment = comment; + } + + /** Create a new table. */ + @Override + public void handle() { + try { + NameIdentifier tableName = NameIdentifier.of(schema, table); + GravitinoClient client = buildClient(metalake); + ReadTableCSV readTableCSV = new ReadTableCSV(); + Map<String, List<String>> tableData = readTableCSV.parse(columnFile); + Column[] columns = readTableCSV.columns(tableData); + + client.loadCatalog(catalog).asTableCatalog().createTable(tableName, columns, comment, null); + } catch (NoSuchMetalakeException err) { Review Comment: Can/shall we split this try block? I'm asking because that these lines can throw many exceptions. It is difficult to maintain the mapping between the functions and the exceptions. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
