satishkotha commented on a change in pull request #2879:
URL: https://github.com/apache/hudi/pull/2879#discussion_r629573686



##########
File path: 
hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/ddl/HMSDDLExecutor.java
##########
@@ -0,0 +1,225 @@
+/*
+ * 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.hudi.hive.ddl;
+
+import org.apache.hudi.common.fs.FSUtils;
+import org.apache.hudi.common.fs.StorageSchemes;
+import org.apache.hudi.hive.HiveSyncConfig;
+import org.apache.hudi.hive.HoodieHiveSyncException;
+import org.apache.hudi.hive.PartitionValueExtractor;
+import org.apache.hudi.hive.util.HiveSchemaUtil;
+
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.common.StatsSetupConst;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.IMetaStoreClient;
+import org.apache.hadoop.hive.metastore.TableType;
+import org.apache.hadoop.hive.metastore.api.Database;
+import org.apache.hadoop.hive.metastore.api.EnvironmentContext;
+import org.apache.hadoop.hive.metastore.api.FieldSchema;
+import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.api.Partition;
+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.ql.metadata.Hive;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.parquet.schema.MessageType;
+import org.apache.thrift.TException;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public class HMSDDLExecutor implements DDLExecutor {

Review comment:
       can you add class javadoc please

##########
File path: 
hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/HiveSyncConfig.java
##########
@@ -73,6 +73,9 @@
   @Parameter(names = {"--use-jdbc"}, description = "Hive jdbc connect url")
   public Boolean useJdbc = true;
 
+  @Parameter(names = {"--use-hms"}, description = "Use hms client for ddl 
commands")
+  public Boolean useHMS = false;

Review comment:
       any thoughts on introducing other option instead of using booleans? only 
one of useJdbc/useHms is allowed to be true. can we introduce something like 
sync_mode=jdbc/hms etc? We can still keep the flags for backward compatibility 
for few more versions. wdyt?

##########
File path: 
hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/HoodieHiveClient.java
##########
@@ -68,24 +57,31 @@
 
   private static final Logger LOG = 
LogManager.getLogger(HoodieHiveClient.class);
   private final PartitionValueExtractor partitionValueExtractor;
+  DDLExecutor ddlExecutor;
   private IMetaStoreClient client;
   private HiveSyncConfig syncConfig;
   private FileSystem fs;
   private Connection connection;
   private HoodieTimeline activeTimeline;
   private HiveConf configuration;
 
-  public HoodieHiveClient(HiveSyncConfig cfg, HiveConf configuration, 
FileSystem fs) {
+  public HoodieHiveClient(HiveSyncConfig cfg, HiveConf configuration, 
FileSystem fs) throws HiveException, MetaException {
     super(cfg.basePath, cfg.assumeDatePartitioning, 
cfg.useFileListingFromMetadata, cfg.verifyMetadataFileListing, fs);
     this.syncConfig = cfg;
     this.fs = fs;
 
     this.configuration = configuration;
     // Support both JDBC and metastore based implementations for backwards 
compatiblity. Future users should
     // disable jdbc and depend on metastore client for all hive registrations
-    if (cfg.useJdbc) {
-      LOG.info("Creating hive connection " + cfg.jdbcUrl);
-      createHiveConnection();
+
+    if (cfg.useHMS) {

Review comment:
       nit: probably better to keep cfg.useJdbc as first check to ensure jdbc 
takes precedence over hms. 

##########
File path: 
hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/HoodieHiveClient.java
##########
@@ -258,94 +179,26 @@ private String getPartitionClause(String partition) {
   }
 
   void updateTableDefinition(String tableName, MessageType newSchema) {
-    try {
-      String newSchemaStr = HiveSchemaUtil.generateSchemaString(newSchema, 
syncConfig.partitionFields, syncConfig.supportTimestamp);
-      // Cascade clause should not be present for non-partitioned tables
-      String cascadeClause = syncConfig.partitionFields.size() > 0 ? " 
cascade" : "";
-      StringBuilder sqlBuilder = new StringBuilder("ALTER TABLE 
").append(HIVE_ESCAPE_CHARACTER)
-              
.append(syncConfig.databaseName).append(HIVE_ESCAPE_CHARACTER).append(".")
-              .append(HIVE_ESCAPE_CHARACTER).append(tableName)
-              .append(HIVE_ESCAPE_CHARACTER).append(" REPLACE COLUMNS(")
-              .append(newSchemaStr).append(" )").append(cascadeClause);
-      LOG.info("Updating table definition with " + sqlBuilder);
-      updateHiveSQL(sqlBuilder.toString());
-    } catch (IOException e) {
-      throw new HoodieHiveSyncException("Failed to update table for " + 
tableName, e);
-    }
+    ddlExecutor.updateTableDefinition(tableName, newSchema);
   }
 
   @Override
   public void createTable(String tableName, MessageType storageSchema, String 
inputFormatClass,
                           String outputFormatClass, String serdeClass,
                           Map<String, String> serdeProperties, Map<String, 
String> tableProperties) {
-    try {
-      String createSQLQuery =
-          HiveSchemaUtil.generateCreateDDL(tableName, storageSchema, 
syncConfig, inputFormatClass,
-              outputFormatClass, serdeClass, serdeProperties, tableProperties);
-      LOG.info("Creating table with " + createSQLQuery);
-      updateHiveSQL(createSQLQuery);
-    } catch (IOException e) {
-      throw new HoodieHiveSyncException("Failed to create table " + tableName, 
e);
-    }
+    ddlExecutor.createTable(tableName, storageSchema, inputFormatClass, 
outputFormatClass, serdeClass, serdeProperties, tableProperties);
   }
 
   /**
    * Get the table schema.
    */
   @Override
   public Map<String, String> getTableSchema(String tableName) {
-    if (syncConfig.useJdbc) {
-      if (!doesTableExist(tableName)) {
-        throw new IllegalArgumentException(
-            "Failed to get schema for table " + tableName + " does not exist");
-      }
-      Map<String, String> schema = new HashMap<>();
-      ResultSet result = null;
-      try {
-        DatabaseMetaData databaseMetaData = connection.getMetaData();
-        result = databaseMetaData.getColumns(null, syncConfig.databaseName, 
tableName, null);
-        while (result.next()) {
-          String columnName = result.getString(4);
-          String columnType = result.getString(6);
-          if ("DECIMAL".equals(columnType)) {
-            int columnSize = result.getInt("COLUMN_SIZE");
-            int decimalDigits = result.getInt("DECIMAL_DIGITS");
-            columnType += String.format("(%s,%s)", columnSize, decimalDigits);
-          }
-          schema.put(columnName, columnType);
-        }
-        return schema;
-      } catch (SQLException e) {
-        throw new HoodieHiveSyncException("Failed to get table schema for " + 
tableName, e);
-      } finally {
-        closeQuietly(result, null);
-      }
-    } else {
-      return getTableSchemaUsingMetastoreClient(tableName);
-    }
-  }
-
-  public Map<String, String> getTableSchemaUsingMetastoreClient(String 
tableName) {
-    try {
-      // HiveMetastoreClient returns partition keys separate from Columns, 
hence get both and merge to
-      // get the Schema of the table.
-      final long start = System.currentTimeMillis();
-      Table table = this.client.getTable(syncConfig.databaseName, tableName);
-      Map<String, String> partitionKeysMap =
-          
table.getPartitionKeys().stream().collect(Collectors.toMap(FieldSchema::getName,
 f -> f.getType().toUpperCase()));
-
-      Map<String, String> columnsMap =
-          
table.getSd().getCols().stream().collect(Collectors.toMap(FieldSchema::getName, 
f -> f.getType().toUpperCase()));
-
-      Map<String, String> schema = new HashMap<>();
-      schema.putAll(columnsMap);
-      schema.putAll(partitionKeysMap);
-      final long end = System.currentTimeMillis();
-      LOG.info(String.format("Time taken to getTableSchema: %s ms", (end - 
start)));
-      return schema;
-    } catch (Exception e) {
-      throw new HoodieHiveSyncException("Failed to get table schema for : " + 
tableName, e);
+    if (!doesTableExist(tableName)) {

Review comment:
       Earlier this check seems to be present only for jdbc executor. Do we 
need it for all cases? (probably not a big deal, but making sure there is no 
redundant checks)

##########
File path: 
hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/ddl/HMSDDLExecutor.java
##########
@@ -0,0 +1,225 @@
+/*
+ * 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.hudi.hive.ddl;
+
+import org.apache.hudi.common.fs.FSUtils;
+import org.apache.hudi.common.fs.StorageSchemes;
+import org.apache.hudi.hive.HiveSyncConfig;
+import org.apache.hudi.hive.HoodieHiveSyncException;
+import org.apache.hudi.hive.PartitionValueExtractor;
+import org.apache.hudi.hive.util.HiveSchemaUtil;
+
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.common.StatsSetupConst;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.IMetaStoreClient;
+import org.apache.hadoop.hive.metastore.TableType;
+import org.apache.hadoop.hive.metastore.api.Database;
+import org.apache.hadoop.hive.metastore.api.EnvironmentContext;
+import org.apache.hadoop.hive.metastore.api.FieldSchema;
+import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.api.Partition;
+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.ql.metadata.Hive;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.parquet.schema.MessageType;
+import org.apache.thrift.TException;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public class HMSDDLExecutor implements DDLExecutor {
+  private static final Logger LOG = LogManager.getLogger(HMSDDLExecutor.class);
+  private final HiveSyncConfig syncConfig;
+  private final PartitionValueExtractor partitionValueExtractor;
+  private final FileSystem fs;
+  private IMetaStoreClient client;
+
+  public HMSDDLExecutor(HiveConf conf, HiveSyncConfig syncConfig, FileSystem 
fs) throws HiveException, MetaException {
+    this.client = Hive.get(conf).getMSC();
+    this.syncConfig = syncConfig;
+    this.fs = fs;
+    try {
+      this.partitionValueExtractor =
+          (PartitionValueExtractor) 
Class.forName(syncConfig.partitionValueExtractorClass).newInstance();
+    } catch (Exception e) {
+      throw new HoodieHiveSyncException(
+          "Failed to initialize PartitionValueExtractor class " + 
syncConfig.partitionValueExtractorClass, e);
+    }
+  }
+
+  @Override
+  public void createDatabase(String databaseName) {
+    try {
+      Database database = new Database(databaseName, "automatically created by 
hoodie", null, null);
+      client.createDatabase(database);
+    } catch (Exception e) {
+      LOG.error("Failed to create database " + databaseName, e);
+      throw new HoodieHiveSyncException("Failed to create database " + 
databaseName, e);
+    }
+  }
+
+  @Override
+  public void createTable(String tableName, MessageType storageSchema, String 
inputFormatClass, String outputFormatClass, String serdeClass, Map<String, 
String> serdeProperties,
+                          Map<String, String> tableProperties) {
+    try {
+      List<FieldSchema> fieldSchema = 
HiveSchemaUtil.convertParquetSchemaToHiveFieldSchema(storageSchema, syncConfig);
+      Map<String, String> hiveSchema = 
HiveSchemaUtil.convertParquetSchemaToHiveSchema(storageSchema, 
syncConfig.supportTimestamp);

Review comment:
        For certain large schemas it is expensive to do these multiple 
conversions 1) convertParquetSchemaToHiveFieldSchema and 2) 
convertParquetSchemaToHiveSchema
   
   Can we avoid one of them? Maybe change getPartitionKeyType to work 
List<FieldSchema>? or change convertParquetSchemaToHiveFieldSchema to return 
partitionSchema as well?

##########
File path: 
hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/HiveSyncTool.java
##########
@@ -68,7 +70,7 @@ public HiveSyncTool(HiveSyncConfig cfg, HiveConf 
configuration, FileSystem fs) {
 
     try {
       this.hoodieHiveClient = new HoodieHiveClient(cfg, configuration, fs);
-    } catch (RuntimeException e) {
+    } catch (RuntimeException | HiveException | MetaException e) { 
//TODO-jsbali FIx this

Review comment:
       minor: revert this change?

##########
File path: 
hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/ddl/DDLExecutor.java
##########
@@ -0,0 +1,42 @@
+/*
+ * 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.hudi.hive.ddl;
+
+import org.apache.parquet.schema.MessageType;
+
+import java.util.List;
+import java.util.Map;
+
+public interface DDLExecutor {

Review comment:
       Please add javadoc for high level public interfaces




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


Reply via email to