kroushan-nit commented on code in PR #600: URL: https://github.com/apache/incubator-xtable/pull/600#discussion_r1888281933
########## xtable-core/src/main/java/org/apache/xtable/catalog/hms/IcebergHMSCatalogSyncHelper.java: ########## @@ -0,0 +1,135 @@ +/* + * 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.xtable.catalog.hms; + +import static org.apache.iceberg.BaseMetastoreTableOperations.METADATA_LOCATION_PROP; +import static org.apache.iceberg.BaseMetastoreTableOperations.PREVIOUS_METADATA_LOCATION_PROP; +import static org.apache.iceberg.BaseMetastoreTableOperations.TABLE_TYPE_PROP; + +import java.io.IOException; +import java.time.ZonedDateTime; +import java.util.HashMap; +import java.util.Map; + +import org.apache.hadoop.hive.metastore.TableType; +import org.apache.hadoop.hive.metastore.api.SerDeInfo; +import org.apache.hadoop.hive.metastore.api.StorageDescriptor; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants; +import org.apache.hadoop.security.UserGroupInformation; + +import org.apache.iceberg.BaseTable; +import org.apache.iceberg.hadoop.HadoopTables; +import org.apache.iceberg.mr.hive.HiveIcebergInputFormat; +import org.apache.iceberg.mr.hive.HiveIcebergOutputFormat; +import org.apache.iceberg.mr.hive.HiveIcebergSerDe; +import org.apache.iceberg.mr.hive.HiveIcebergStorageHandler; + +import com.google.common.annotations.VisibleForTesting; + +import org.apache.xtable.model.InternalTable; +import org.apache.xtable.model.catalog.CatalogTableIdentifier; +import org.apache.xtable.model.storage.TableFormat; + +class IcebergHMSCatalogSyncHelper { + + private static final String ICEBERG_CATALOG_NAME_PROP = "iceberg.catalog"; + private static final String ICEBERG_HADOOP_TABLE_NAME = "location_based_table"; + private final HMSCatalogSyncClient syncClient; + private final HadoopTables hadoopTables; + + IcebergHMSCatalogSyncHelper(HMSCatalogSyncClient syncClient) { + this.syncClient = syncClient; + this.hadoopTables = new HadoopTables(syncClient.getConfiguration()); + } + + @VisibleForTesting + IcebergHMSCatalogSyncHelper(HMSCatalogSyncClient syncClient, HadoopTables hadoopTables) { + this.syncClient = syncClient; + this.hadoopTables = hadoopTables; + } + + Table getNewTable(InternalTable table, CatalogTableIdentifier tableIdentifier) { + try { + Table newTb = new Table(); + newTb.setDbName(tableIdentifier.getDatabaseName()); + newTb.setTableName(tableIdentifier.getTableName()); + newTb.setOwner(UserGroupInformation.getCurrentUser().getShortUserName()); + newTb.setCreateTime((int) ZonedDateTime.now().toEpochSecond()); + newTb.setSd(getStorageDescriptor(table)); + newTb.setTableType(TableType.EXTERNAL_TABLE.toString()); + newTb.setParameters(getTableParameters(loadTableFromFs(table.getBasePath()))); + return newTb; + } catch (IOException e) { + throw new RuntimeException( + "Failed to set owner for hms table: " + tableIdentifier.getId(), e); + } + } + + Table getUpdatedTable(InternalTable table, Table catalogTable) { + BaseTable icebergTable = loadTableFromFs(table.getBasePath()); + Table copyTb = new Table(catalogTable); + Map<String, String> parameters = copyTb.getParameters(); + parameters.putAll(icebergTable.properties()); + String currentMetadataLocation = parameters.get(METADATA_LOCATION_PROP); + parameters.put(PREVIOUS_METADATA_LOCATION_PROP, currentMetadataLocation); + parameters.put(METADATA_LOCATION_PROP, getMetadataFileLocation(icebergTable)); + copyTb.setParameters(parameters); + copyTb + .getSd() + .setCols( + syncClient.getSchemaExtractor().toColumns(TableFormat.ICEBERG, table.getReadSchema())); Review Comment: the reason for passing syncClient itself is to access configuration and schemaExtractor from a single field instead of passing them separately. In future, if we require any other objects, we can initialize them in client and use it instead of updating method definitions -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
