kbendick commented on a change in pull request #3424: URL: https://github.com/apache/iceberg/pull/3424#discussion_r751594368
########## File path: core/src/main/java/org/apache/iceberg/rest/RestCatalog.java ########## @@ -0,0 +1,324 @@ +/* + * 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.exceptions.ValidationException; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting; +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) { + 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. + this.restClient = HttpClient.builder() + .baseUrl(String.format("%s/warehouse/%s", baseUrl, catalogName)) + .mapper(mapper) + .defaultErrorHandler(ErrorHandlers.tableErrorHandler()) + .build(); + } + + // TODO - Pass in ObjectMapper too when testing. + @VisibleForTesting + public void initialize( + String name, + Map<String, String> props, + HttpClient httpClient) { + 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); + + // Pass in the mock client instead. + this.restClient = httpClient; + } + + @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) { + throw new UnsupportedOperationException("Not implemented: defaultWarehouseLocation"); + } + + @Override + public List<TableIdentifier> listTables(Namespace namespace) { + throw new UnsupportedOperationException("Not implemented: listTables"); + } + + @Override + public boolean tableExists(TableIdentifier identifier) { + throw new UnsupportedOperationException("Not implemented: tableExists"); + } + + @Override + public boolean dropTable(TableIdentifier identifier) { + return dropTable(identifier, false); + } + + @Override + public boolean dropTable(TableIdentifier identifier, boolean purge) { + throw new UnsupportedOperationException("Not implemented: dropTable"); + } + + @Override + public void renameTable(TableIdentifier from, TableIdentifier to) { + throw new UnsupportedOperationException("Not implemented: renameTable"); + } + + @Override + public void close() throws IOException { + restClient.close(); + } + + @Override + public void createNamespace(Namespace namespace, Map<String, String> metadata) { + CreateNamespaceRequest req = CreateNamespaceRequest.builder() + .withNamespace(namespace) + .withProperties(metadata) + .build(); + + // TODO - This should come from the server side. + String path = properties.getOrDefault("create-namespace-path", "namespace"); + restClient.post(path, req, CreateNamespaceResponse.class, ErrorHandlers.namespaceErrorHandler()); + } + + public CreateNamespaceResponse createDatabase(Namespace namespace, Map<String, String> props) { + CreateNamespaceRequest req = CreateNamespaceRequest.builder() + .withNamespace(namespace) + .withProperties(props) + .build(); + + // TODO - This should come from the server side. + String path = properties.getOrDefault("create-namespace-path", "namespace"); Review comment: This is left over from a previous idea I was toying with. I will remove this and others like it. It would be pretty hard to deal with it from the iceberg library if users were rewriting the path, given that it contains path variables and things at times. This is just an artifact I forgot to remove. Good catch. -- 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]
