diff --git a/nessie/src/main/java/org/apache/iceberg/nessie/NessieCatalog.java b/nessie/src/main/java/org/apache/iceberg/nessie/NessieCatalog.java index a242bfeb67fa..2ca1660cf084 100644 --- a/nessie/src/main/java/org/apache/iceberg/nessie/NessieCatalog.java +++ b/nessie/src/main/java/org/apache/iceberg/nessie/NessieCatalog.java @@ -25,6 +25,8 @@ import java.util.Set; import java.util.UUID; import java.util.function.Function; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.iceberg.CatalogProperties; import org.apache.iceberg.CatalogUtil; import org.apache.iceberg.TableOperations; @@ -103,9 +105,12 @@ public void initialize(String name, Map options) { .fallbackTo(x -> options.get(removePrefix.apply(x))); NessieClientBuilder nessieClientBuilder = NessieClientBuilder.createClientBuilderFromSystemSettings(configSource); - // default version is set to v1. - final String apiVersion = - options.getOrDefault(removePrefix.apply(NessieUtil.CLIENT_API_VERSION), "1"); + // default version is inferred by uri. + String apiVersion = options.get(removePrefix.apply(NessieUtil.CLIENT_API_VERSION)); + if (apiVersion == null) { + apiVersion = inferVersionFromURI(options.get(CatalogProperties.URI)); + } + NessieApiV1 api; switch (apiVersion) { case "1": @@ -128,6 +133,26 @@ public void initialize(String name, Map options) { catalogOptions); } + private static String inferVersionFromURI(String uri) { + if (uri == null) { + throw new IllegalArgumentException("URI is not specified in the catalog properties"); + } + + // match for uri ending with /v1, /v2 etc + Pattern pattern = Pattern.compile("/v(\\d+)$"); + Matcher matcher = pattern.matcher(uri); + // Extract and print the version number + if (matcher.find()) { + return matcher.group(1); + } else { + throw new IllegalArgumentException( + String.format( + "URI doesn't end with the version: %s. " + + "Please configure `client-api-version` in the catalog properties explicitly.", + uri)); + } + } + /** * An alternative way to initialize the catalog using a pre-configured {@link NessieIcebergClient} * and {@link FileIO} instance. diff --git a/nessie/src/test/java/org/apache/iceberg/nessie/TestNessieCatalog.java b/nessie/src/test/java/org/apache/iceberg/nessie/TestNessieCatalog.java index dfa8f0a62ab4..fa381194ba0c 100644 --- a/nessie/src/test/java/org/apache/iceberg/nessie/TestNessieCatalog.java +++ b/nessie/src/test/java/org/apache/iceberg/nessie/TestNessieCatalog.java @@ -35,7 +35,6 @@ import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.io.TempDir; import org.projectnessie.client.api.NessieApiV1; -import org.projectnessie.client.ext.NessieApiVersion; import org.projectnessie.client.ext.NessieApiVersions; import org.projectnessie.client.ext.NessieClientFactory; import org.projectnessie.client.ext.NessieClientUri; @@ -65,7 +64,6 @@ public class TestNessieCatalog extends CatalogTests { private NessieCatalog catalog; private NessieApiV1 api; - private NessieApiVersion apiVersion; private Configuration hadoopConfig; private String initialHashOfDefaultBranch; private String uri; @@ -74,7 +72,6 @@ public class TestNessieCatalog extends CatalogTests { public void setUp(NessieClientFactory clientFactory, @NessieClientUri URI nessieUri) throws NessieNotFoundException { api = clientFactory.make(); - apiVersion = clientFactory.apiVersion(); initialHashOfDefaultBranch = api.getDefaultBranch().getHash(); uri = nessieUri.toASCIIString(); hadoopConfig = new Configuration(); @@ -122,9 +119,7 @@ private NessieCatalog initNessieCatalog(String ref) { CatalogProperties.URI, uri, CatalogProperties.WAREHOUSE_LOCATION, - temp.toUri().toString(), - "client-api-version", - apiVersion == NessieApiVersion.V2 ? "2" : "1"); + temp.toUri().toString()); newCatalog.initialize("nessie", options); return newCatalog; }