jackye1995 commented on a change in pull request #3424: URL: https://github.com/apache/iceberg/pull/3424#discussion_r751585082
########## 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(); Review comment: I think it is recommended to use object mapper as a singleton. We can reuse `JsonUtil.mapper()` and add util methods around that to register modules. And actually why not directly register the serdes in that mapper directly, instead of only registering it when initializing the REST catalog? I think it does not hurt to do that even for people not using REST catalog. -- 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]
