Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#7224] improvement(cli): Add ability to create a Fileset with multiple locations in the Gravitino CLI (#7768)

Lord of Abyss31cb0940aa96
diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/FilesetCommandHandler.java b/clients/cli/src/main/java/org/apache/gravitino/cli/FilesetCommandHandler.java
index bdcc809ac9..bb8862e2e7 100644
--- a/clients/cli/src/main/java/org/apache/gravitino/cli/FilesetCommandHandler.java
+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/FilesetCommandHandler.java
@@ -139,7 +139,7 @@ public class FilesetCommandHandler extends CommandHandler {
/** Handles the "CREATE" command. */
private void handleCreateCommand() {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
- String[] properties = line.getOptionValues(CommandActions.PROPERTIES);
+ String[] properties = line.getOptionValues(GravitinoOptions.PROPERTIES);
Map<String, String> propertyMap = new Properties().parse(properties);
gravitinoCommandLine
.newCreateFileset(context, metalake, catalog, schema, fileset, comment, propertyMap)
diff --git a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CreateFileset.java b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CreateFileset.java
index e50e3f9168..10f77a76fa 100644
--- a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CreateFileset.java
+++ b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/CreateFileset.java
@@ -29,6 +29,7 @@ import org.apache.gravitino.exceptions.NoSuchCatalogException;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
import org.apache.gravitino.file.Fileset;
+import org.apache.gravitino.utils.MapUtils;
public class CreateFileset extends Command {
protected final String metalake;
@@ -70,8 +71,11 @@ public class CreateFileset extends Command {
@Override
public void handle() {
NameIdentifier name = NameIdentifier.of(schema, fileset);
- boolean managed = properties.get("managed").equals("true");
- String location = properties.get("location");
+ boolean managed = "true".equals(properties.get("managed"));
+ Map<String, String> storageLocations = MapUtils.getPrefixMap(properties, "location-", true);
+ Map<String, String> propertiesWithoutLocation =
+ MapUtils.getMapWithoutPrefix(properties, "location-");
+
Fileset.Type filesetType = managed ? Fileset.Type.MANAGED : Fileset.Type.EXTERNAL;
try {
@@ -79,7 +83,8 @@ public class CreateFileset extends Command {
client
.loadCatalog(catalog)
.asFilesetCatalog()
- .createFileset(name, comment, filesetType, location, null);
+ .createMultipleLocationFileset(
+ name, comment, filesetType, storageLocations, propertiesWithoutLocation);
} catch (NoSuchMetalakeException err) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (NoSuchCatalogException err) {
@@ -94,4 +99,13 @@ public class CreateFileset extends Command {
printInformation(fileset + " created");
}
+
+ @Override
+ public Command validate() {
+ if (!properties.containsKey("managed")) {
+ exitWithError("Missing property 'managed'");
+ }
+
+ return this;
+ }
}
diff --git a/clients/cli/src/test/java/org/apache/gravitino/cli/TestFilesetCommands.java b/clients/cli/src/test/java/org/apache/gravitino/cli/TestFilesetCommands.java
index 1b73958c98..8f875ebf0c 100644
--- a/clients/cli/src/test/java/org/apache/gravitino/cli/TestFilesetCommands.java
+++ b/clients/cli/src/test/java/org/apache/gravitino/cli/TestFilesetCommands.java
@@ -48,6 +48,7 @@ import org.apache.gravitino.cli.commands.SetFilesetProperty;
import org.apache.gravitino.cli.commands.UpdateFilesetComment;
import org.apache.gravitino.cli.commands.UpdateFilesetName;
import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -153,6 +154,27 @@ class TestFilesetCommands {
verify(mockCreate).handle();
}
+ @Test
+ void testCreateFilesetCommandWithoutManagedType() {
+ Main.useExit = false;
+ when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);
+ when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");
+ when(mockCommandLine.hasOption(GravitinoOptions.NAME)).thenReturn(true);
+ when(mockCommandLine.getOptionValue(GravitinoOptions.NAME))
+ .thenReturn("catalog.schema.fileset");
+ when(mockCommandLine.hasOption(GravitinoOptions.COMMENT)).thenReturn(true);
+ when(mockCommandLine.getOptionValue(GravitinoOptions.COMMENT)).thenReturn("comment");
+ when(mockCommandLine.hasOption(GravitinoOptions.PROPERTIES)).thenReturn(true);
+ when(mockCommandLine.getOptionValues(GravitinoOptions.PROPERTIES))
+ .thenReturn(new String[] {"key1=value1", "key2=value2"});
+ GravitinoCommandLine commandLine =
+ spy(
+ new GravitinoCommandLine(
+ mockCommandLine, mockOptions, CommandEntities.FILESET, CommandActions.CREATE));
+
+ Assertions.assertThrowsExactly(RuntimeException.class, commandLine::handleCommandLine);
+ }
+
@Test
void testDeleteFilesetCommand() {
DeleteFileset mockDelete = mock(DeleteFileset.class);
diff --git a/common/src/main/java/org/apache/gravitino/utils/MapUtils.java b/common/src/main/java/org/apache/gravitino/utils/MapUtils.java
index 0549df13d8..fcc8b5862e 100644
--- a/common/src/main/java/org/apache/gravitino/utils/MapUtils.java
+++ b/common/src/main/java/org/apache/gravitino/utils/MapUtils.java
@@ -23,6 +23,7 @@ import com.google.common.collect.Maps;
import java.util.Collections;
import java.util.Map;
import java.util.function.Predicate;
+import java.util.stream.Collectors;
/** Utility class for working with maps. */
public class MapUtils {
@@ -36,16 +37,49 @@ public class MapUtils {
* @return A map with all keys that start with the given prefix.
*/
public static Map<String, String> getPrefixMap(Map<String, String> m, String prefix) {
- Map<String, String> configs = Maps.newHashMap();
- m.forEach(
- (k, v) -> {
- if (k.startsWith(prefix)) {
- String newKey = k.substring(prefix.length());
- configs.put(newKey, v);
- }
- });
+ return getPrefixMap(m, prefix, false);
+ }
- return Collections.unmodifiableMap(configs);
+ /**
+ * Returns a map with all keys that start with the given prefix.
+ *
+ * @param m The map to filter.
+ * @param prefix The prefix to filter by.
+ * @param keepPrefix Whether to keep the prefix in the key or not.
+ * @return A map with all keys that start with the given prefix.
+ */
+ public static Map<String, String> getPrefixMap(
+ Map<String, String> m, String prefix, boolean keepPrefix) {
+ if (m == null || prefix == null) {
+ throw new IllegalArgumentException("Map and prefix cannot be null");
+ }
+
+ return m.entrySet().stream()
+ .filter(entry -> entry.getKey() != null && entry.getKey().startsWith(prefix))
+ .collect(
+ Collectors.collectingAndThen(
+ Collectors.toMap(
+ entry ->
+ keepPrefix ? entry.getKey() : entry.getKey().substring(prefix.length()),
+ Map.Entry::getValue),
+ Collections::unmodifiableMap));
+ }
+
+ /**
+ * Returns a new map containing entries whose keys do NOT start with the given prefix.
+ *
+ * @param m the original map
+ * @param prefix the prefix to exclude
+ * @return a filtered map without entries whose keys start with the prefix
+ */
+ public static Map<String, String> getMapWithoutPrefix(Map<String, String> m, String prefix) {
+ if (m == null || prefix == null) {
+ throw new IllegalArgumentException("Map and prefix cannot be null");
+ }
+
+ return m.entrySet().stream()
+ .filter(entry -> entry.getKey() == null || !entry.getKey().startsWith(prefix))
+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
/**
diff --git a/common/src/test/java/org/apache/gravitino/utils/TestMapUtils.java b/common/src/test/java/org/apache/gravitino/utils/TestMapUtils.java
index 2941cc312b..b8108eeb9a 100644
--- a/common/src/test/java/org/apache/gravitino/utils/TestMapUtils.java
+++ b/common/src/test/java/org/apache/gravitino/utils/TestMapUtils.java
@@ -29,13 +29,49 @@ public class TestMapUtils {
@Test
public void testGetPrefixMap() {
Map configs = ImmutableMap.of("a.b", "", "a.c", "", "", "", "b.a", "");
+ Assertions.assertEquals(
+ ImmutableMap.of("a.b", "", "a.c", ""), MapUtils.getPrefixMap(configs, "a.", true));
+ Assertions.assertEquals(
+ ImmutableMap.of("b", "", "c", ""), MapUtils.getPrefixMap(configs, "a.", false));
+
+ Assertions.assertEquals(ImmutableMap.of("b.a", ""), MapUtils.getPrefixMap(configs, "b.", true));
+ Assertions.assertEquals(ImmutableMap.of("a", ""), MapUtils.getPrefixMap(configs, "b.", false));
- Assertions.assertEquals(ImmutableMap.of(), MapUtils.getPrefixMap(configs, "xx"));
+ Assertions.assertEquals(configs, MapUtils.getPrefixMap(configs, "", true));
+ Assertions.assertEquals(configs, MapUtils.getPrefixMap(configs, "", false));
+
+ Assertions.assertThrowsExactly(
+ IllegalArgumentException.class, () -> MapUtils.getPrefixMap(configs, null, true));
Assertions.assertThrowsExactly(
- NullPointerException.class, () -> MapUtils.getPrefixMap(configs, null));
+ IllegalArgumentException.class, () -> MapUtils.getPrefixMap(null, "", true));
+ }
+
+ @Test
+ void testGetMapWithoutPrefix() {
+ Map<String, String> properties =
+ ImmutableMap.of(
+ "location-a",
+ "path-a",
+ "location-b",
+ "path-b",
+ "location-c",
+ "path-c",
+ "property-a",
+ "value-a",
+ "property-b",
+ "value-b",
+ "property-c",
+ "value-c");
+
Assertions.assertEquals(
- ImmutableMap.of("b", "", "c", ""), MapUtils.getPrefixMap(configs, "a."));
- Assertions.assertEquals(ImmutableMap.of("a", ""), MapUtils.getPrefixMap(configs, "b."));
- Assertions.assertEquals(configs, MapUtils.getPrefixMap(configs, ""));
+ ImmutableMap.of("property-a", "value-a", "property-b", "value-b", "property-c", "value-c"),
+ MapUtils.getMapWithoutPrefix(properties, "location-"));
+
+ Assertions.assertEquals(
+ ImmutableMap.of("location-a", "path-a", "location-b", "path-b", "location-c", "path-c"),
+ MapUtils.getMapWithoutPrefix(properties, "property-"));
+ Assertions.assertEquals(properties, MapUtils.getMapWithoutPrefix(properties, "unknown-"));
+ Assertions.assertThrowsExactly(
+ IllegalArgumentException.class, () -> MapUtils.getMapWithoutPrefix(properties, null));
}
}