difin commented on code in PR #5628: URL: https://github.com/apache/hive/pull/5628#discussion_r2124404527
########## iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/HiveIcebergRESTCatalogClientAdapter.java: ########## @@ -0,0 +1,2426 @@ +/* + * 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.hive; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.stream.Collectors; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.common.TableName; +import org.apache.hadoop.hive.common.ValidCleanerWriteIdList; +import org.apache.hadoop.hive.common.ValidReadTxnList; +import org.apache.hadoop.hive.common.ValidTxnList; +import org.apache.hadoop.hive.common.ValidWriteIdList; +import org.apache.hadoop.hive.metastore.HiveMetaHook; +import org.apache.hadoop.hive.metastore.HiveMetaHookLoader; +import org.apache.hadoop.hive.metastore.IMetaStoreClient; +import org.apache.hadoop.hive.metastore.PartitionDropOptions; +import org.apache.hadoop.hive.metastore.TableType; +import org.apache.hadoop.hive.metastore.api.AbortCompactResponse; +import org.apache.hadoop.hive.metastore.api.AbortCompactionRequest; +import org.apache.hadoop.hive.metastore.api.AbortTxnRequest; +import org.apache.hadoop.hive.metastore.api.AbortTxnsRequest; +import org.apache.hadoop.hive.metastore.api.AddPackageRequest; +import org.apache.hadoop.hive.metastore.api.AggrStats; +import org.apache.hadoop.hive.metastore.api.AllTableConstraintsRequest; +import org.apache.hadoop.hive.metastore.api.AlreadyExistsException; +import org.apache.hadoop.hive.metastore.api.Catalog; +import org.apache.hadoop.hive.metastore.api.CheckConstraintsRequest; +import org.apache.hadoop.hive.metastore.api.CmRecycleRequest; +import org.apache.hadoop.hive.metastore.api.CmRecycleResponse; +import org.apache.hadoop.hive.metastore.api.ColumnStatistics; +import org.apache.hadoop.hive.metastore.api.ColumnStatisticsObj; +import org.apache.hadoop.hive.metastore.api.CommitTxnRequest; +import org.apache.hadoop.hive.metastore.api.CompactionInfoStruct; +import org.apache.hadoop.hive.metastore.api.CompactionMetricsDataRequest; +import org.apache.hadoop.hive.metastore.api.CompactionMetricsDataStruct; +import org.apache.hadoop.hive.metastore.api.CompactionRequest; +import org.apache.hadoop.hive.metastore.api.CompactionResponse; +import org.apache.hadoop.hive.metastore.api.CompactionType; +import org.apache.hadoop.hive.metastore.api.ConfigValSecurityException; +import org.apache.hadoop.hive.metastore.api.CreateTableRequest; +import org.apache.hadoop.hive.metastore.api.CreationMetadata; +import org.apache.hadoop.hive.metastore.api.CurrentNotificationEventId; +import org.apache.hadoop.hive.metastore.api.DataConnector; +import org.apache.hadoop.hive.metastore.api.DataOperationType; +import org.apache.hadoop.hive.metastore.api.Database; +import org.apache.hadoop.hive.metastore.api.DefaultConstraintsRequest; +import org.apache.hadoop.hive.metastore.api.DeleteColumnStatisticsRequest; +import org.apache.hadoop.hive.metastore.api.DropDatabaseRequest; +import org.apache.hadoop.hive.metastore.api.DropPackageRequest; +import org.apache.hadoop.hive.metastore.api.EnvironmentContext; +import org.apache.hadoop.hive.metastore.api.ExtendedTableInfo; +import org.apache.hadoop.hive.metastore.api.FieldSchema; +import org.apache.hadoop.hive.metastore.api.FindNextCompactRequest; +import org.apache.hadoop.hive.metastore.api.FindSchemasByColsResp; +import org.apache.hadoop.hive.metastore.api.FindSchemasByColsRqst; +import org.apache.hadoop.hive.metastore.api.FireEventRequest; +import org.apache.hadoop.hive.metastore.api.FireEventResponse; +import org.apache.hadoop.hive.metastore.api.ForeignKeysRequest; +import org.apache.hadoop.hive.metastore.api.Function; +import org.apache.hadoop.hive.metastore.api.GetAllFunctionsResponse; +import org.apache.hadoop.hive.metastore.api.GetAllWriteEventInfoRequest; +import org.apache.hadoop.hive.metastore.api.GetDatabaseObjectsRequest; +import org.apache.hadoop.hive.metastore.api.GetDatabaseObjectsResponse; +import org.apache.hadoop.hive.metastore.api.GetFieldsRequest; +import org.apache.hadoop.hive.metastore.api.GetFieldsResponse; +import org.apache.hadoop.hive.metastore.api.GetFunctionsRequest; +import org.apache.hadoop.hive.metastore.api.GetFunctionsResponse; +import org.apache.hadoop.hive.metastore.api.GetLatestCommittedCompactionInfoRequest; +import org.apache.hadoop.hive.metastore.api.GetLatestCommittedCompactionInfoResponse; +import org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse; +import org.apache.hadoop.hive.metastore.api.GetOpenTxnsResponse; +import org.apache.hadoop.hive.metastore.api.GetPackageRequest; +import org.apache.hadoop.hive.metastore.api.GetPartitionNamesPsRequest; +import org.apache.hadoop.hive.metastore.api.GetPartitionNamesPsResponse; +import org.apache.hadoop.hive.metastore.api.GetPartitionRequest; +import org.apache.hadoop.hive.metastore.api.GetPartitionResponse; +import org.apache.hadoop.hive.metastore.api.GetPartitionsByNamesRequest; +import org.apache.hadoop.hive.metastore.api.GetPartitionsByNamesResult; +import org.apache.hadoop.hive.metastore.api.GetPartitionsPsWithAuthRequest; +import org.apache.hadoop.hive.metastore.api.GetPartitionsPsWithAuthResponse; +import org.apache.hadoop.hive.metastore.api.GetPartitionsRequest; +import org.apache.hadoop.hive.metastore.api.GetPartitionsResponse; +import org.apache.hadoop.hive.metastore.api.GetPrincipalsInRoleRequest; +import org.apache.hadoop.hive.metastore.api.GetPrincipalsInRoleResponse; +import org.apache.hadoop.hive.metastore.api.GetProjectionsSpec; +import org.apache.hadoop.hive.metastore.api.GetReplicationMetricsRequest; +import org.apache.hadoop.hive.metastore.api.GetRoleGrantsForPrincipalRequest; +import org.apache.hadoop.hive.metastore.api.GetRoleGrantsForPrincipalResponse; +import org.apache.hadoop.hive.metastore.api.GetSchemaRequest; +import org.apache.hadoop.hive.metastore.api.GetSchemaResponse; +import org.apache.hadoop.hive.metastore.api.GetTableRequest; +import org.apache.hadoop.hive.metastore.api.HeartbeatTxnRangeResponse; +import org.apache.hadoop.hive.metastore.api.HiveObjectPrivilege; +import org.apache.hadoop.hive.metastore.api.HiveObjectRef; +import org.apache.hadoop.hive.metastore.api.ISchema; +import org.apache.hadoop.hive.metastore.api.InvalidInputException; +import org.apache.hadoop.hive.metastore.api.InvalidObjectException; +import org.apache.hadoop.hive.metastore.api.InvalidOperationException; +import org.apache.hadoop.hive.metastore.api.InvalidPartitionException; +import org.apache.hadoop.hive.metastore.api.ListPackageRequest; +import org.apache.hadoop.hive.metastore.api.ListStoredProcedureRequest; +import org.apache.hadoop.hive.metastore.api.LockRequest; +import org.apache.hadoop.hive.metastore.api.LockResponse; +import org.apache.hadoop.hive.metastore.api.Materialization; +import org.apache.hadoop.hive.metastore.api.MetaException; +import org.apache.hadoop.hive.metastore.api.MetadataPpdResult; +import org.apache.hadoop.hive.metastore.api.NoSuchLockException; +import org.apache.hadoop.hive.metastore.api.NoSuchObjectException; +import org.apache.hadoop.hive.metastore.api.NoSuchTxnException; +import org.apache.hadoop.hive.metastore.api.NotNullConstraintsRequest; +import org.apache.hadoop.hive.metastore.api.NotificationEventRequest; +import org.apache.hadoop.hive.metastore.api.NotificationEventResponse; +import org.apache.hadoop.hive.metastore.api.NotificationEventsCountRequest; +import org.apache.hadoop.hive.metastore.api.NotificationEventsCountResponse; +import org.apache.hadoop.hive.metastore.api.OpenTxnsResponse; +import org.apache.hadoop.hive.metastore.api.OptionalCompactionInfoStruct; +import org.apache.hadoop.hive.metastore.api.Package; +import org.apache.hadoop.hive.metastore.api.Partition; +import org.apache.hadoop.hive.metastore.api.PartitionEventType; +import org.apache.hadoop.hive.metastore.api.PartitionSpec; +import org.apache.hadoop.hive.metastore.api.PartitionValuesRequest; +import org.apache.hadoop.hive.metastore.api.PartitionValuesResponse; +import org.apache.hadoop.hive.metastore.api.PartitionsByExprRequest; +import org.apache.hadoop.hive.metastore.api.PartitionsRequest; +import org.apache.hadoop.hive.metastore.api.PartitionsResponse; +import org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest; +import org.apache.hadoop.hive.metastore.api.PrincipalPrivilegeSet; +import org.apache.hadoop.hive.metastore.api.PrincipalType; +import org.apache.hadoop.hive.metastore.api.PrivilegeBag; +import org.apache.hadoop.hive.metastore.api.ReplicationMetricList; +import org.apache.hadoop.hive.metastore.api.Role; +import org.apache.hadoop.hive.metastore.api.RuntimeStat; +import org.apache.hadoop.hive.metastore.api.SQLAllTableConstraints; +import org.apache.hadoop.hive.metastore.api.SQLCheckConstraint; +import org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint; +import org.apache.hadoop.hive.metastore.api.SQLForeignKey; +import org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint; +import org.apache.hadoop.hive.metastore.api.SQLPrimaryKey; +import org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint; +import org.apache.hadoop.hive.metastore.api.ScheduledQuery; +import org.apache.hadoop.hive.metastore.api.ScheduledQueryKey; +import org.apache.hadoop.hive.metastore.api.ScheduledQueryMaintenanceRequest; +import org.apache.hadoop.hive.metastore.api.ScheduledQueryPollRequest; +import org.apache.hadoop.hive.metastore.api.ScheduledQueryPollResponse; +import org.apache.hadoop.hive.metastore.api.ScheduledQueryProgressInfo; +import org.apache.hadoop.hive.metastore.api.SchemaVersion; +import org.apache.hadoop.hive.metastore.api.SchemaVersionState; +import org.apache.hadoop.hive.metastore.api.SerDeInfo; +import org.apache.hadoop.hive.metastore.api.SetPartitionsStatsRequest; +import org.apache.hadoop.hive.metastore.api.ShowCompactRequest; +import org.apache.hadoop.hive.metastore.api.ShowCompactResponse; +import org.apache.hadoop.hive.metastore.api.ShowLocksRequest; +import org.apache.hadoop.hive.metastore.api.ShowLocksResponse; +import org.apache.hadoop.hive.metastore.api.StorageDescriptor; +import org.apache.hadoop.hive.metastore.api.StoredProcedure; +import org.apache.hadoop.hive.metastore.api.StoredProcedureRequest; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.metastore.api.TableMeta; +import org.apache.hadoop.hive.metastore.api.TableValidWriteIds; +import org.apache.hadoop.hive.metastore.api.TxnAbortedException; +import org.apache.hadoop.hive.metastore.api.TxnOpenException; +import org.apache.hadoop.hive.metastore.api.TxnToWriteId; +import org.apache.hadoop.hive.metastore.api.TxnType; +import org.apache.hadoop.hive.metastore.api.UniqueConstraintsRequest; +import org.apache.hadoop.hive.metastore.api.UnknownDBException; +import org.apache.hadoop.hive.metastore.api.UnknownPartitionException; +import org.apache.hadoop.hive.metastore.api.UnknownTableException; +import org.apache.hadoop.hive.metastore.api.UpdateTransactionalStatsRequest; +import org.apache.hadoop.hive.metastore.api.WMFullResourcePlan; +import org.apache.hadoop.hive.metastore.api.WMMapping; +import org.apache.hadoop.hive.metastore.api.WMNullablePool; +import org.apache.hadoop.hive.metastore.api.WMNullableResourcePlan; +import org.apache.hadoop.hive.metastore.api.WMPool; +import org.apache.hadoop.hive.metastore.api.WMResourcePlan; +import org.apache.hadoop.hive.metastore.api.WMTrigger; +import org.apache.hadoop.hive.metastore.api.WMValidateResourcePlanResponse; +import org.apache.hadoop.hive.metastore.api.WriteEventInfo; +import org.apache.hadoop.hive.metastore.api.WriteNotificationLogBatchRequest; +import org.apache.hadoop.hive.metastore.api.WriteNotificationLogRequest; +import org.apache.hadoop.hive.metastore.partition.spec.PartitionSpecProxy; +import org.apache.hadoop.hive.serde.serdeConstants; +import org.apache.iceberg.BaseTable; +import org.apache.iceberg.CatalogUtil; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.SessionCatalog; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.exceptions.NoSuchTableException; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.rest.RESTCatalog; +import org.apache.thrift.TException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HiveIcebergRESTCatalogClientAdapter implements IMetaStoreClient { + + private static final Logger LOG = LoggerFactory.getLogger(HiveIcebergRESTCatalogClientAdapter.class); + public static final String NAMESPACE_SEPARATOR = "."; + public static final String NAME = "name"; + public static final String LOCATION = "location"; + public static final String CATALOG_NAME = "iceberg.catalog"; + public static final String DB_OWNER = "owner"; + public static final String DB_OWNER_TYPE = "ownerType"; + public static final String SERDE_INFO = "serdeInfo"; + public static final String DEFAULT_INPUT_FORMAT_CLASS = "org.apache.iceberg.mr.hive.HiveIcebergInputFormat"; + public static final String DEFAULT_OUTPUT_FORMAT_CLASS + = "org.apache.iceberg.mr.hive.HiveIcebergOutputFormat"; + public static final String DEFAULT_SERDE_CLASS = "org.apache.iceberg.mr.hive.HiveIcebergSerDe"; + public static final String CATALOG_CONFIG_PREFIX = "iceberg.catalog."; + private final Configuration conf; + private RESTCatalog restCatalog; + private final HiveMetaHookLoader hookLoader; + + private final long maxHiveTablePropertySize; + + public HiveIcebergRESTCatalogClientAdapter(Configuration conf, HiveMetaHookLoader hookLoader) { + this.conf = conf; + this.hookLoader = hookLoader; + this.maxHiveTablePropertySize = conf.getLong(HiveOperationsBase.HIVE_TABLE_PROPERTY_MAX_SIZE, + HiveOperationsBase.HIVE_TABLE_PROPERTY_MAX_SIZE_DEFAULT); + } + + @Override + public boolean isCompatibleWith(Configuration configuration) { + return false; + } + + @Override + public void setHiveAddedJars(String addedJars) { + + } + + @Override + public boolean isLocalMetaStore() { + return false; + } + + @Override + public void reconnect() throws MetaException { + SessionCatalog.SessionContext context = SessionCatalog.SessionContext.createEmpty(); + String catalogName = conf.get(CATALOG_NAME); + Map<String, String> properties = getCatalogProperties(conf, catalogName); + restCatalog = (RESTCatalog) CatalogUtil.buildIcebergCatalog(catalogName, properties, conf); + restCatalog.initialize(catalogName, properties); + } + private static Map<String, String> getCatalogProperties( + Configuration conf, String catalogName) { + Map<String, String> catalogProperties = Maps.newHashMap(); + String keyPrefix = CATALOG_CONFIG_PREFIX + catalogName; + conf.forEach(config -> { + if (config.getKey().startsWith(keyPrefix)) { + catalogProperties.put( + config.getKey().substring(keyPrefix.length() + 1), + config.getValue()); + } + }); + return catalogProperties; + } + @Override + public void close() { + + } + + @Override + public void setMetaConf(String key, String value) throws MetaException, TException { + + } + + @Override + public String getMetaConf(String key) throws MetaException, TException { + return ""; + } + + @Override + public void createCatalog(Catalog catalog) + throws AlreadyExistsException, InvalidObjectException, MetaException, TException { + } + + @Override + public void alterCatalog(String catalogName, Catalog newCatalog) + throws NoSuchObjectException, InvalidObjectException, MetaException, TException { + + } + + @Override + public Catalog getCatalog(String catName) throws NoSuchObjectException, MetaException, TException { + return null; + } + + @Override + public List<String> getCatalogs() throws MetaException, TException { + return new LinkedList<>(); + } + + @Override + public void dropCatalog(String catName) + throws NoSuchObjectException, InvalidOperationException, MetaException, TException { + + } + + @Override + public void dropCatalog(String catName, boolean ifExists) throws TException { + + } + + @Override + public List<String> getDatabases(String databasePattern) throws MetaException, TException { + return getAllDatabases(); + } + + @Override + public List<String> getDatabases(String catName, String databasePattern) throws MetaException, TException { + return getAllDatabases(); + } + + @Override + public List<String> getAllDatabases() throws MetaException, TException { + return restCatalog.listNamespaces(Namespace.empty()).stream().map(Namespace::toString).collect(Collectors.toList()); + } + + @Override + public List<String> getAllDatabases(String catName) throws MetaException, TException { + return getAllDatabases(); + } + + @Override + public List<String> getTables(String dbName, String tablePattern) + throws MetaException, TException, UnknownDBException { + return getTables(null, dbName, tablePattern, null); + } + + @Override + public List<String> getTables(String catName, String dbName, String tablePattern) + throws MetaException, TException, UnknownDBException { + return getTables(catName, dbName, tablePattern, null); + } + + @Override + public List<String> getTables(String dbName, String tablePattern, TableType tableType) + throws MetaException, TException, UnknownDBException { + return getTables(null, dbName, tablePattern, tableType); + } + + @Override + public List<String> getTables(String catName, String dbName, String tablePattern, TableType tableType) + throws MetaException, TException, UnknownDBException { + List<TableIdentifier> tableIdentifiers = restCatalog.listTables(Namespace.of(dbName)); + return tableIdentifiers.stream().map(tableIdentifier -> tableIdentifier.name()).collect(Collectors.toList()); + } + + @Override + public List<Table> getAllMaterializedViewObjectsForRewriting() throws MetaException, TException, UnknownDBException { + return new LinkedList<>(); + } + + @Override + public List<ExtendedTableInfo> getTablesExt(String catName, String dbName, String tablePattern, int requestedFields, + int limit) throws MetaException, TException { + return new LinkedList<>(); + } + + @Override + public List<String> getMaterializedViewsForRewriting(String dbName) + throws MetaException, TException, UnknownDBException { + return new LinkedList<>(); + } + + @Override + public List<String> getMaterializedViewsForRewriting(String catName, String dbName) + throws MetaException, TException, UnknownDBException { + return new LinkedList<>(); + } + + @Override + public List<TableMeta> getTableMeta(String dbPatterns, String tablePatterns, List<String> tableTypes) + throws MetaException, TException, UnknownDBException { + return new LinkedList<>(); + } + + @Override + public List<TableMeta> getTableMeta(String catName, String dbPatterns, String tablePatterns, List<String> tableTypes) + throws MetaException, TException, UnknownDBException { + return new LinkedList<>(); + } + + @Override + public List<String> getAllTables(String dbName) throws MetaException, TException, UnknownDBException { + return getTables(null, dbName, "", null); + } + + @Override + public List<String> getAllTables(String catName, String dbName) throws MetaException, TException, UnknownDBException { + return getTables(catName, dbName, "", null); + } + + @Override + public List<String> listTableNamesByFilter(String dbName, String filter, short maxTables) + throws TException, InvalidOperationException, UnknownDBException { + return new LinkedList<>(); + } + + @Override + public List<String> listTableNamesByFilter(String catName, String dbName, String filter, int maxTables) + throws TException, InvalidOperationException, UnknownDBException { + return new LinkedList<>(); + } + + @Override + public void dropTable(String dbname, String tableName, boolean deleteData, boolean ignoreUnknownTab) + throws MetaException, TException, NoSuchObjectException { + dropTable(dbname, tableName); + } + + @Override + public void dropTable(String dbname, String tableName, boolean deleteData, boolean ignoreUnknownTab, boolean ifPurge) + throws MetaException, TException, NoSuchObjectException { + dropTable(dbname, tableName); + } + + @Override + public void dropTable(Table table, boolean deleteData, boolean ignoreUnknownTab, boolean ifPurge) throws TException { + dropTable(table.getDbName(), table.getTableName()); + } + + @Override + public void dropTable(String dbname, String tableName) throws MetaException, TException, NoSuchObjectException { + restCatalog.dropTable(TableIdentifier.of(dbname, tableName)); + } + + @Override + public void dropTable(String catName, String dbName, String tableName, boolean deleteData, boolean ignoreUnknownTable, + boolean ifPurge) throws MetaException, NoSuchObjectException, TException { + dropTable(dbName, tableName); + } + + @Override + public void truncateTable(String dbName, String tableName, List<String> partNames) throws MetaException, TException { + + } + + @Override + public void truncateTable(TableName table, List<String> partNames) throws TException { + + } + + @Override + public void truncateTable(String dbName, String tableName, List<String> partNames, String validWriteIds, long writeId) + throws TException { + + } + + @Override + public void truncateTable(String dbName, String tableName, List<String> partNames, String validWriteIds, long writeId, + boolean deleteData) throws TException { + + } + + @Override + public void truncateTable(String catName, String dbName, String tableName, List<String> partNames) + throws MetaException, TException { + + } + + @Override + public CmRecycleResponse recycleDirToCmPath(CmRecycleRequest request) throws MetaException, TException { + return new CmRecycleResponse(); + } + + @Override + public boolean tableExists(String databaseName, String tableName) + throws MetaException, TException, UnknownDBException { + try { + getTables(databaseName, tableName); + } catch (NoSuchTableException e) { + return false; + } + return true; + } + + @Override + public boolean tableExists(String catName, String dbName, String tableName) + throws MetaException, TException, UnknownDBException { + return tableExists(dbName, tableName); + } + + @Override + public Database getDatabase(String databaseName) throws NoSuchObjectException, MetaException, TException { + return restCatalog.listNamespaces(Namespace.empty()).stream() + .filter(namespace -> namespace.levels()[0].equals(databaseName)).map(namespace -> { + Database database = new Database(); + database.setName(String.join(NAMESPACE_SEPARATOR, namespace.levels())); + Map<String, String> namespaceMetadata = restCatalog.loadNamespaceMetadata(Namespace.of(databaseName)); + database.setLocationUri(namespaceMetadata.get(LOCATION)); + database.setCatalogName("REST"); + database.setOwnerName(namespaceMetadata.get(DB_OWNER)); + try { + database.setOwnerType(PrincipalType.valueOf(namespaceMetadata.get(DB_OWNER_TYPE))); + } catch (Exception e) { + LOG.warn("Can not set ownerType: {}", namespaceMetadata.get(DB_OWNER_TYPE), e); + } + return database; + }).findFirst().get(); + } + + @Override + public Database getDatabase(String catalogName, String databaseName) + throws NoSuchObjectException, MetaException, TException { + return getDatabase(databaseName); + } + + @Override + public Table getTable(String dbName, String tableName) throws MetaException, TException, NoSuchObjectException { + org.apache.iceberg.Table icebergTable = null; + try { + icebergTable = restCatalog.loadTable(TableIdentifier.of(dbName, tableName)); + } catch (NoSuchTableException exception) { + throw new NoSuchObjectException(); + } + Table hiveTable = convertIcebergTableToHiveTable(icebergTable); + return hiveTable; + } + private Table convertIcebergTableToHiveTable(org.apache.iceberg.Table icebergTable) { + Table hiveTable = new Table(); + TableMetadata metadata = ((BaseTable) icebergTable).operations().current(); + HMSTablePropertyHelper.updateHmsTableForIcebergTable(metadata.metadataFileLocation(), hiveTable, + metadata, null, true, maxHiveTablePropertySize, null); + hiveTable.getParameters().put(CATALOG_NAME, CatalogUtil.ICEBERG_CATALOG_TYPE_REST); + hiveTable.setTableName(getTableName(icebergTable)); + hiveTable.setDbName(getDbName(icebergTable)); + StorageDescriptor storageDescriptor = new StorageDescriptor(); + hiveTable.setSd(storageDescriptor); + hiveTable.setTableType("EXTERNAL_TABLE"); + hiveTable.setPartitionKeys(new LinkedList<>()); + List<FieldSchema> cols = new LinkedList<>(); + storageDescriptor.setCols(cols); + storageDescriptor.setLocation(icebergTable.location()); + storageDescriptor.setInputFormat(DEFAULT_INPUT_FORMAT_CLASS); + storageDescriptor.setOutputFormat(DEFAULT_OUTPUT_FORMAT_CLASS); + storageDescriptor.setBucketCols(new LinkedList<>()); + storageDescriptor.setSortCols(new LinkedList<>()); + storageDescriptor.setParameters(Maps.newHashMap()); + if (icebergTable.properties().containsKey(SERDE_INFO)) { + // TODO + } else { + SerDeInfo serDeInfo = new SerDeInfo("icebergSerde", DEFAULT_SERDE_CLASS, Maps.newHashMap()); + serDeInfo.getParameters().put(serdeConstants.SERIALIZATION_FORMAT, "1"); // Default serialization format. + storageDescriptor.setSerdeInfo(serDeInfo); + } + icebergTable.schema().columns().forEach(icebergColumn -> { + FieldSchema fieldSchema = new FieldSchema(); + fieldSchema.setName(icebergColumn.name()); + fieldSchema.setType(icebergColumn.type().toString()); + cols.add(fieldSchema); + }); + return hiveTable; + } + private String getTableName(org.apache.iceberg.Table icebergTable) { + String[] nameParts = icebergTable.name().split("\\."); + if (nameParts.length == 3) { + return nameParts[2]; + } + if (nameParts.length == 2) { + return nameParts[1]; + } + return icebergTable.name(); + } + + private String getDbName(org.apache.iceberg.Table icebergTable) { + String[] nameParts = icebergTable.name().split("\\."); + return nameParts.length == 3 ? nameParts[1] : nameParts[0]; + } + + @Override + public Table getTable(String dbName, String tableName, boolean getColumnStats, String engine) + throws MetaException, TException, NoSuchObjectException { + return getTable(dbName, tableName); + } + + @Override + public Table getTable(String catName, String dbName, String tableName) throws MetaException, TException { + return getTable(dbName, tableName); + } + + @Override + public Table getTable(String catName, String dbName, String tableName, String validWriteIdList) throws TException { + return getTable(dbName, tableName); + } + + @Override + public Table getTable(String catName, String dbName, String tableName, String validWriteIdList, + boolean getColumnStats, String engine) throws TException { + return getTable(dbName, tableName); + } + + @Override + public Table getTable(GetTableRequest getTableRequest) throws MetaException, TException, NoSuchObjectException { + return getTable(getTableRequest.getDbName(), getTableRequest.getTblName()); + } + + @Override + public List<Table> getTableObjectsByName(String dbName, List<String> tableNames) + throws MetaException, InvalidOperationException, UnknownDBException, TException { + return null; + } + + @Override + public List<Table> getTables(String catName, String dbName, List<String> tableNames, + GetProjectionsSpec projectionsSpec) + throws MetaException, InvalidOperationException, UnknownDBException, TException { + return new LinkedList<>(); + } + + @Override + public List<Table> getTableObjectsByName(String catName, String dbName, List<String> tableNames) + throws MetaException, InvalidOperationException, UnknownDBException, TException { + return new LinkedList<>(); + } + + @Override + public Materialization getMaterializationInvalidationInfo(CreationMetadata cm, String validTxnList) + throws MetaException, InvalidOperationException, UnknownDBException, TException { + return new Materialization(); + } + + @Override + public void updateCreationMetadata(String dbName, String tableName, CreationMetadata cm) + throws MetaException, TException { + } + + @Override + public void updateCreationMetadata(String catName, String dbName, String tableName, CreationMetadata cm) + throws MetaException, TException { + + } + + @Override + public Partition appendPartition(String dbName, String tableName, List<String> partVals) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return new Partition(); + } + + @Override + public Partition appendPartition(String catName, String dbName, String tableName, List<String> partVals) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return new Partition(); + } + + @Override + public Partition appendPartition(String dbName, String tableName, String name) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return new Partition(); + } + + @Override + public Partition appendPartition(String catName, String dbName, String tableName, String name) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return new Partition(); + } + + @Override + public Partition add_partition(Partition partition) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return new Partition(); + } + + @Override + public int add_partitions(List<Partition> partitions) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return 0; + } + + @Override + public int add_partitions_pspec(PartitionSpecProxy partitionSpec) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return 0; + } + + @Override + public List<Partition> add_partitions(List<Partition> partitions, boolean ifNotExists, boolean needResults) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return null; + } + + @Override + public Partition getPartition(String dbName, String tblName, List<String> partVals) + throws NoSuchObjectException, MetaException, TException { + return new Partition(); + } + + @Override + public GetPartitionResponse getPartitionRequest(GetPartitionRequest req) + throws NoSuchObjectException, MetaException, TException { + return new GetPartitionResponse(); + } + + @Override + public Partition getPartition(String catName, String dbName, String tblName, List<String> partVals) + throws NoSuchObjectException, MetaException, TException { + return new Partition(); + } + + @Override + public Partition exchange_partition(Map<String, String> partitionSpecs, String sourceDb, String sourceTable, + String destdb, String destTableName) + throws MetaException, NoSuchObjectException, InvalidObjectException, TException { + return new Partition(); + } + + @Override + public Partition exchange_partition(Map<String, String> partitionSpecs, String sourceCat, String sourceDb, + String sourceTable, String destCat, String destdb, String destTableName) + throws MetaException, NoSuchObjectException, InvalidObjectException, TException { + return new Partition(); + } + + @Override + public List<Partition> exchange_partitions(Map<String, String> partitionSpecs, String sourceDb, String sourceTable, + String destdb, String destTableName) + throws MetaException, NoSuchObjectException, InvalidObjectException, TException { + return new LinkedList<>(); + } + + @Override + public List<Partition> exchange_partitions(Map<String, String> partitionSpecs, String sourceCat, String sourceDb, + String sourceTable, String destCat, String destdb, String destTableName) + throws MetaException, NoSuchObjectException, InvalidObjectException, TException { + return new LinkedList<>(); + } + + @Override + public Partition getPartition(String dbName, String tblName, String name) + throws MetaException, UnknownTableException, NoSuchObjectException, TException { + return new Partition(); + } + + @Override + public Partition getPartition(String catName, String dbName, String tblName, String name) + throws MetaException, UnknownTableException, NoSuchObjectException, TException { + return new Partition(); + } + + @Override + public Partition getPartitionWithAuthInfo(String dbName, String tableName, List<String> pvals, String userName, + List<String> groupNames) throws MetaException, UnknownTableException, NoSuchObjectException, TException { + return new Partition(); + } + + @Override + public Partition getPartitionWithAuthInfo(String catName, String dbName, String tableName, List<String> pvals, + String userName, List<String> groupNames) + throws MetaException, UnknownTableException, NoSuchObjectException, TException { + return null; + } + + @Override + public List<Partition> listPartitions(String db_name, String tbl_name, short max_parts) + throws NoSuchObjectException, MetaException, TException { + return new LinkedList<>(); + } + + @Override + public List<Partition> listPartitions(String catName, String db_name, String tbl_name, int max_parts) + throws NoSuchObjectException, MetaException, TException { + return new LinkedList<>(); + } + + @Override + public PartitionSpecProxy listPartitionSpecs(String dbName, String tableName, int maxParts) throws TException { + return null; + } + + @Override + public PartitionSpecProxy listPartitionSpecs(String catName, String dbName, String tableName, int maxParts) + throws TException { + return null; + } + + @Override + public List<Partition> listPartitions(String db_name, String tbl_name, List<String> part_vals, short max_parts) + throws NoSuchObjectException, MetaException, TException { + return new LinkedList<>(); + } + + @Override + public List<Partition> listPartitions(String catName, String db_name, String tbl_name, List<String> part_vals, + int max_parts) throws NoSuchObjectException, MetaException, TException { Review Comment: String db_name, String tbl_name ########## iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/HiveIcebergRESTCatalogClientAdapter.java: ########## @@ -0,0 +1,2426 @@ +/* + * 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.hive; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.stream.Collectors; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.common.TableName; +import org.apache.hadoop.hive.common.ValidCleanerWriteIdList; +import org.apache.hadoop.hive.common.ValidReadTxnList; +import org.apache.hadoop.hive.common.ValidTxnList; +import org.apache.hadoop.hive.common.ValidWriteIdList; +import org.apache.hadoop.hive.metastore.HiveMetaHook; +import org.apache.hadoop.hive.metastore.HiveMetaHookLoader; +import org.apache.hadoop.hive.metastore.IMetaStoreClient; +import org.apache.hadoop.hive.metastore.PartitionDropOptions; +import org.apache.hadoop.hive.metastore.TableType; +import org.apache.hadoop.hive.metastore.api.AbortCompactResponse; +import org.apache.hadoop.hive.metastore.api.AbortCompactionRequest; +import org.apache.hadoop.hive.metastore.api.AbortTxnRequest; +import org.apache.hadoop.hive.metastore.api.AbortTxnsRequest; +import org.apache.hadoop.hive.metastore.api.AddPackageRequest; +import org.apache.hadoop.hive.metastore.api.AggrStats; +import org.apache.hadoop.hive.metastore.api.AllTableConstraintsRequest; +import org.apache.hadoop.hive.metastore.api.AlreadyExistsException; +import org.apache.hadoop.hive.metastore.api.Catalog; +import org.apache.hadoop.hive.metastore.api.CheckConstraintsRequest; +import org.apache.hadoop.hive.metastore.api.CmRecycleRequest; +import org.apache.hadoop.hive.metastore.api.CmRecycleResponse; +import org.apache.hadoop.hive.metastore.api.ColumnStatistics; +import org.apache.hadoop.hive.metastore.api.ColumnStatisticsObj; +import org.apache.hadoop.hive.metastore.api.CommitTxnRequest; +import org.apache.hadoop.hive.metastore.api.CompactionInfoStruct; +import org.apache.hadoop.hive.metastore.api.CompactionMetricsDataRequest; +import org.apache.hadoop.hive.metastore.api.CompactionMetricsDataStruct; +import org.apache.hadoop.hive.metastore.api.CompactionRequest; +import org.apache.hadoop.hive.metastore.api.CompactionResponse; +import org.apache.hadoop.hive.metastore.api.CompactionType; +import org.apache.hadoop.hive.metastore.api.ConfigValSecurityException; +import org.apache.hadoop.hive.metastore.api.CreateTableRequest; +import org.apache.hadoop.hive.metastore.api.CreationMetadata; +import org.apache.hadoop.hive.metastore.api.CurrentNotificationEventId; +import org.apache.hadoop.hive.metastore.api.DataConnector; +import org.apache.hadoop.hive.metastore.api.DataOperationType; +import org.apache.hadoop.hive.metastore.api.Database; +import org.apache.hadoop.hive.metastore.api.DefaultConstraintsRequest; +import org.apache.hadoop.hive.metastore.api.DeleteColumnStatisticsRequest; +import org.apache.hadoop.hive.metastore.api.DropDatabaseRequest; +import org.apache.hadoop.hive.metastore.api.DropPackageRequest; +import org.apache.hadoop.hive.metastore.api.EnvironmentContext; +import org.apache.hadoop.hive.metastore.api.ExtendedTableInfo; +import org.apache.hadoop.hive.metastore.api.FieldSchema; +import org.apache.hadoop.hive.metastore.api.FindNextCompactRequest; +import org.apache.hadoop.hive.metastore.api.FindSchemasByColsResp; +import org.apache.hadoop.hive.metastore.api.FindSchemasByColsRqst; +import org.apache.hadoop.hive.metastore.api.FireEventRequest; +import org.apache.hadoop.hive.metastore.api.FireEventResponse; +import org.apache.hadoop.hive.metastore.api.ForeignKeysRequest; +import org.apache.hadoop.hive.metastore.api.Function; +import org.apache.hadoop.hive.metastore.api.GetAllFunctionsResponse; +import org.apache.hadoop.hive.metastore.api.GetAllWriteEventInfoRequest; +import org.apache.hadoop.hive.metastore.api.GetDatabaseObjectsRequest; +import org.apache.hadoop.hive.metastore.api.GetDatabaseObjectsResponse; +import org.apache.hadoop.hive.metastore.api.GetFieldsRequest; +import org.apache.hadoop.hive.metastore.api.GetFieldsResponse; +import org.apache.hadoop.hive.metastore.api.GetFunctionsRequest; +import org.apache.hadoop.hive.metastore.api.GetFunctionsResponse; +import org.apache.hadoop.hive.metastore.api.GetLatestCommittedCompactionInfoRequest; +import org.apache.hadoop.hive.metastore.api.GetLatestCommittedCompactionInfoResponse; +import org.apache.hadoop.hive.metastore.api.GetOpenTxnsInfoResponse; +import org.apache.hadoop.hive.metastore.api.GetOpenTxnsResponse; +import org.apache.hadoop.hive.metastore.api.GetPackageRequest; +import org.apache.hadoop.hive.metastore.api.GetPartitionNamesPsRequest; +import org.apache.hadoop.hive.metastore.api.GetPartitionNamesPsResponse; +import org.apache.hadoop.hive.metastore.api.GetPartitionRequest; +import org.apache.hadoop.hive.metastore.api.GetPartitionResponse; +import org.apache.hadoop.hive.metastore.api.GetPartitionsByNamesRequest; +import org.apache.hadoop.hive.metastore.api.GetPartitionsByNamesResult; +import org.apache.hadoop.hive.metastore.api.GetPartitionsPsWithAuthRequest; +import org.apache.hadoop.hive.metastore.api.GetPartitionsPsWithAuthResponse; +import org.apache.hadoop.hive.metastore.api.GetPartitionsRequest; +import org.apache.hadoop.hive.metastore.api.GetPartitionsResponse; +import org.apache.hadoop.hive.metastore.api.GetPrincipalsInRoleRequest; +import org.apache.hadoop.hive.metastore.api.GetPrincipalsInRoleResponse; +import org.apache.hadoop.hive.metastore.api.GetProjectionsSpec; +import org.apache.hadoop.hive.metastore.api.GetReplicationMetricsRequest; +import org.apache.hadoop.hive.metastore.api.GetRoleGrantsForPrincipalRequest; +import org.apache.hadoop.hive.metastore.api.GetRoleGrantsForPrincipalResponse; +import org.apache.hadoop.hive.metastore.api.GetSchemaRequest; +import org.apache.hadoop.hive.metastore.api.GetSchemaResponse; +import org.apache.hadoop.hive.metastore.api.GetTableRequest; +import org.apache.hadoop.hive.metastore.api.HeartbeatTxnRangeResponse; +import org.apache.hadoop.hive.metastore.api.HiveObjectPrivilege; +import org.apache.hadoop.hive.metastore.api.HiveObjectRef; +import org.apache.hadoop.hive.metastore.api.ISchema; +import org.apache.hadoop.hive.metastore.api.InvalidInputException; +import org.apache.hadoop.hive.metastore.api.InvalidObjectException; +import org.apache.hadoop.hive.metastore.api.InvalidOperationException; +import org.apache.hadoop.hive.metastore.api.InvalidPartitionException; +import org.apache.hadoop.hive.metastore.api.ListPackageRequest; +import org.apache.hadoop.hive.metastore.api.ListStoredProcedureRequest; +import org.apache.hadoop.hive.metastore.api.LockRequest; +import org.apache.hadoop.hive.metastore.api.LockResponse; +import org.apache.hadoop.hive.metastore.api.Materialization; +import org.apache.hadoop.hive.metastore.api.MetaException; +import org.apache.hadoop.hive.metastore.api.MetadataPpdResult; +import org.apache.hadoop.hive.metastore.api.NoSuchLockException; +import org.apache.hadoop.hive.metastore.api.NoSuchObjectException; +import org.apache.hadoop.hive.metastore.api.NoSuchTxnException; +import org.apache.hadoop.hive.metastore.api.NotNullConstraintsRequest; +import org.apache.hadoop.hive.metastore.api.NotificationEventRequest; +import org.apache.hadoop.hive.metastore.api.NotificationEventResponse; +import org.apache.hadoop.hive.metastore.api.NotificationEventsCountRequest; +import org.apache.hadoop.hive.metastore.api.NotificationEventsCountResponse; +import org.apache.hadoop.hive.metastore.api.OpenTxnsResponse; +import org.apache.hadoop.hive.metastore.api.OptionalCompactionInfoStruct; +import org.apache.hadoop.hive.metastore.api.Package; +import org.apache.hadoop.hive.metastore.api.Partition; +import org.apache.hadoop.hive.metastore.api.PartitionEventType; +import org.apache.hadoop.hive.metastore.api.PartitionSpec; +import org.apache.hadoop.hive.metastore.api.PartitionValuesRequest; +import org.apache.hadoop.hive.metastore.api.PartitionValuesResponse; +import org.apache.hadoop.hive.metastore.api.PartitionsByExprRequest; +import org.apache.hadoop.hive.metastore.api.PartitionsRequest; +import org.apache.hadoop.hive.metastore.api.PartitionsResponse; +import org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest; +import org.apache.hadoop.hive.metastore.api.PrincipalPrivilegeSet; +import org.apache.hadoop.hive.metastore.api.PrincipalType; +import org.apache.hadoop.hive.metastore.api.PrivilegeBag; +import org.apache.hadoop.hive.metastore.api.ReplicationMetricList; +import org.apache.hadoop.hive.metastore.api.Role; +import org.apache.hadoop.hive.metastore.api.RuntimeStat; +import org.apache.hadoop.hive.metastore.api.SQLAllTableConstraints; +import org.apache.hadoop.hive.metastore.api.SQLCheckConstraint; +import org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint; +import org.apache.hadoop.hive.metastore.api.SQLForeignKey; +import org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint; +import org.apache.hadoop.hive.metastore.api.SQLPrimaryKey; +import org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint; +import org.apache.hadoop.hive.metastore.api.ScheduledQuery; +import org.apache.hadoop.hive.metastore.api.ScheduledQueryKey; +import org.apache.hadoop.hive.metastore.api.ScheduledQueryMaintenanceRequest; +import org.apache.hadoop.hive.metastore.api.ScheduledQueryPollRequest; +import org.apache.hadoop.hive.metastore.api.ScheduledQueryPollResponse; +import org.apache.hadoop.hive.metastore.api.ScheduledQueryProgressInfo; +import org.apache.hadoop.hive.metastore.api.SchemaVersion; +import org.apache.hadoop.hive.metastore.api.SchemaVersionState; +import org.apache.hadoop.hive.metastore.api.SerDeInfo; +import org.apache.hadoop.hive.metastore.api.SetPartitionsStatsRequest; +import org.apache.hadoop.hive.metastore.api.ShowCompactRequest; +import org.apache.hadoop.hive.metastore.api.ShowCompactResponse; +import org.apache.hadoop.hive.metastore.api.ShowLocksRequest; +import org.apache.hadoop.hive.metastore.api.ShowLocksResponse; +import org.apache.hadoop.hive.metastore.api.StorageDescriptor; +import org.apache.hadoop.hive.metastore.api.StoredProcedure; +import org.apache.hadoop.hive.metastore.api.StoredProcedureRequest; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.metastore.api.TableMeta; +import org.apache.hadoop.hive.metastore.api.TableValidWriteIds; +import org.apache.hadoop.hive.metastore.api.TxnAbortedException; +import org.apache.hadoop.hive.metastore.api.TxnOpenException; +import org.apache.hadoop.hive.metastore.api.TxnToWriteId; +import org.apache.hadoop.hive.metastore.api.TxnType; +import org.apache.hadoop.hive.metastore.api.UniqueConstraintsRequest; +import org.apache.hadoop.hive.metastore.api.UnknownDBException; +import org.apache.hadoop.hive.metastore.api.UnknownPartitionException; +import org.apache.hadoop.hive.metastore.api.UnknownTableException; +import org.apache.hadoop.hive.metastore.api.UpdateTransactionalStatsRequest; +import org.apache.hadoop.hive.metastore.api.WMFullResourcePlan; +import org.apache.hadoop.hive.metastore.api.WMMapping; +import org.apache.hadoop.hive.metastore.api.WMNullablePool; +import org.apache.hadoop.hive.metastore.api.WMNullableResourcePlan; +import org.apache.hadoop.hive.metastore.api.WMPool; +import org.apache.hadoop.hive.metastore.api.WMResourcePlan; +import org.apache.hadoop.hive.metastore.api.WMTrigger; +import org.apache.hadoop.hive.metastore.api.WMValidateResourcePlanResponse; +import org.apache.hadoop.hive.metastore.api.WriteEventInfo; +import org.apache.hadoop.hive.metastore.api.WriteNotificationLogBatchRequest; +import org.apache.hadoop.hive.metastore.api.WriteNotificationLogRequest; +import org.apache.hadoop.hive.metastore.partition.spec.PartitionSpecProxy; +import org.apache.hadoop.hive.serde.serdeConstants; +import org.apache.iceberg.BaseTable; +import org.apache.iceberg.CatalogUtil; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.SessionCatalog; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.exceptions.NoSuchTableException; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.rest.RESTCatalog; +import org.apache.thrift.TException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class HiveIcebergRESTCatalogClientAdapter implements IMetaStoreClient { + + private static final Logger LOG = LoggerFactory.getLogger(HiveIcebergRESTCatalogClientAdapter.class); + public static final String NAMESPACE_SEPARATOR = "."; + public static final String NAME = "name"; + public static final String LOCATION = "location"; + public static final String CATALOG_NAME = "iceberg.catalog"; + public static final String DB_OWNER = "owner"; + public static final String DB_OWNER_TYPE = "ownerType"; + public static final String SERDE_INFO = "serdeInfo"; + public static final String DEFAULT_INPUT_FORMAT_CLASS = "org.apache.iceberg.mr.hive.HiveIcebergInputFormat"; + public static final String DEFAULT_OUTPUT_FORMAT_CLASS + = "org.apache.iceberg.mr.hive.HiveIcebergOutputFormat"; + public static final String DEFAULT_SERDE_CLASS = "org.apache.iceberg.mr.hive.HiveIcebergSerDe"; + public static final String CATALOG_CONFIG_PREFIX = "iceberg.catalog."; + private final Configuration conf; + private RESTCatalog restCatalog; + private final HiveMetaHookLoader hookLoader; + + private final long maxHiveTablePropertySize; + + public HiveIcebergRESTCatalogClientAdapter(Configuration conf, HiveMetaHookLoader hookLoader) { + this.conf = conf; + this.hookLoader = hookLoader; + this.maxHiveTablePropertySize = conf.getLong(HiveOperationsBase.HIVE_TABLE_PROPERTY_MAX_SIZE, + HiveOperationsBase.HIVE_TABLE_PROPERTY_MAX_SIZE_DEFAULT); + } + + @Override + public boolean isCompatibleWith(Configuration configuration) { + return false; + } + + @Override + public void setHiveAddedJars(String addedJars) { + + } + + @Override + public boolean isLocalMetaStore() { + return false; + } + + @Override + public void reconnect() throws MetaException { + SessionCatalog.SessionContext context = SessionCatalog.SessionContext.createEmpty(); + String catalogName = conf.get(CATALOG_NAME); + Map<String, String> properties = getCatalogProperties(conf, catalogName); + restCatalog = (RESTCatalog) CatalogUtil.buildIcebergCatalog(catalogName, properties, conf); + restCatalog.initialize(catalogName, properties); + } + private static Map<String, String> getCatalogProperties( + Configuration conf, String catalogName) { + Map<String, String> catalogProperties = Maps.newHashMap(); + String keyPrefix = CATALOG_CONFIG_PREFIX + catalogName; + conf.forEach(config -> { + if (config.getKey().startsWith(keyPrefix)) { + catalogProperties.put( + config.getKey().substring(keyPrefix.length() + 1), + config.getValue()); + } + }); + return catalogProperties; + } + @Override + public void close() { + + } + + @Override + public void setMetaConf(String key, String value) throws MetaException, TException { + + } + + @Override + public String getMetaConf(String key) throws MetaException, TException { + return ""; + } + + @Override + public void createCatalog(Catalog catalog) + throws AlreadyExistsException, InvalidObjectException, MetaException, TException { + } + + @Override + public void alterCatalog(String catalogName, Catalog newCatalog) + throws NoSuchObjectException, InvalidObjectException, MetaException, TException { + + } + + @Override + public Catalog getCatalog(String catName) throws NoSuchObjectException, MetaException, TException { + return null; + } + + @Override + public List<String> getCatalogs() throws MetaException, TException { + return new LinkedList<>(); + } + + @Override + public void dropCatalog(String catName) + throws NoSuchObjectException, InvalidOperationException, MetaException, TException { + + } + + @Override + public void dropCatalog(String catName, boolean ifExists) throws TException { + + } + + @Override + public List<String> getDatabases(String databasePattern) throws MetaException, TException { + return getAllDatabases(); + } + + @Override + public List<String> getDatabases(String catName, String databasePattern) throws MetaException, TException { + return getAllDatabases(); + } + + @Override + public List<String> getAllDatabases() throws MetaException, TException { + return restCatalog.listNamespaces(Namespace.empty()).stream().map(Namespace::toString).collect(Collectors.toList()); + } + + @Override + public List<String> getAllDatabases(String catName) throws MetaException, TException { + return getAllDatabases(); + } + + @Override + public List<String> getTables(String dbName, String tablePattern) + throws MetaException, TException, UnknownDBException { + return getTables(null, dbName, tablePattern, null); + } + + @Override + public List<String> getTables(String catName, String dbName, String tablePattern) + throws MetaException, TException, UnknownDBException { + return getTables(catName, dbName, tablePattern, null); + } + + @Override + public List<String> getTables(String dbName, String tablePattern, TableType tableType) + throws MetaException, TException, UnknownDBException { + return getTables(null, dbName, tablePattern, tableType); + } + + @Override + public List<String> getTables(String catName, String dbName, String tablePattern, TableType tableType) + throws MetaException, TException, UnknownDBException { + List<TableIdentifier> tableIdentifiers = restCatalog.listTables(Namespace.of(dbName)); + return tableIdentifiers.stream().map(tableIdentifier -> tableIdentifier.name()).collect(Collectors.toList()); + } + + @Override + public List<Table> getAllMaterializedViewObjectsForRewriting() throws MetaException, TException, UnknownDBException { + return new LinkedList<>(); + } + + @Override + public List<ExtendedTableInfo> getTablesExt(String catName, String dbName, String tablePattern, int requestedFields, + int limit) throws MetaException, TException { + return new LinkedList<>(); + } + + @Override + public List<String> getMaterializedViewsForRewriting(String dbName) + throws MetaException, TException, UnknownDBException { + return new LinkedList<>(); + } + + @Override + public List<String> getMaterializedViewsForRewriting(String catName, String dbName) + throws MetaException, TException, UnknownDBException { + return new LinkedList<>(); + } + + @Override + public List<TableMeta> getTableMeta(String dbPatterns, String tablePatterns, List<String> tableTypes) + throws MetaException, TException, UnknownDBException { + return new LinkedList<>(); + } + + @Override + public List<TableMeta> getTableMeta(String catName, String dbPatterns, String tablePatterns, List<String> tableTypes) + throws MetaException, TException, UnknownDBException { + return new LinkedList<>(); + } + + @Override + public List<String> getAllTables(String dbName) throws MetaException, TException, UnknownDBException { + return getTables(null, dbName, "", null); + } + + @Override + public List<String> getAllTables(String catName, String dbName) throws MetaException, TException, UnknownDBException { + return getTables(catName, dbName, "", null); + } + + @Override + public List<String> listTableNamesByFilter(String dbName, String filter, short maxTables) + throws TException, InvalidOperationException, UnknownDBException { + return new LinkedList<>(); + } + + @Override + public List<String> listTableNamesByFilter(String catName, String dbName, String filter, int maxTables) + throws TException, InvalidOperationException, UnknownDBException { + return new LinkedList<>(); + } + + @Override + public void dropTable(String dbname, String tableName, boolean deleteData, boolean ignoreUnknownTab) + throws MetaException, TException, NoSuchObjectException { + dropTable(dbname, tableName); + } + + @Override + public void dropTable(String dbname, String tableName, boolean deleteData, boolean ignoreUnknownTab, boolean ifPurge) + throws MetaException, TException, NoSuchObjectException { + dropTable(dbname, tableName); + } + + @Override + public void dropTable(Table table, boolean deleteData, boolean ignoreUnknownTab, boolean ifPurge) throws TException { + dropTable(table.getDbName(), table.getTableName()); + } + + @Override + public void dropTable(String dbname, String tableName) throws MetaException, TException, NoSuchObjectException { + restCatalog.dropTable(TableIdentifier.of(dbname, tableName)); + } + + @Override + public void dropTable(String catName, String dbName, String tableName, boolean deleteData, boolean ignoreUnknownTable, + boolean ifPurge) throws MetaException, NoSuchObjectException, TException { + dropTable(dbName, tableName); + } + + @Override + public void truncateTable(String dbName, String tableName, List<String> partNames) throws MetaException, TException { + + } + + @Override + public void truncateTable(TableName table, List<String> partNames) throws TException { + + } + + @Override + public void truncateTable(String dbName, String tableName, List<String> partNames, String validWriteIds, long writeId) + throws TException { + + } + + @Override + public void truncateTable(String dbName, String tableName, List<String> partNames, String validWriteIds, long writeId, + boolean deleteData) throws TException { + + } + + @Override + public void truncateTable(String catName, String dbName, String tableName, List<String> partNames) + throws MetaException, TException { + + } + + @Override + public CmRecycleResponse recycleDirToCmPath(CmRecycleRequest request) throws MetaException, TException { + return new CmRecycleResponse(); + } + + @Override + public boolean tableExists(String databaseName, String tableName) + throws MetaException, TException, UnknownDBException { + try { + getTables(databaseName, tableName); + } catch (NoSuchTableException e) { + return false; + } + return true; + } + + @Override + public boolean tableExists(String catName, String dbName, String tableName) + throws MetaException, TException, UnknownDBException { + return tableExists(dbName, tableName); + } + + @Override + public Database getDatabase(String databaseName) throws NoSuchObjectException, MetaException, TException { + return restCatalog.listNamespaces(Namespace.empty()).stream() + .filter(namespace -> namespace.levels()[0].equals(databaseName)).map(namespace -> { + Database database = new Database(); + database.setName(String.join(NAMESPACE_SEPARATOR, namespace.levels())); + Map<String, String> namespaceMetadata = restCatalog.loadNamespaceMetadata(Namespace.of(databaseName)); + database.setLocationUri(namespaceMetadata.get(LOCATION)); + database.setCatalogName("REST"); + database.setOwnerName(namespaceMetadata.get(DB_OWNER)); + try { + database.setOwnerType(PrincipalType.valueOf(namespaceMetadata.get(DB_OWNER_TYPE))); + } catch (Exception e) { + LOG.warn("Can not set ownerType: {}", namespaceMetadata.get(DB_OWNER_TYPE), e); + } + return database; + }).findFirst().get(); + } + + @Override + public Database getDatabase(String catalogName, String databaseName) + throws NoSuchObjectException, MetaException, TException { + return getDatabase(databaseName); + } + + @Override + public Table getTable(String dbName, String tableName) throws MetaException, TException, NoSuchObjectException { + org.apache.iceberg.Table icebergTable = null; + try { + icebergTable = restCatalog.loadTable(TableIdentifier.of(dbName, tableName)); + } catch (NoSuchTableException exception) { + throw new NoSuchObjectException(); + } + Table hiveTable = convertIcebergTableToHiveTable(icebergTable); + return hiveTable; + } + private Table convertIcebergTableToHiveTable(org.apache.iceberg.Table icebergTable) { + Table hiveTable = new Table(); + TableMetadata metadata = ((BaseTable) icebergTable).operations().current(); + HMSTablePropertyHelper.updateHmsTableForIcebergTable(metadata.metadataFileLocation(), hiveTable, + metadata, null, true, maxHiveTablePropertySize, null); + hiveTable.getParameters().put(CATALOG_NAME, CatalogUtil.ICEBERG_CATALOG_TYPE_REST); + hiveTable.setTableName(getTableName(icebergTable)); + hiveTable.setDbName(getDbName(icebergTable)); + StorageDescriptor storageDescriptor = new StorageDescriptor(); + hiveTable.setSd(storageDescriptor); + hiveTable.setTableType("EXTERNAL_TABLE"); + hiveTable.setPartitionKeys(new LinkedList<>()); + List<FieldSchema> cols = new LinkedList<>(); + storageDescriptor.setCols(cols); + storageDescriptor.setLocation(icebergTable.location()); + storageDescriptor.setInputFormat(DEFAULT_INPUT_FORMAT_CLASS); + storageDescriptor.setOutputFormat(DEFAULT_OUTPUT_FORMAT_CLASS); + storageDescriptor.setBucketCols(new LinkedList<>()); + storageDescriptor.setSortCols(new LinkedList<>()); + storageDescriptor.setParameters(Maps.newHashMap()); + if (icebergTable.properties().containsKey(SERDE_INFO)) { + // TODO + } else { + SerDeInfo serDeInfo = new SerDeInfo("icebergSerde", DEFAULT_SERDE_CLASS, Maps.newHashMap()); + serDeInfo.getParameters().put(serdeConstants.SERIALIZATION_FORMAT, "1"); // Default serialization format. + storageDescriptor.setSerdeInfo(serDeInfo); + } + icebergTable.schema().columns().forEach(icebergColumn -> { + FieldSchema fieldSchema = new FieldSchema(); + fieldSchema.setName(icebergColumn.name()); + fieldSchema.setType(icebergColumn.type().toString()); + cols.add(fieldSchema); + }); + return hiveTable; + } + private String getTableName(org.apache.iceberg.Table icebergTable) { + String[] nameParts = icebergTable.name().split("\\."); + if (nameParts.length == 3) { + return nameParts[2]; + } + if (nameParts.length == 2) { + return nameParts[1]; + } + return icebergTable.name(); + } + + private String getDbName(org.apache.iceberg.Table icebergTable) { + String[] nameParts = icebergTable.name().split("\\."); + return nameParts.length == 3 ? nameParts[1] : nameParts[0]; + } + + @Override + public Table getTable(String dbName, String tableName, boolean getColumnStats, String engine) + throws MetaException, TException, NoSuchObjectException { + return getTable(dbName, tableName); + } + + @Override + public Table getTable(String catName, String dbName, String tableName) throws MetaException, TException { + return getTable(dbName, tableName); + } + + @Override + public Table getTable(String catName, String dbName, String tableName, String validWriteIdList) throws TException { + return getTable(dbName, tableName); + } + + @Override + public Table getTable(String catName, String dbName, String tableName, String validWriteIdList, + boolean getColumnStats, String engine) throws TException { + return getTable(dbName, tableName); + } + + @Override + public Table getTable(GetTableRequest getTableRequest) throws MetaException, TException, NoSuchObjectException { + return getTable(getTableRequest.getDbName(), getTableRequest.getTblName()); + } + + @Override + public List<Table> getTableObjectsByName(String dbName, List<String> tableNames) + throws MetaException, InvalidOperationException, UnknownDBException, TException { + return null; + } + + @Override + public List<Table> getTables(String catName, String dbName, List<String> tableNames, + GetProjectionsSpec projectionsSpec) + throws MetaException, InvalidOperationException, UnknownDBException, TException { + return new LinkedList<>(); + } + + @Override + public List<Table> getTableObjectsByName(String catName, String dbName, List<String> tableNames) + throws MetaException, InvalidOperationException, UnknownDBException, TException { + return new LinkedList<>(); + } + + @Override + public Materialization getMaterializationInvalidationInfo(CreationMetadata cm, String validTxnList) + throws MetaException, InvalidOperationException, UnknownDBException, TException { + return new Materialization(); + } + + @Override + public void updateCreationMetadata(String dbName, String tableName, CreationMetadata cm) + throws MetaException, TException { + } + + @Override + public void updateCreationMetadata(String catName, String dbName, String tableName, CreationMetadata cm) + throws MetaException, TException { + + } + + @Override + public Partition appendPartition(String dbName, String tableName, List<String> partVals) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return new Partition(); + } + + @Override + public Partition appendPartition(String catName, String dbName, String tableName, List<String> partVals) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return new Partition(); + } + + @Override + public Partition appendPartition(String dbName, String tableName, String name) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return new Partition(); + } + + @Override + public Partition appendPartition(String catName, String dbName, String tableName, String name) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return new Partition(); + } + + @Override + public Partition add_partition(Partition partition) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return new Partition(); + } + + @Override + public int add_partitions(List<Partition> partitions) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return 0; + } + + @Override + public int add_partitions_pspec(PartitionSpecProxy partitionSpec) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return 0; + } + + @Override + public List<Partition> add_partitions(List<Partition> partitions, boolean ifNotExists, boolean needResults) + throws InvalidObjectException, AlreadyExistsException, MetaException, TException { + return null; + } + + @Override + public Partition getPartition(String dbName, String tblName, List<String> partVals) + throws NoSuchObjectException, MetaException, TException { + return new Partition(); + } + + @Override + public GetPartitionResponse getPartitionRequest(GetPartitionRequest req) + throws NoSuchObjectException, MetaException, TException { + return new GetPartitionResponse(); + } + + @Override + public Partition getPartition(String catName, String dbName, String tblName, List<String> partVals) + throws NoSuchObjectException, MetaException, TException { + return new Partition(); + } + + @Override + public Partition exchange_partition(Map<String, String> partitionSpecs, String sourceDb, String sourceTable, + String destdb, String destTableName) + throws MetaException, NoSuchObjectException, InvalidObjectException, TException { + return new Partition(); + } + + @Override + public Partition exchange_partition(Map<String, String> partitionSpecs, String sourceCat, String sourceDb, + String sourceTable, String destCat, String destdb, String destTableName) + throws MetaException, NoSuchObjectException, InvalidObjectException, TException { + return new Partition(); + } + + @Override + public List<Partition> exchange_partitions(Map<String, String> partitionSpecs, String sourceDb, String sourceTable, + String destdb, String destTableName) + throws MetaException, NoSuchObjectException, InvalidObjectException, TException { + return new LinkedList<>(); + } + + @Override + public List<Partition> exchange_partitions(Map<String, String> partitionSpecs, String sourceCat, String sourceDb, + String sourceTable, String destCat, String destdb, String destTableName) + throws MetaException, NoSuchObjectException, InvalidObjectException, TException { + return new LinkedList<>(); + } + + @Override + public Partition getPartition(String dbName, String tblName, String name) + throws MetaException, UnknownTableException, NoSuchObjectException, TException { + return new Partition(); + } + + @Override + public Partition getPartition(String catName, String dbName, String tblName, String name) + throws MetaException, UnknownTableException, NoSuchObjectException, TException { + return new Partition(); + } + + @Override + public Partition getPartitionWithAuthInfo(String dbName, String tableName, List<String> pvals, String userName, + List<String> groupNames) throws MetaException, UnknownTableException, NoSuchObjectException, TException { + return new Partition(); + } + + @Override + public Partition getPartitionWithAuthInfo(String catName, String dbName, String tableName, List<String> pvals, + String userName, List<String> groupNames) + throws MetaException, UnknownTableException, NoSuchObjectException, TException { + return null; + } + + @Override + public List<Partition> listPartitions(String db_name, String tbl_name, short max_parts) + throws NoSuchObjectException, MetaException, TException { + return new LinkedList<>(); + } + + @Override + public List<Partition> listPartitions(String catName, String db_name, String tbl_name, int max_parts) + throws NoSuchObjectException, MetaException, TException { + return new LinkedList<>(); + } + + @Override + public PartitionSpecProxy listPartitionSpecs(String dbName, String tableName, int maxParts) throws TException { + return null; + } + + @Override + public PartitionSpecProxy listPartitionSpecs(String catName, String dbName, String tableName, int maxParts) + throws TException { + return null; + } + + @Override + public List<Partition> listPartitions(String db_name, String tbl_name, List<String> part_vals, short max_parts) + throws NoSuchObjectException, MetaException, TException { + return new LinkedList<>(); + } + + @Override + public List<Partition> listPartitions(String catName, String db_name, String tbl_name, List<String> part_vals, + int max_parts) throws NoSuchObjectException, MetaException, TException { Review Comment: String db_name, String tbl_name, List<String> part_vals -- 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. To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org