You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2020/07/31 20:59:10 UTC

[GitHub] [iceberg] rdblue commented on a change in pull request #1243: Hive: Add support for custom catalog to Iceberg StorageHandler (#1155)

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



##########
File path: mr/src/main/java/org/apache/iceberg/mr/Catalogs.java
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.mr;
+
+import java.util.Optional;
+import java.util.Properties;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Catalog;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.common.DynConstructors;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.apache.iceberg.hadoop.HadoopCatalog;
+import org.apache.iceberg.hadoop.HadoopTables;
+import org.apache.iceberg.hive.HiveCatalogs;
+import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+
+public final class Catalogs {
+
+  private static final String HADOOP = "hadoop";
+  private static final String HIVE = "hive";
+
+  private static final String NAME = "name";
+  private static final String LOCATION = "location";
+
+  private Catalogs() {}
+
+  /**
+   * Load an Iceberg table using the catalog and table identifier (or table path) specified by the configuration.
+   * Catalog resolution happens in this order:
+   * 1. Custom catalog if specified by {@link InputFormatConfig#CATALOG_LOADER_CLASS}
+   * 2. Hadoop or Hive catalog if specified by {@link InputFormatConfig#CATALOG}
+   * 3. Hadoop Tables
+   * @param conf a Hadoop conf
+   * @return an Iceberg table
+   */
+  public static Table loadTable(Configuration conf) {
+    return loadTable(conf, conf.get(InputFormatConfig.TABLE_IDENTIFIER), conf.get(InputFormatConfig.TABLE_LOCATION));
+  }
+
+  // For use in HiveIcebergSerDe and HiveIcebergStorageHandler
+  public static Table loadTable(Configuration conf, Properties props) {
+    return loadTable(conf, props.getProperty(NAME), props.getProperty(LOCATION));
+  }
+
+  private static Table loadTable(Configuration conf, String tableIdentifier, String tableLocation) {
+    Optional<Catalog> catalog = loadCatalog(conf);
+
+    if (catalog.isPresent()) {
+      Preconditions.checkArgument(tableIdentifier != null, "Table identifier not set");
+      return catalog.get().loadTable(TableIdentifier.parse(tableIdentifier));
+    }
+
+    Preconditions.checkArgument(tableLocation != null, "Table location not set");
+    return new HadoopTables(conf).load(tableLocation);
+  }
+
+  @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();
+      return Optional.of(loader.load(conf));
+    }
+
+    String catalogName = conf.get(InputFormatConfig.CATALOG);
+
+    if (catalogName != null) {
+      switch (catalogName.toLowerCase()) {
+        case HADOOP:
+          return Optional.of(new HadoopCatalog(conf));

Review comment:
       There are two relevant configs in addition to the type: `uri` for Hive and `warehouse` for Hadoop. Hive defaults to the value in configuration of `hive.metastore.uris` from `hive-site.xml`, so it makes sense to not pass it explicitly here.
   
   But, the Hadoop catalog doesn't really have a standard warehouse path. It just has a default. Should we add a configuration option to set up the warehouse when using a `HadoopCatalog`?




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org