Skip to content

Commit bfb8568

Browse files
authored
[#5960] fix(CLI): Add register and link commands to CLI for model (#6066)
### What changes were proposed in this pull request? Add register and link commands to CLI for model - register a model:`model create` - link a model:`model update <—uri uri> [--alias aliaA aliaB]` meantime, add two options - `—uri` :The URI of the model version artifact. - `—alias` :The aliases of the model version. The documentation will be updated after #6047 merge and I will create a new issue. ### Why are the changes needed? Fix: #5960 ### Does this PR introduce _any_ user-facing change? NO ### How was this patch tested? #### register test ```bash # register a model gcli model create -m demo_metalake --name Hive_catalog.default.model # register a model with comment gcli model create -m demo_metalake --name Hive_catalog.default.model --comment comment # register a model with properties gcli model create -m demo_metalake --name Hive_catalog.default.model --properties key1=val1 key2=val2 # register a model with properties and comment gcli model create -m demo_metalake --name Hive_catalog.default.model --properties key1=val1 klinkey2=val2 --comment comment ``` #### link test ```bash # link a model gcli model update -m demo_metalake --name Hive_catalog.default.model --uri file:///tmp/file # link a model with alias gcli model update -m demo_metalake --name Hive_catalog.default.model --uri file:///tmp/file --alias aliasA aliasB # link a model with all component gcli model update -m demo_metalake --name Hive_catalog.default.model --uri file:///tmp/file --alias aliasA aliasB --comment comment --properties key1=val1 key2=val2 # link a model without uri gcli model update -m demo_metalake --name Hive_catalog.default.model ```
1 parent e98498e commit bfb8568

File tree

7 files changed

+564
-1
lines changed

7 files changed

+564
-1
lines changed

clients/cli/src/main/java/org/apache/gravitino/cli/ErrorMessages.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class ErrorMessages {
3636
public static final String MISSING_USER = "Missing --user option.";
3737
public static final String MISSING_ROLE = "Missing --role option.";
3838
public static final String MISSING_TAG = "Missing --tag option.";
39+
public static final String MISSING_URI = "Missing --uri option.";
3940
public static final String METALAKE_EXISTS = "Metalake already exists.";
4041
public static final String CATALOG_EXISTS = "Catalog already exists.";
4142
public static final String SCHEMA_EXISTS = "Schema already exists.";
@@ -51,13 +52,13 @@ public class ErrorMessages {
5152
public static final String COLUMN_EXISTS = "Column already exists.";
5253
public static final String UNKNOWN_TOPIC = "Unknown topic.";
5354
public static final String TOPIC_EXISTS = "Topic already exists.";
55+
public static final String MODEL_EXISTS = "Model already exists.";
5456
public static final String UNKNOWN_FILESET = "Unknown fileset.";
5557
public static final String FILESET_EXISTS = "Fileset already exists.";
5658
public static final String TAG_EMPTY = "Error: Must configure --tag option.";
5759
public static final String UNKNOWN_ROLE = "Unknown role.";
5860
public static final String ROLE_EXISTS = "Role already exists.";
5961
public static final String TABLE_EXISTS = "Table already exists.";
60-
public static final String MODEL_EXISTS = "Model already exists.";
6162
public static final String INVALID_SET_COMMAND =
6263
"Unsupported combination of options either use --name, --user, --group or --property and --value.";
6364
public static final String INVALID_REMOVE_COMMAND =

clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java

+34
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,40 @@ private void handleModelCommand() {
11921192
}
11931193
break;
11941194

1195+
case CommandActions.CREATE:
1196+
String createComment = line.getOptionValue(GravitinoOptions.COMMENT);
1197+
String[] createProperties = line.getOptionValues(GravitinoOptions.PROPERTIES);
1198+
Map<String, String> createPropertyMap = new Properties().parse(createProperties);
1199+
newCreateModel(
1200+
url, ignore, metalake, catalog, schema, model, createComment, createPropertyMap)
1201+
.handle();
1202+
break;
1203+
1204+
case CommandActions.UPDATE:
1205+
String[] alias = line.getOptionValues(GravitinoOptions.ALIAS);
1206+
String uri = line.getOptionValue(GravitinoOptions.URI);
1207+
if (uri == null) {
1208+
System.err.println(ErrorMessages.MISSING_URI);
1209+
Main.exit(-1);
1210+
}
1211+
1212+
String linkComment = line.getOptionValue(GravitinoOptions.COMMENT);
1213+
String[] linkProperties = line.getOptionValues(CommandActions.PROPERTIES);
1214+
Map<String, String> linkPropertityMap = new Properties().parse(linkProperties);
1215+
newLinkModel(
1216+
url,
1217+
ignore,
1218+
metalake,
1219+
catalog,
1220+
schema,
1221+
model,
1222+
uri,
1223+
alias,
1224+
linkComment,
1225+
linkPropertityMap)
1226+
.handle();
1227+
break;
1228+
11951229
default:
11961230
System.err.println(ErrorMessages.UNSUPPORTED_ACTION);
11971231
break;

clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoOptions.java

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public class GravitinoOptions {
6262
public static final String ALL = "all";
6363
public static final String ENABLE = "enable";
6464
public static final String DISABLE = "disable";
65+
public static final String ALIAS = "alias";
66+
public static final String URI = "uri";
6567

6668
/**
6769
* Builds and returns the CLI options for Gravitino.
@@ -109,6 +111,10 @@ public Options options() {
109111
options.addOption(createArgOption(COLUMNFILE, "CSV file describing columns"));
110112
options.addOption(createSimpleOption(null, ALL, "all operation for --enable"));
111113

114+
// model options
115+
options.addOption(createArgOption(null, URI, "model version artifact"));
116+
options.addOption(createArgsOption(null, ALIAS, "model aliases"));
117+
112118
// Options that support multiple values
113119
options.addOption(createArgsOption("p", PROPERTIES, "property name/value pairs"));
114120
options.addOption(createArgsOption("t", TAG, "tag name"));

clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java

+29
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.apache.gravitino.cli.commands.GrantPrivilegesToRole;
5656
import org.apache.gravitino.cli.commands.GroupAudit;
5757
import org.apache.gravitino.cli.commands.GroupDetails;
58+
import org.apache.gravitino.cli.commands.LinkModel;
5859
import org.apache.gravitino.cli.commands.ListAllTags;
5960
import org.apache.gravitino.cli.commands.ListCatalogProperties;
6061
import org.apache.gravitino.cli.commands.ListCatalogs;
@@ -83,6 +84,7 @@
8384
import org.apache.gravitino.cli.commands.ModelAudit;
8485
import org.apache.gravitino.cli.commands.ModelDetails;
8586
import org.apache.gravitino.cli.commands.OwnerDetails;
87+
import org.apache.gravitino.cli.commands.RegisterModel;
8688
import org.apache.gravitino.cli.commands.RemoveAllTags;
8789
import org.apache.gravitino.cli.commands.RemoveCatalogProperty;
8890
import org.apache.gravitino.cli.commands.RemoveFilesetProperty;
@@ -925,4 +927,31 @@ protected ModelDetails newModelDetails(
925927
String url, boolean ignore, String metalake, String catalog, String schema, String model) {
926928
return new ModelDetails(url, ignore, metalake, catalog, schema, model);
927929
}
930+
931+
protected RegisterModel newCreateModel(
932+
String url,
933+
boolean ignore,
934+
String metalake,
935+
String catalog,
936+
String schema,
937+
String model,
938+
String comment,
939+
Map<String, String> properties) {
940+
return new RegisterModel(url, ignore, metalake, catalog, schema, model, comment, properties);
941+
}
942+
943+
protected LinkModel newLinkModel(
944+
String url,
945+
boolean ignore,
946+
String metalake,
947+
String catalog,
948+
String schema,
949+
String model,
950+
String uri,
951+
String[] alias,
952+
String comment,
953+
Map<String, String> properties) {
954+
return new LinkModel(
955+
url, ignore, metalake, catalog, schema, model, uri, alias, comment, properties);
956+
}
928957
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.gravitino.cli.commands;
20+
21+
/** Link a new model version to the registered model. */
22+
import java.util.Arrays;
23+
import java.util.Map;
24+
import org.apache.gravitino.NameIdentifier;
25+
import org.apache.gravitino.cli.ErrorMessages;
26+
import org.apache.gravitino.client.GravitinoClient;
27+
import org.apache.gravitino.exceptions.ModelVersionAliasesAlreadyExistException;
28+
import org.apache.gravitino.exceptions.NoSuchCatalogException;
29+
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
30+
import org.apache.gravitino.exceptions.NoSuchModelException;
31+
import org.apache.gravitino.exceptions.NoSuchSchemaException;
32+
import org.apache.gravitino.model.ModelCatalog;
33+
34+
public class LinkModel extends Command {
35+
protected final String metalake;
36+
protected final String catalog;
37+
protected final String schema;
38+
protected final String model;
39+
protected final String uri;
40+
protected final String[] alias;
41+
protected final String comment;
42+
protected final Map<String, String> properties;
43+
44+
/**
45+
* Link a new model version to the registered model.
46+
*
47+
* @param url The URL of the Gravitino server.
48+
* @param ignoreVersions If true don't check the client/server versions match.
49+
* @param metalake The name of the metalake.
50+
* @param catalog The name of the catalog.
51+
* @param schema The name of schema.
52+
* @param model The name of model.
53+
* @param uri The URI of the model version artifact.
54+
* @param alias The aliases of the model version.
55+
* @param comment The comment of the model version.
56+
* @param properties The properties of the model version.
57+
*/
58+
public LinkModel(
59+
String url,
60+
boolean ignoreVersions,
61+
String metalake,
62+
String catalog,
63+
String schema,
64+
String model,
65+
String uri,
66+
String[] alias,
67+
String comment,
68+
Map<String, String> properties) {
69+
super(url, ignoreVersions);
70+
this.metalake = metalake;
71+
this.catalog = catalog;
72+
this.schema = schema;
73+
this.model = model;
74+
this.uri = uri;
75+
this.alias = alias;
76+
this.comment = comment;
77+
this.properties = properties;
78+
}
79+
80+
/** Link a new model version to the registered model. */
81+
@Override
82+
public void handle() {
83+
NameIdentifier name = NameIdentifier.of(schema, model);
84+
85+
try {
86+
GravitinoClient client = buildClient(metalake);
87+
ModelCatalog modelCatalog = client.loadCatalog(catalog).asModelCatalog();
88+
modelCatalog.linkModelVersion(name, uri, alias, comment, properties);
89+
} catch (NoSuchMetalakeException err) {
90+
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
91+
} catch (NoSuchCatalogException err) {
92+
exitWithError(ErrorMessages.UNKNOWN_CATALOG);
93+
} catch (NoSuchSchemaException err) {
94+
exitWithError(ErrorMessages.UNKNOWN_SCHEMA);
95+
} catch (NoSuchModelException err) {
96+
exitWithError(ErrorMessages.UNKNOWN_MODEL);
97+
} catch (ModelVersionAliasesAlreadyExistException err) {
98+
exitWithError(Arrays.toString(alias) + " already exist.");
99+
} catch (Exception err) {
100+
exitWithError(err.getMessage());
101+
}
102+
103+
System.out.println(
104+
"Linked model " + model + " to " + uri + " with aliases " + Arrays.toString(alias));
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.gravitino.cli.commands;
21+
22+
import java.util.Map;
23+
import org.apache.gravitino.NameIdentifier;
24+
import org.apache.gravitino.cli.ErrorMessages;
25+
import org.apache.gravitino.cli.Main;
26+
import org.apache.gravitino.client.GravitinoClient;
27+
import org.apache.gravitino.exceptions.ModelAlreadyExistsException;
28+
import org.apache.gravitino.exceptions.NoSuchCatalogException;
29+
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
30+
import org.apache.gravitino.exceptions.NoSuchSchemaException;
31+
import org.apache.gravitino.model.Model;
32+
import org.apache.gravitino.model.ModelCatalog;
33+
34+
/** Register a model in the catalog */
35+
public class RegisterModel extends Command {
36+
37+
protected final String metalake;
38+
protected final String catalog;
39+
protected final String schema;
40+
protected final String model;
41+
protected final String comment;
42+
protected final Map<String, String> properties;
43+
44+
/**
45+
* Register a model in the catalog
46+
*
47+
* @param url The URL of the Gravitino server.
48+
* @param ignoreVersions If true don't check the client/server versions match.
49+
* @param metalake The name of the metalake.
50+
* @param catalog The name of the catalog.
51+
* @param schema The name of schema.
52+
* @param model The name of model.
53+
* @param comment The comment of the model version.
54+
* @param properties The properties of the model version.
55+
*/
56+
public RegisterModel(
57+
String url,
58+
boolean ignoreVersions,
59+
String metalake,
60+
String catalog,
61+
String schema,
62+
String model,
63+
String comment,
64+
Map<String, String> properties) {
65+
super(url, ignoreVersions);
66+
this.metalake = metalake;
67+
this.catalog = catalog;
68+
this.schema = schema;
69+
this.model = model;
70+
this.comment = comment;
71+
this.properties = properties;
72+
}
73+
74+
/** Register a model in the catalog */
75+
@Override
76+
public void handle() {
77+
NameIdentifier name = NameIdentifier.of(schema, model);
78+
Model registeredModel = null;
79+
80+
try {
81+
GravitinoClient client = buildClient(metalake);
82+
ModelCatalog modelCatalog = client.loadCatalog(catalog).asModelCatalog();
83+
registeredModel = modelCatalog.registerModel(name, comment, properties);
84+
} catch (NoSuchMetalakeException err) {
85+
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
86+
} catch (NoSuchCatalogException err) {
87+
exitWithError(ErrorMessages.UNKNOWN_CATALOG);
88+
} catch (NoSuchSchemaException err) {
89+
exitWithError(ErrorMessages.UNKNOWN_SCHEMA);
90+
} catch (ModelAlreadyExistsException err) {
91+
exitWithError(ErrorMessages.MODEL_EXISTS);
92+
} catch (Exception err) {
93+
exitWithError(err.getMessage());
94+
}
95+
96+
if (registeredModel != null) {
97+
System.out.println("Successful register " + registeredModel.name() + ".");
98+
} else {
99+
System.err.println("Failed to register model: " + model + ".");
100+
Main.exit(-1);
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)