rdblue commented on a change in pull request #2129:
URL: https://github.com/apache/iceberg/pull/2129#discussion_r566476061



##########
File path: mr/src/main/java/org/apache/iceberg/mr/Catalogs.java
##########
@@ -180,47 +191,82 @@ public static boolean dropTable(Configuration conf, 
Properties props) {
   /**
    * Returns true if HiveCatalog is used
    * @param conf a Hadoop conf
+   * @param props the controlling properties
    * @return true if the Catalog is HiveCatalog
    */
-  public static boolean hiveCatalog(Configuration conf) {
-    return HIVE.equalsIgnoreCase(conf.get(InputFormatConfig.CATALOG));
+  public static boolean hiveCatalog(Configuration conf, Properties props) {
+    String catalogName = props.getProperty(InputFormatConfig.TABLE_CATALOG);
+    return HIVE.equalsIgnoreCase(getCatalogType(conf, catalogName));
   }
 
   @VisibleForTesting
-  static Optional<Catalog> loadCatalog(Configuration conf) {
-    String catalogLoaderClass = 
conf.get(InputFormatConfig.CATALOG_LOADER_CLASS);
-
-    if (catalogLoaderClass != null) {
-      CatalogLoader loader = (CatalogLoader) 
DynConstructors.builder(CatalogLoader.class)
-              .impl(catalogLoaderClass)
-              .build()
-              .newInstance();
-      Catalog catalog = loader.load(conf);
-      LOG.info("Loaded catalog {} using {}", catalog, catalogLoaderClass);
-      return Optional.of(catalog);
-    }
+  static Optional<Catalog> loadCatalog(Configuration conf, String catalogName) 
{
+    String name = catalogName == null ? DEFAULT_CATALOG : catalogName;
+    String catalogType = getCatalogType(conf, name);
+    return loadCatalog(conf, name, catalogType);
+  }
 
-    String catalogName = conf.get(InputFormatConfig.CATALOG);
+  private static Optional<Catalog> loadCatalog(Configuration conf, String 
catalogName, String catalogType) {
+    if (catalogType == null) {
+      LOG.info("Catalog is not configured");
+      return Optional.empty();
+    }
 
-    if (catalogName != null) {
-      Catalog catalog;
-      switch (catalogName.toLowerCase()) {
-        case HADOOP:
+    Map<String, String> properties = getCatalogProperties(conf, catalogName);
+    Catalog catalog;
+    switch (catalogType.toLowerCase()) {
+      case HADOOP:
+        if (properties.containsKey(CatalogProperties.WAREHOUSE_LOCATION)) {
+          catalog = CatalogUtil.loadCatalog(HadoopCatalog.class.getName(), 
catalogName, properties, conf);
+        } else {
           String warehouseLocation = 
conf.get(InputFormatConfig.HADOOP_CATALOG_WAREHOUSE_LOCATION);
-
           catalog = (warehouseLocation != null) ? new HadoopCatalog(conf, 
warehouseLocation) : new HadoopCatalog(conf);
-          LOG.info("Loaded Hadoop catalog {}", catalog);
+        }
+        LOG.info("Loaded Hadoop catalog {}", catalog);
+        return Optional.of(catalog);
+      case HIVE:
+        catalog = HiveCatalogs.loadCatalog(catalogName, properties, conf);
+        LOG.info("Loaded Hive Metastore catalog {}", catalog);
+        return Optional.of(catalog);
+      case CUSTOM:
+        if (properties.containsKey(CatalogProperties.CATALOG_IMPL)) {
+          String catalogLoaderClass = 
properties.get(CatalogProperties.CATALOG_IMPL);
+          catalog = CatalogUtil.loadCatalog(catalogLoaderClass, catalogName, 
properties, conf);
+          LOG.info("Loaded catalog {} using {}", catalog, catalogLoaderClass);
           return Optional.of(catalog);
-        case HIVE:
-          catalog = HiveCatalogs.loadCatalog(conf);
-          LOG.info("Loaded Hive Metastore catalog {}", catalog);
+        } else if (conf.get(InputFormatConfig.CATALOG_LOADER_CLASS) != null) {
+          String catalogLoaderClass = 
conf.get(InputFormatConfig.CATALOG_LOADER_CLASS);
+          CatalogLoader loader = (CatalogLoader) 
DynConstructors.builder(CatalogLoader.class)
+                  .impl(catalogLoaderClass)
+                  .build()
+                  .newInstance();
+          catalog = loader.load(conf);
+          LOG.info("Loaded catalog {} using {}", catalog, catalogLoaderClass);
           return Optional.of(catalog);
-        default:
-          throw new NoSuchNamespaceException("Catalog %s is not supported.", 
catalogName);
-      }
+        } else {
+          return Optional.empty();
+        }
+      default:
+        throw new NoSuchNamespaceException("Catalog %s is not supported.", 
catalogType);
     }
+  }
 
-    LOG.info("Catalog is not configured");
-    return Optional.empty();
+  private static Map<String, String> getCatalogProperties(Configuration conf, 
String catalogName) {
+    String keyPrefix = InputFormatConfig.CATALOG_CONFIG_PREFIX + catalogName;
+    return Streams.stream(conf.iterator()).filter(e -> 
e.getKey().startsWith(keyPrefix))
+            .collect(Collectors.toMap(e -> 
e.getKey().substring(keyPrefix.length() + 1), Map.Entry::getValue));
+  }
+
+  private static String getCatalogType(Configuration conf, String catalogName) 
{
+    String name = catalogName == null ? DEFAULT_CATALOG : catalogName;

Review comment:
       Okay, after going back to the description, it looks like that is the 
intent.




----------------------------------------------------------------
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]

Reply via email to