Skip to content

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

[#5611] Improvement(iceberg): support schema exists interface for Iceberg REST server (#5616)

JUNd8a6ff8152f3
diff --git a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java
index f1d3b178e8..de265ed34f 100644
--- a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java
+++ b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java
@@ -134,6 +134,11 @@ public class IcebergCatalogWrapper implements AutoCloseable {
return CatalogHandlers.loadNamespace(asNamespaceCatalog, namespace);
}
+ public boolean existNamespace(Namespace namespace) {
+ validateNamespace(Optional.of(namespace));
+ return asNamespaceCatalog.namespaceExists(namespace);
+ }
+
public ListNamespacesResponse listNamespace(Namespace parent) {
validateNamespace(Optional.empty());
return CatalogHandlers.listNamespaces(asNamespaceCatalog, parent);
diff --git a/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/rest/IcebergNamespaceOperations.java b/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/rest/IcebergNamespaceOperations.java
index 831a9b9072..c4e3a3fe6e 100644
--- a/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/rest/IcebergNamespaceOperations.java
+++ b/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/rest/IcebergNamespaceOperations.java
@@ -26,6 +26,7 @@ import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
+import javax.ws.rs.HEAD;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@@ -95,6 +96,24 @@ public class IcebergNamespaceOperations {
return IcebergRestUtils.ok(getNamespaceResponse);
}
+ @HEAD
+ @Path("{namespace}")
+ @Produces(MediaType.APPLICATION_JSON)
+ @Timed(name = "namespace-exists." + MetricNames.HTTP_PROCESS_DURATION, absolute = true)
+ @ResponseMetered(name = "namespace-exists", absolute = true)
+ public Response namespaceExists(
+ @PathParam("prefix") String prefix, @PathParam("namespace") String namespace) {
+ boolean exists =
+ icebergCatalogWrapperManager
+ .getOps(prefix)
+ .existNamespace(RESTUtil.decodeNamespace(namespace));
+ if (exists) {
+ return IcebergRestUtils.noContent();
+ } else {
+ return IcebergRestUtils.notExists();
+ }
+ }
+
@DELETE
@Path("{namespace}")
@Produces(MediaType.APPLICATION_JSON)
diff --git a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergNamespaceOperations.java b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergNamespaceOperations.java
index 6049a153e1..59e94442d1 100644
--- a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergNamespaceOperations.java
+++ b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergNamespaceOperations.java
@@ -93,6 +93,10 @@ public class TestIcebergNamespaceOperations extends IcebergTestBase {
return getNamespaceClientBuilder(Optional.of(name)).get();
}
+ private Response doNamespaceExists(String name) {
+ return getNamespaceClientBuilder(Optional.of(name)).head();
+ }
+
private Response doDropNamespace(String name) {
return getNamespaceClientBuilder(Optional.of(name)).delete();
}
@@ -121,6 +125,11 @@ public class TestIcebergNamespaceOperations extends IcebergTestBase {
Assertions.assertEquals(status, response.getStatus());
}
+ private void verifyNamespaceExistsStatusCode(int status, String name) {
+ Response response = doNamespaceExists(name);
+ Assertions.assertEquals(status, response.getStatus());
+ }
+
protected void verifyCreateNamespaceSucc(String... name) {
Response response = doCreateNamespace(name);
Assertions.assertEquals(Status.OK.getStatusCode(), response.getStatus());
@@ -167,6 +176,13 @@ public class TestIcebergNamespaceOperations extends IcebergTestBase {
verifyLoadNamespaceFail(404, "load_foo2");
}
+ @Test
+ void testNamespaceExists() {
+ verifyNamespaceExistsStatusCode(404, "exists_foo1");
+ verifyCreateNamespaceSucc("exists_foo1");
+ verifyNamespaceExistsStatusCode(204, "exists_foo1");
+ }
+
@Test
void testDropNamespace() {
verifyCreateNamespaceSucc("drop_foo1");