rdblue commented on a change in pull request #1875: URL: https://github.com/apache/iceberg/pull/1875#discussion_r547547165
########## File path: spark2/src/main/java/org/apache/iceberg/spark/source/CustomCatalogs.java ########## @@ -0,0 +1,108 @@ +/* + * 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.spark.source; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.apache.hadoop.conf.Configuration; +import org.apache.iceberg.CatalogUtil; +import org.apache.iceberg.Table; +import org.apache.iceberg.catalog.Catalog; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting; +import org.apache.iceberg.relocated.com.google.common.base.Splitter; +import org.apache.iceberg.spark.SparkUtil; +import org.apache.iceberg.util.Pair; +import org.apache.spark.SparkConf; +import org.apache.spark.sql.SparkSession; + +public final class CustomCatalogs { + private static final Cache<Pair<SparkSession, String>, Catalog> CATALOG_CACHE = Caffeine.newBuilder() + .softValues().build(); + + public static final String ICEBERG_DEFAULT_CATALOG = "default_catalog"; + public static final String ICEBERG_CATALOG_PREFIX = "spark.sql.catalog"; + + private CustomCatalogs() { + } + + /** + * Build an Iceberg {@link Catalog} to be used by this Spark source adapter. + * + * <p> + * The cache is to facilitate reuse of catalogs, especially if wrapped in CachingCatalog. For non-Hive catalogs all + * custom parameters passed to the catalog are considered in the cache key. Hive catalogs only cache based on + * the Metastore URIs as per previous behaviour. + * + * + * @param spark Spark Session + * @param name Catalog Name + * @return an Iceberg catalog + */ + public static Catalog buildIcebergCatalog(SparkSession spark, String name) { + return CATALOG_CACHE.get(Pair.of(spark, name), CustomCatalogs::load); + } + + private static Catalog load(Pair<SparkSession, String> sparkAndName) { + SparkSession spark = sparkAndName.first(); + String name = sparkAndName.second(); + SparkConf sparkConf = spark.sparkContext().getConf(); + Configuration conf = spark.sessionState().newHadoopConf(); + + String catalogPrefix = String.format("%s.%s", ICEBERG_CATALOG_PREFIX, name); + String catalogName = sparkConf.get(catalogPrefix, null); + if (!name.equals(ICEBERG_DEFAULT_CATALOG) && + !org.apache.spark.sql.catalog.Catalog.class.getName().equals(catalogName)) { Review comment: When I suggested using `SparkCatalog` earlier, I didn't think about how it isn't defined for 2.4. Instead of this check, let's just check whether the catalog property is defined at all. As long as `catalogName` is non-null, it is a catalog for the purposes of this config. If the catalog doesn't have a valid `type` or implementation class then loading it will fail. Also, it would be `catalogImpl` not name because `name` is the catalog name. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
