vinishjail97 commented on code in PR #600:
URL: https://github.com/apache/incubator-xtable/pull/600#discussion_r1887964667


##########
xtable-core/src/main/java/org/apache/xtable/catalog/hms/HMSCatalogSyncClient.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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.TABLE_TYPE_PROP;
+
+import java.time.ZonedDateTime;
+import java.util.Collections;
+import java.util.Locale;
+import java.util.Properties;
+
+import lombok.Getter;
+import lombok.extern.log4j.Log4j2;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.metastore.IMetaStoreClient;
+import org.apache.hadoop.hive.metastore.api.Database;
+import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.thrift.TException;
+
+import com.google.common.base.Strings;
+
+import org.apache.xtable.catalog.TableFormatUtils;
+import org.apache.xtable.conversion.ExternalCatalogConfig;
+import org.apache.xtable.conversion.SourceTable;
+import org.apache.xtable.exception.CatalogSyncException;
+import org.apache.xtable.exception.NotSupportedException;
+import org.apache.xtable.model.InternalTable;
+import org.apache.xtable.model.catalog.CatalogTableIdentifier;
+import org.apache.xtable.model.storage.TableFormat;
+import org.apache.xtable.spi.extractor.CatalogConversionSource;
+import org.apache.xtable.spi.sync.CatalogSyncClient;
+
+@Log4j2
+public class HMSCatalogSyncClient implements CatalogSyncClient<Table>, 
CatalogConversionSource {
+
+  private static final String TEMP_SUFFIX = "_temp";
+  private final ExternalCatalogConfig catalogConfig;
+  private final HMSCatalogConfig hmsCatalogConfig;
+  @Getter private final Configuration configuration;
+  private final IMetaStoreClient metaStoreClient;
+  @Getter private final HMSSchemaExtractor schemaExtractor;
+  private final IcebergHMSCatalogSyncHelper icebergHMSCatalogSyncHelper;

Review Comment:
   If I'm syncing hudi and delta table format, why do I need to load this in 
the constructor ? 



##########
xtable-core/src/main/java/org/apache/xtable/catalog/hms/HMSCatalogSyncClient.java:
##########
@@ -0,0 +1,264 @@
+/*
+ * 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.TABLE_TYPE_PROP;
+
+import java.time.ZonedDateTime;
+import java.util.Collections;
+import java.util.Locale;
+import java.util.Properties;
+
+import lombok.Getter;
+import lombok.extern.log4j.Log4j2;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.metastore.IMetaStoreClient;
+import org.apache.hadoop.hive.metastore.api.Database;
+import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.thrift.TException;
+
+import com.google.common.base.Strings;
+
+import org.apache.xtable.catalog.TableFormatUtils;
+import org.apache.xtable.conversion.ExternalCatalogConfig;
+import org.apache.xtable.conversion.SourceTable;
+import org.apache.xtable.exception.CatalogSyncException;
+import org.apache.xtable.exception.NotSupportedException;
+import org.apache.xtable.model.InternalTable;
+import org.apache.xtable.model.catalog.CatalogTableIdentifier;
+import org.apache.xtable.model.storage.TableFormat;
+import org.apache.xtable.spi.extractor.CatalogConversionSource;
+import org.apache.xtable.spi.sync.CatalogSyncClient;
+
+@Log4j2
+public class HMSCatalogSyncClient implements CatalogSyncClient<Table>, 
CatalogConversionSource {
+
+  private static final String TEMP_SUFFIX = "_temp";
+  private final ExternalCatalogConfig catalogConfig;
+  private final HMSCatalogConfig hmsCatalogConfig;
+  @Getter private final Configuration configuration;
+  private final IMetaStoreClient metaStoreClient;
+  @Getter private final HMSSchemaExtractor schemaExtractor;
+  private final IcebergHMSCatalogSyncHelper icebergHMSCatalogSyncHelper;

Review Comment:
   This field can be a class which takes tableFormat as a function argument 
rather than the class name - something like `HMSCatalogSyncRequestProvider`  ? 



##########
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:
   If you pass this as an argument from caller you can avoid needing syncClient 
as a field in this class and a separate class for iceberg. If you look at 
closely these methods will have similar implementation for delta and hudi 
except different parameters, think about it and let's discuss.
    



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

Reply via email to