rdblue commented on a change in pull request #3424: URL: https://github.com/apache/iceberg/pull/3424#discussion_r739877441
########## File path: core/src/main/java/org/apache/iceberg/rest/RestCatalog.java ########## @@ -0,0 +1,274 @@ +/* + * 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.iceberg.rest; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.Closeable; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.hadoop.conf.Configurable; +import org.apache.hadoop.conf.Configuration; +import org.apache.iceberg.BaseMetastoreCatalog; +import org.apache.iceberg.CatalogProperties; +import org.apache.iceberg.CatalogUtil; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableOperations; +import org.apache.iceberg.Transaction; +import org.apache.iceberg.catalog.Catalog; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.SupportsNamespaces; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.exceptions.NamespaceNotEmptyException; +import org.apache.iceberg.exceptions.NoSuchNamespaceException; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.rest.http.ErrorHandlers; +import org.apache.iceberg.rest.http.HttpClient; +import org.apache.iceberg.rest.http.RequestResponseSerializers; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RestCatalog extends BaseMetastoreCatalog implements Closeable, SupportsNamespaces, Configurable { + + private static final Logger LOG = LoggerFactory.getLogger(RestCatalog.class); + + private String catalogName; + private Map<String, String> properties; + private ObjectMapper mapper; + private String baseUrl; + private Configuration hadoopConf; + private FileIO fileIO; + + // TODO - Refactor to use interface, which doesn't currently have POST etc embedded into it. + private HttpClient restClient; + + @Override + public void initialize(String name, Map<String, String> props) { + super.initialize(name, props); + this.catalogName = name; + this.properties = props; + + // TODO - Possibly authenticate with the server initially and then have the server return some of this information + Preconditions.checkNotNull( + properties.getOrDefault("baseUrl", null), + "Cannot initialize the RestCatalog as the baseUrl is a required parameter."); + + this.baseUrl = properties.get("baseUrl"); + this.fileIO = initializeFileIO(properties); + + this.mapper = new ObjectMapper(); + RequestResponseSerializers.registerAll(mapper); + + // TODO - We can possibly handle multiple warehouses via one RestCatalog to reuse the connection pool + // and for cross database calls if users need to authenticate with each. + restClient = HttpClient.builder() + .baseUrl(String.format("%s/warehouse/%s", baseUrl, catalogName)) + .mapper(mapper) + .defaultErrorHandler(ErrorHandlers.tableErrorHandler()) + .build(); + } + + @Override + public String name() { + return catalogName; + } + + @Override + public Table createTable( + TableIdentifier identifier, Schema schema, PartitionSpec spec, String location, + Map<String, String> props) { + throw new UnsupportedOperationException("Not implemented: createTable"); + } + + @Override + protected TableOperations newTableOps(TableIdentifier tableIdentifier) { + // Users might have not authenticated, possibly need to reauthenticate, or get authentication info per request. + // Though usually, auth tokens are persistent for at least some period of time. + return newTableOps(tableIdentifier, null); + } + + protected RestTableOperations newTableOps(TableIdentifier tableIdentifier, String authToken) { + throw new UnsupportedOperationException("Not implemented: newTableOps"); + } + + @Override + protected String defaultWarehouseLocation(TableIdentifier tableIdentifier) { Review comment: I'm not sure that we want to implement `BaseMetastoreCatalog`, since it has conventions that we don't need, like this method. This method is a way for the catalog to determine the default location for a table. But shouldn't this be done on the server side for a REST-based catalog? That makes the most sense to me because we want to keep the client as simple as possible and do all of the customization in the server that is easier to change. Deployed client Jars are hard to update! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
