Skip to content

Commit

Permalink
Fixed merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
VigneshSK17 committed Jan 2, 2025
2 parents 2175486 + 539ac36 commit a0fad91
Show file tree
Hide file tree
Showing 9 changed files with 473 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public class CommandEntities {
public static final String SCHEMA = "schema";
public static final String TABLE = "table";
public static final String COLUMN = "column";
public static final String MODEL = "model";
public static final String USER = "user";
public static final String GROUP = "group";
public static final String TAG = "tag";
public static final String TOPIC = "topic";
public static final String FILESET = "fileset";
public static final String ROLE = "role";
public static final String MODEL = "model";

private static final HashSet<String> VALID_ENTITIES = new HashSet<>();

Expand All @@ -54,6 +54,7 @@ public class CommandEntities {
VALID_ENTITIES.add(TOPIC);
VALID_ENTITIES.add(FILESET);
VALID_ENTITIES.add(ROLE);
VALID_ENTITIES.add(MODEL);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public class ErrorMessages {
public static final String UNKNOWN_CATALOG = "Unknown catalog name.";
public static final String UNKNOWN_SCHEMA = "Unknown schema name.";
public static final String UNKNOWN_TABLE = "Unknown table name.";
public static final String UNKNOWN_MODEL = "Unknown model name.";
public static final String MALFORMED_NAME = "Malformed entity name.";
public static final String MISSING_NAME = "Missing --name option.";
public static final String MISSING_METALAKE = "Missing --metalake option.";
public static final String MISSING_GROUP = "Missing --group option.";
public static final String MISSING_USER = "Missing --user option.";
public static final String MISSING_ROLE = "Missing --role option.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ public String getMetalakeName() {
}
}

// Extract the metalake name from the full name option
if (line.hasOption(GravitinoOptions.NAME)) {
return line.getOptionValue(GravitinoOptions.NAME).split("\\.")[0];
}
System.err.println(ErrorMessages.MISSING_METALAKE);

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ private void executeCommand() {
handleTagCommand();
} else if (entity.equals(CommandEntities.ROLE)) {
handleRoleCommand();
} else if (entity.equals(CommandEntities.MODEL)) {
handleModelCommand();
}
}

Expand Down Expand Up @@ -1189,6 +1191,43 @@ private void handleFilesetCommand() {
}
}

private void handleModelCommand() {
String url = getUrl();
String auth = getAuth();
String userName = line.getOptionValue(GravitinoOptions.LOGIN);
FullName name = new FullName(line);
String metalake = name.getMetalakeName();
String catalog = name.getCatalogName();
String schema = name.getSchemaName();

Command.setAuthenticationMode(auth, userName);

List<String> missingEntities = Lists.newArrayList();
if (catalog == null) missingEntities.add(CommandEntities.CATALOG);
if (schema == null) missingEntities.add(CommandEntities.SCHEMA);

// Handle CommandActions.LIST action separately as it doesn't require the `model`
if (CommandActions.LIST.equals(command)) {
checkEntities(missingEntities);
newListModel(url, ignore, metalake, catalog, schema).handle();
return;
}

String model = name.getModelName();
if (model == null) missingEntities.add(CommandEntities.MODEL);
checkEntities(missingEntities);

switch (command) {
case CommandActions.DETAILS:
newModelDetails(url, ignore, metalake, catalog, schema, model).handle();
break;

default:
System.err.println(ErrorMessages.UNSUPPORTED_ACTION);
break;
}
}

/**
* Retrieves the Gravitinno URL from the command line options or the GRAVITINO_URL environment
* variable or the Gravitio config file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.apache.gravitino.cli.commands.ListIndexes;
import org.apache.gravitino.cli.commands.ListMetalakeProperties;
import org.apache.gravitino.cli.commands.ListMetalakes;
import org.apache.gravitino.cli.commands.ListModel;
import org.apache.gravitino.cli.commands.ListRoles;
import org.apache.gravitino.cli.commands.ListSchema;
import org.apache.gravitino.cli.commands.ListSchemaProperties;
Expand All @@ -80,6 +81,7 @@
import org.apache.gravitino.cli.commands.MetalakeDisable;
import org.apache.gravitino.cli.commands.MetalakeEnable;
import org.apache.gravitino.cli.commands.ModelAudit;
import org.apache.gravitino.cli.commands.ModelDetails;
import org.apache.gravitino.cli.commands.OwnerDetails;
import org.apache.gravitino.cli.commands.RemoveAllTags;
import org.apache.gravitino.cli.commands.RemoveCatalogProperty;
Expand Down Expand Up @@ -299,11 +301,6 @@ protected ListSchemaProperties newListSchemaProperties(
return new ListSchemaProperties(url, ignore, metalake, catalog, schema);
}

protected ModelAudit newModelAudit(
String url, boolean ignore, String metalake, String catalog, String schema, String model) {
return new ModelAudit(url, ignore, metalake, catalog, schema, model);
}

protected TableAudit newTableAudit(
String url, boolean ignore, String metalake, String catalog, String schema, String table) {
return new TableAudit(url, ignore, metalake, catalog, schema, table);
Expand Down Expand Up @@ -913,4 +910,19 @@ protected CatalogDisable newCatalogDisable(
String url, boolean ignore, String metalake, String catalog) {
return new CatalogDisable(url, ignore, metalake, catalog);
}

protected ListModel newListModel(
String url, boolean ignore, String metalake, String catalog, String schema) {
return new ListModel(url, ignore, metalake, catalog, schema);
}

protected ModelAudit newModelAudit(
String url, boolean ignore, String metalake, String catalog, String schema, String model) {
return new ModelAudit(url, ignore, metalake, catalog, schema, model);
}

protected ModelDetails newModelDetails(
String url, boolean ignore, String metalake, String catalog, String schema, String model) {
return new ModelDetails(url, ignore, metalake, catalog, schema, model);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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 com.google.common.base.Joiner;
import java.util.Arrays;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.Namespace;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;

/** List the names of all models in a schema. */
public class ListModel extends Command {
protected final String metalake;
protected final String catalog;
protected final String schema;

/**
* List the names of all models in a schema.
*
* @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 schema.
*/
public ListModel(
String url, boolean ignoreVersions, String metalake, String catalog, String schema) {
super(url, ignoreVersions);
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
}

/** List the names of all models in a schema. */
@Override
public void handle() {
NameIdentifier[] models = new NameIdentifier[0];
Namespace name = Namespace.of(schema);

try {
GravitinoClient client = buildClient(metalake);
models = client.loadCatalog(catalog).asModelCatalog().listModels(name);
} catch (NoSuchMetalakeException noSuchMetalakeException) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (NoSuchCatalogException noSuchCatalogException) {
exitWithError(ErrorMessages.UNKNOWN_CATALOG);
} catch (NoSuchSchemaException noSuchSchemaException) {
exitWithError(ErrorMessages.UNKNOWN_SCHEMA);
} catch (Exception err) {
exitWithError(err.getMessage());
}

String output =
models.length == 0
? "No models exist."
: Joiner.on(",").join(Arrays.stream(models).map(model -> model.name()).iterator());

System.out.println(output);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.Arrays;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
import org.apache.gravitino.exceptions.NoSuchModelException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
import org.apache.gravitino.model.Model;
import org.apache.gravitino.model.ModelCatalog;

/** Displays the details of a model. */
public class ModelDetails extends Command {
protected final String metalake;
protected final String catalog;
protected final String schema;
protected final String model;

/**
* Displays the details of a model.
*
* @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 schema.
* @param model The name of model.
*/
public ModelDetails(
String url,
boolean ignoreVersions,
String metalake,
String catalog,
String schema,
String model) {
super(url, ignoreVersions);
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
this.model = model;
}

/** Displays the details of a model. */
@Override
public void handle() {
NameIdentifier name = NameIdentifier.of(schema, model);
Model gModel = null;
int[] versions = new int[0];

try {
GravitinoClient client = buildClient(metalake);
ModelCatalog modelCatalog = client.loadCatalog(catalog).asModelCatalog();
gModel = modelCatalog.getModel(name);
versions = modelCatalog.listModelVersions(name);
} catch (NoSuchMetalakeException noSuchMetalakeException) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (NoSuchCatalogException noSuchCatalogException) {
exitWithError(ErrorMessages.UNKNOWN_CATALOG);
} catch (NoSuchSchemaException noSuchSchemaException) {
exitWithError(ErrorMessages.UNKNOWN_SCHEMA);
} catch (NoSuchModelException noSuchModelException) {
exitWithError(ErrorMessages.UNKNOWN_MODEL);
} catch (Exception err) {
exitWithError(err.getMessage());
}
String basicInfo =
String.format("Model name %s, latest version: %s%n", gModel.name(), gModel.latestVersion());
String versionInfo = Arrays.toString(versions);
System.out.printf(basicInfo + "versions: " + versionInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,28 @@ public void testMalformedName() throws ParseException {
String output = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(output, ErrorMessages.MALFORMED_NAME);
}

@Test
@SuppressWarnings("DefaultCharset")
public void testGetMetalake() throws ParseException {
String[] args = {
"table", "list", "-i", "-m", "demo_metalake", "--name", "Hive_catalog.default"
};
CommandLine commandLine = new DefaultParser().parse(options, args);
FullName fullName = new FullName(commandLine);
String metalakeName = fullName.getMetalakeName();
assertEquals(metalakeName, "demo_metalake");
}

@Test
@SuppressWarnings("DefaultCharset")
public void testGetMetalakeWithoutMetalakeOption() throws ParseException {
String[] args = {"table", "list", "-i", "--name", "Hive_catalog.default"};
CommandLine commandLine = new DefaultParser().parse(options, args);
FullName fullName = new FullName(commandLine);
String metalakeName = fullName.getMetalakeName();
assertNull(metalakeName);
String errOutput = new String(errContent.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(errOutput, ErrorMessages.MISSING_METALAKE);
}
}
Loading

0 comments on commit a0fad91

Please sign in to comment.