Skip to content

Commit

Permalink
[#5379] Add basic Fileset commands to Gravitno CLI (#5380)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Added basic Fileset commands to Gravitno CLI

### Why are the changes needed?

To expand Gravitino CLI support.

Fix: #5379

### Does this PR introduce _any_ user-facing change?

No, but it adds extra commands to the Gravitino CLI.

### How was this patch tested?

Compiled and tested locally.
  • Loading branch information
justinmclean authored Nov 29, 2024
1 parent e25a67a commit 7a42199
Show file tree
Hide file tree
Showing 14 changed files with 691 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class CommandEntities {
public static final String USER = "user";
public static final String GROUP = "group";
public static final String TAG = "tag";
public static final String FILESET = "fileset";
public static final String ROLE = "role";

private static final HashSet<String> VALID_ENTITIES = new HashSet<>();
Expand All @@ -47,6 +48,7 @@ public class CommandEntities {
VALID_ENTITIES.add(USER);
VALID_ENTITIES.add(GROUP);
VALID_ENTITIES.add(TAG);
VALID_ENTITIES.add(FILESET);
VALID_ENTITIES.add(ROLE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class ErrorMessages {
public static final String MULTIPLE_TAG_COMMAND_ERROR =
"Error: The current command only supports one --tag option.";
public static final String TAG_EXISTS = "Tag already exists.";
public static final String UNKNOWN_FILESET = "Unknown fileset.";
public static final String FILESET_EXISTS = "Fileset already exists.";
public static final String TAG_EMPTY = "Error: Must configure --tag option.";
public static final String UNKNOWN_ROLE = "Unknown role.";
public static final String ROLE_EXISTS = "Role already exists.";
Expand Down
15 changes: 12 additions & 3 deletions clients/cli/src/main/java/org/apache/gravitino/cli/FullName.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public String getMetalakeName() {
}

/**
* Retrieves the catalog name from the second part of the full name option.
* Retrieves the catalog name from the first part of the full name option.
*
* @return The catalog name, or null if not found.
*/
Expand All @@ -89,7 +89,7 @@ public String getCatalogName() {
}

/**
* Retrieves the schema name from the third part of the full name option.
* Retrieves the schema name from the second part of the full name option.
*
* @return The schema name, or null if not found.
*/
Expand All @@ -98,14 +98,23 @@ public String getSchemaName() {
}

/**
* Retrieves the table name from the fourth part of the full name option.
* Retrieves the table name from the third part of the full name option.
*
* @return The table name, or null if not found.
*/
public String getTableName() {
return getNamePart(2);
}

/**
* Retrieves the fileset name from the third part of the full name option.
*
* @return The table name, or null if not found.
*/
public String getFilesetName() {
return getNamePart(2);
}

/**
* Helper method to retrieve a specific part of the full name based on the position of the part.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ private void executeCommand() {
handleCatalogCommand();
} else if (entity.equals(CommandEntities.METALAKE)) {
handleMetalakeCommand();
} else if (entity.equals(CommandEntities.FILESET)) {
handleFilesetCommand();
} else if (entity.equals(CommandEntities.USER)) {
handleUserCommand();
} else if (entity.equals(CommandEntities.GROUP)) {
Expand Down Expand Up @@ -489,6 +491,33 @@ private void handleOwnerCommand() {
}
}

/**
* Handles the command execution for filesets based on command type and the command line options.
*/
private void handleFilesetCommand() {
String url = getUrl();
FullName name = new FullName(line);
String metalake = name.getMetalakeName();
String catalog = name.getCatalogName();
String schema = name.getSchemaName();
String fileset = name.getFilesetName();

if (CommandActions.DETAILS.equals(command)) {
newFilesetDetails(url, ignore, metalake, catalog, schema, fileset).handle();
} else if (CommandActions.LIST.equals(command)) {
newListFilesets(url, ignore, metalake, catalog, schema).handle();
} else if (CommandActions.CREATE.equals(command)) {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
String[] properties = line.getOptionValues(GravitinoOptions.PROPERTIES);
Map<String, String> propertyMap = new Properties().parse(properties);
newCreateFileset(url, ignore, metalake, catalog, schema, fileset, comment, propertyMap)
.handle();
} else if (CommandActions.DELETE.equals(command)) {
boolean force = line.hasOption(GravitinoOptions.FORCE);
newDeleteFileset(url, ignore, force, metalake, catalog, schema, fileset).handle();
}
}

/**
* 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 @@ -65,9 +65,9 @@ public Options options() {
options.addOption(createArgOption("m", METALAKE, "metalake name"));
options.addOption(createSimpleOption("i", IGNORE, "ignore client/sever version check"));
options.addOption(createSimpleOption("a", AUDIT, "display audit information"));
options.addOption(createSimpleOption("x", INDEX, "Display index infromation"));
options.addOption(createSimpleOption("d", DISTRIBUTION, "Display distribution information"));
options.addOption(createSimpleOption(null, PARTITION, "Display partition information"));
options.addOption(createSimpleOption("x", INDEX, "display index information"));
options.addOption(createSimpleOption("d", DISTRIBUTION, "display distribution information"));
options.addOption(createSimpleOption(null, PARTITION, "display partition information"));

// Create/update options
options.addOption(createArgOption(null, RENAME, "new entity name"));
Expand Down
18 changes: 10 additions & 8 deletions clients/cli/src/main/java/org/apache/gravitino/cli/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ public Properties(String delimiter, String keyValueSeparator) {
public Map<String, String> parse(String[] inputs) {
HashMap<String, String> map = new HashMap<>();

for (String input : inputs) {
// Split the input by the delimiter into key-value pairs
String[] pairs = input.split(delimiter);
for (String pair : pairs) {
// Split each key-value pair by the separator
String[] keyValue = pair.split(keyValueSeparator, 2);
if (keyValue.length == 2) {
map.put(keyValue[0].trim(), keyValue[1].trim());
if (inputs != null) {
for (String input : inputs) {
// Split the input by the delimiter into key-value pairs
String[] pairs = input.split(delimiter);
for (String pair : pairs) {
// Split each key-value pair by the separator
String[] keyValue = pair.split(keyValueSeparator, 2);
if (keyValue.length == 2) {
map.put(keyValue[0].trim(), keyValue[1].trim());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,30 @@
import org.apache.gravitino.cli.commands.CatalogDetails;
import org.apache.gravitino.cli.commands.ClientVersion;
import org.apache.gravitino.cli.commands.CreateCatalog;
import org.apache.gravitino.cli.commands.CreateFileset;
import org.apache.gravitino.cli.commands.CreateGroup;
import org.apache.gravitino.cli.commands.CreateMetalake;
import org.apache.gravitino.cli.commands.CreateRole;
import org.apache.gravitino.cli.commands.CreateSchema;
import org.apache.gravitino.cli.commands.CreateTag;
import org.apache.gravitino.cli.commands.CreateUser;
import org.apache.gravitino.cli.commands.DeleteCatalog;
import org.apache.gravitino.cli.commands.DeleteFileset;
import org.apache.gravitino.cli.commands.DeleteGroup;
import org.apache.gravitino.cli.commands.DeleteMetalake;
import org.apache.gravitino.cli.commands.DeleteRole;
import org.apache.gravitino.cli.commands.DeleteSchema;
import org.apache.gravitino.cli.commands.DeleteTable;
import org.apache.gravitino.cli.commands.DeleteTag;
import org.apache.gravitino.cli.commands.DeleteUser;
import org.apache.gravitino.cli.commands.FilesetDetails;
import org.apache.gravitino.cli.commands.GroupDetails;
import org.apache.gravitino.cli.commands.ListAllTags;
import org.apache.gravitino.cli.commands.ListCatalogProperties;
import org.apache.gravitino.cli.commands.ListCatalogs;
import org.apache.gravitino.cli.commands.ListColumns;
import org.apache.gravitino.cli.commands.ListEntityTags;
import org.apache.gravitino.cli.commands.ListFilesets;
import org.apache.gravitino.cli.commands.ListGroups;
import org.apache.gravitino.cli.commands.ListIndexes;
import org.apache.gravitino.cli.commands.ListMetalakeProperties;
Expand Down Expand Up @@ -442,4 +446,37 @@ protected OwnerDetails newOwnerDetails(
String url, boolean ignore, String metalake, String entity, String entityType) {
return new OwnerDetails(url, ignore, metalake, entity, entityType);
}

protected FilesetDetails newFilesetDetails(
String url, boolean ignore, String metalake, String catalog, String schema, String fileset) {
return new FilesetDetails(url, ignore, metalake, catalog, schema, fileset);
}

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

protected CreateFileset newCreateFileset(
String url,
boolean ignore,
String metalake,
String catalog,
String schema,
String fileset,
String comment,
Map<String, String> propertyMap) {
return new CreateFileset(url, ignore, metalake, catalog, schema, fileset, comment, propertyMap);
}

protected DeleteFileset newDeleteFileset(
String url,
boolean ignore,
boolean force,
String metalake,
String catalog,
String schema,
String fileset) {
return new DeleteFileset(url, ignore, force, metalake, catalog, schema, fileset);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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.Map;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
import org.apache.gravitino.exceptions.FilesetAlreadyExistsException;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
import org.apache.gravitino.file.Fileset;

public class CreateFileset extends Command {
protected final String metalake;
protected final String catalog;
protected final String schema;
protected final String fileset;
protected final String comment;
protected final Map<String, String> properties;

/**
* Create a new fileset.
*
* @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 fileset The name of the fileset.
* @param comment The fileset's comment.
* @param properties The catalog's properties.
*/
public CreateFileset(
String url,
boolean ignoreVersions,
String metalake,
String catalog,
String schema,
String fileset,
String comment,
Map<String, String> properties) {
super(url, ignoreVersions);
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
this.fileset = fileset;
this.comment = comment;
this.properties = properties;
}

/** Create a new fileset. */
@Override
public void handle() {
NameIdentifier name = NameIdentifier.of(schema, fileset);
boolean managed = properties.get("managed").equals("true");
String location = properties.get("location");
Fileset.Type filesetType = managed ? Fileset.Type.MANAGED : Fileset.Type.EXTERNAL;

try {
GravitinoClient client = buildClient(metalake);
client
.loadCatalog(catalog)
.asFilesetCatalog()
.createFileset(name, comment, filesetType, location, null);
} catch (NoSuchMetalakeException err) {
System.err.println(ErrorMessages.UNKNOWN_METALAKE);
return;
} catch (NoSuchCatalogException err) {
System.err.println(ErrorMessages.UNKNOWN_CATALOG);
return;
} catch (NoSuchSchemaException err) {
System.err.println(ErrorMessages.UNKNOWN_SCHEMA);
return;
} catch (FilesetAlreadyExistsException err) {
System.err.println(ErrorMessages.FILESET_EXISTS);
return;
} catch (Exception exp) {
System.err.println(exp.getMessage());
return;
}

System.out.println(fileset + " created");
}
}
Loading

0 comments on commit 7a42199

Please sign in to comment.