rdsr commented on a change in pull request #1243: URL: https://github.com/apache/iceberg/pull/1243#discussion_r463782035
########## 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); Review comment: Can we do something like what Spark does ``` protected Table findTable(Map<String, String> options, Configuration conf) { Preconditions.checkArgument(options.containsKey("path"), "Cannot open table: path is not set"); String path = options.get("path"); if (path.contains("/")) { HadoopTables tables = new HadoopTables(conf); return tables.load(path); } else { HiveCatalog hiveCatalog = HiveCatalogs.loadCatalog(conf); TableIdentifier tableIdentifier = TableIdentifier.parse(path); return hiveCatalog.loadTable(tableIdentifier); } } ``` use TABLE_PATH to determine whether we need to use Hadoop or Hive catalog first and only then try custom catalog in the end. Seems like custom catalog would be used very rarely, if at all. ---------------------------------------------------------------- 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]
