|
| 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 | +} |
0 commit comments