|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.catalog.v2; |
| 19 | + |
| 20 | +import org.apache.spark.SparkException; |
| 21 | +import org.apache.spark.annotation.Private; |
| 22 | +import org.apache.spark.sql.internal.SQLConf; |
| 23 | +import org.apache.spark.sql.util.CaseInsensitiveStringMap; |
| 24 | +import org.apache.spark.util.Utils; |
| 25 | + |
| 26 | +import java.util.Map; |
| 27 | +import java.util.regex.Matcher; |
| 28 | +import java.util.regex.Pattern; |
| 29 | + |
| 30 | +import static scala.collection.JavaConverters.mapAsJavaMapConverter; |
| 31 | + |
| 32 | +@Private |
| 33 | +public class Catalogs { |
| 34 | + private Catalogs() { |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Load and configure a catalog by name. |
| 39 | + * <p> |
| 40 | + * This loads, instantiates, and initializes the catalog plugin for each call; it does not cache |
| 41 | + * or reuse instances. |
| 42 | + * |
| 43 | + * @param name a String catalog name |
| 44 | + * @param conf a SQLConf |
| 45 | + * @return an initialized CatalogPlugin |
| 46 | + * @throws SparkException If the plugin class cannot be found or instantiated |
| 47 | + */ |
| 48 | + public static CatalogPlugin load(String name, SQLConf conf) throws SparkException { |
| 49 | + String pluginClassName = conf.getConfString("spark.sql.catalog." + name, null); |
| 50 | + if (pluginClassName == null) { |
| 51 | + throw new SparkException(String.format( |
| 52 | + "Catalog '%s' plugin class not found: spark.sql.catalog.%s is not defined", name, name)); |
| 53 | + } |
| 54 | + |
| 55 | + ClassLoader loader = Utils.getContextOrSparkClassLoader(); |
| 56 | + |
| 57 | + try { |
| 58 | + Class<?> pluginClass = loader.loadClass(pluginClassName); |
| 59 | + |
| 60 | + if (!CatalogPlugin.class.isAssignableFrom(pluginClass)) { |
| 61 | + throw new SparkException(String.format( |
| 62 | + "Plugin class for catalog '%s' does not implement CatalogPlugin: %s", |
| 63 | + name, pluginClassName)); |
| 64 | + } |
| 65 | + |
| 66 | + CatalogPlugin plugin = CatalogPlugin.class.cast(pluginClass.newInstance()); |
| 67 | + |
| 68 | + plugin.initialize(name, catalogOptions(name, conf)); |
| 69 | + |
| 70 | + return plugin; |
| 71 | + |
| 72 | + } catch (ClassNotFoundException e) { |
| 73 | + throw new SparkException(String.format( |
| 74 | + "Cannot find catalog plugin class for catalog '%s': %s", name, pluginClassName)); |
| 75 | + |
| 76 | + } catch (IllegalAccessException e) { |
| 77 | + throw new SparkException(String.format( |
| 78 | + "Failed to call public no-arg constructor for catalog '%s': %s", name, pluginClassName), |
| 79 | + e); |
| 80 | + |
| 81 | + } catch (InstantiationException e) { |
| 82 | + throw new SparkException(String.format( |
| 83 | + "Failed while instantiating plugin for catalog '%s': %s", name, pluginClassName), |
| 84 | + e.getCause()); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Extracts a named catalog's configuration from a SQLConf. |
| 90 | + * |
| 91 | + * @param name a catalog name |
| 92 | + * @param conf a SQLConf |
| 93 | + * @return a case insensitive string map of options starting with spark.sql.catalog.(name). |
| 94 | + */ |
| 95 | + private static CaseInsensitiveStringMap catalogOptions(String name, SQLConf conf) { |
| 96 | + Map<String, String> allConfs = mapAsJavaMapConverter(conf.getAllConfs()).asJava(); |
| 97 | + Pattern prefix = Pattern.compile("^spark\\.sql\\.catalog\\." + name + "\\.(.+)"); |
| 98 | + |
| 99 | + CaseInsensitiveStringMap options = CaseInsensitiveStringMap.empty(); |
| 100 | + for (Map.Entry<String, String> entry : allConfs.entrySet()) { |
| 101 | + Matcher matcher = prefix.matcher(entry.getKey()); |
| 102 | + if (matcher.matches() && matcher.groupCount() > 0) { |
| 103 | + options.put(matcher.group(1), entry.getValue()); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return options; |
| 108 | + } |
| 109 | +} |
0 commit comments