rdblue commented on a change in pull request #1875: URL: https://github.com/apache/iceberg/pull/1875#discussion_r541278363
########## File path: spark2/src/main/java/org/apache/iceberg/spark/source/CustomCatalogs.java ########## @@ -0,0 +1,98 @@ +/* + * 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.Locale; +import java.util.Map; +import java.util.stream.Collectors; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.iceberg.CatalogProperties; +import org.apache.iceberg.CatalogUtil; +import org.apache.iceberg.catalog.Catalog; +import org.apache.iceberg.hadoop.HadoopCatalog; +import org.apache.iceberg.hive.HiveCatalogs; +import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting; +import org.apache.spark.SparkConf; +import org.apache.spark.sql.SparkSession; + +public final class CustomCatalogs { + private static final Cache<String, Catalog> CATALOG_CACHE = Caffeine.newBuilder().softValues().build(); + + public static final String ICEBERG_CATALOG_PREFIX = "spark.sql.catalog.iceberg."; + public static final String ICEBERG_CATALOG_TYPE = "type"; + public static final String ICEBERG_CATALOG_TYPE_HADOOP = "hadoop"; + public static final String ICEBERG_CATALOG_TYPE_HIVE = "hive"; + + private CustomCatalogs() { + } + + /** + * Build an Iceberg {@link Catalog} to be used by this Spark source adapter. + * + * 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 options options from Spark + * @return an Iceberg catalog + */ + public static Catalog buildIcebergCatalog(Map<String, String> options) { + String name = "spark_source"; + SparkConf sparkConf = SparkSession.active().sparkContext().getConf(); + Map<String, String> sparkMap = Arrays.stream(sparkConf.getAllWithPrefix(ICEBERG_CATALOG_PREFIX)) + .collect(Collectors.toMap(x -> x._1, x -> x._2)); + sparkMap.putAll(options); + Configuration conf = SparkSession.active().sessionState().newHadoopConf(); + + String catalogImpl = sparkMap.get(CatalogProperties.CATALOG_IMPL); + if (catalogImpl != null) { + String cacheKey = options.entrySet() + .stream().map(x -> String.format("%s:%s", x.getKey(), x.getValue())).collect(Collectors.joining(";")); + return CATALOG_CACHE.get(cacheKey, x -> CatalogUtil.loadCatalog(catalogImpl, name, sparkMap, conf)); Review comment: I would expect the cache to delegate to `buildIcebergCatalog`, no the other way around. What about using a simple getter: ```java public static Catalog loadCatalog(SparkSession spark, String name) { return CATALOG_CACHE.get(Pair.of(spark, name)); } ``` Then this just needs to build the named catalog for a particular Spark session. ---------------------------------------------------------------- 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]
