liunaijie commented on code in PR #9743:
URL: https://github.com/apache/seatunnel/pull/9743#discussion_r2295151723


##########
seatunnel-connectors-v2/connector-hive/src/main/java/org/apache/seatunnel/connectors/seatunnel/hive/sink/HiveSaveModeHandler.java:
##########
@@ -0,0 +1,460 @@
+/*
+ * 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.seatunnel.connectors.seatunnel.hive.sink;
+
+import org.apache.seatunnel.api.configuration.ReadonlyConfig;
+import org.apache.seatunnel.api.sink.DataSaveMode;
+import org.apache.seatunnel.api.sink.SaveModeHandler;
+import org.apache.seatunnel.api.sink.SchemaSaveMode;
+import org.apache.seatunnel.api.table.catalog.Catalog;
+import org.apache.seatunnel.api.table.catalog.CatalogTable;
+import org.apache.seatunnel.api.table.catalog.TablePath;
+import org.apache.seatunnel.api.table.catalog.TableSchema;
+import org.apache.seatunnel.connectors.seatunnel.hive.config.HiveOptions;
+import 
org.apache.seatunnel.connectors.seatunnel.hive.exception.HiveConnectorErrorCode;
+import 
org.apache.seatunnel.connectors.seatunnel.hive.exception.HiveConnectorException;
+import org.apache.seatunnel.connectors.seatunnel.hive.utils.HiveFormatUtils;
+import org.apache.seatunnel.connectors.seatunnel.hive.utils.HiveMetaStoreProxy;
+import org.apache.seatunnel.connectors.seatunnel.hive.utils.HiveTypeConvertor;
+
+import org.apache.hadoop.hive.metastore.api.FieldSchema;
+import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.thrift.TException;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+@Slf4j
+public class HiveSaveModeHandler implements SaveModeHandler, AutoCloseable {
+
+    private final ReadonlyConfig readonlyConfig;
+    private final CatalogTable catalogTable;
+    private final SchemaSaveMode schemaSaveMode;
+    private final String createTemplate;
+    private final TablePath tablePath;
+    private final String dbName;
+    private final String tableName;
+    private final TableSchema tableSchema;
+    private final List<String> partitionFields;
+    private final List<String> sourceFieldNames;
+    private final List<String> partitionFieldsFromSource;
+    private final List<String> nonPartitionFields;
+
+    private HiveMetaStoreProxy hiveMetaStoreProxy;
+
+    public HiveSaveModeHandler(
+            ReadonlyConfig readonlyConfig,
+            CatalogTable catalogTable,
+            SchemaSaveMode schemaSaveMode,
+            String createTemplate) {
+        this.readonlyConfig = readonlyConfig;
+        this.catalogTable = catalogTable;
+        this.schemaSaveMode = schemaSaveMode;
+        this.createTemplate = createTemplate;
+        this.tablePath = 
TablePath.of(readonlyConfig.get(HiveOptions.TABLE_NAME));
+        this.dbName = tablePath.getDatabaseName();
+        this.tableName = tablePath.getTableName();
+        this.tableSchema = catalogTable.getTableSchema();
+
+        // Initialize partition fields and validation
+        this.partitionFields = 
readonlyConfig.get(HiveSinkOptions.PARTITION_FIELDS);
+        this.sourceFieldNames =
+                tableSchema.getColumns().stream()
+                        
.map(org.apache.seatunnel.api.table.catalog.Column::getName)
+                        .collect(Collectors.toList());
+
+        // Validate and categorize partition fields
+        validatePartitionFields();
+        this.partitionFieldsFromSource =
+                partitionFields.stream()
+                        .filter(sourceFieldNames::contains)
+                        .collect(Collectors.toList());
+        this.nonPartitionFields =
+                sourceFieldNames.stream()
+                        .filter(field -> 
!partitionFieldsFromSource.contains(field))
+                        .collect(Collectors.toList());
+    }
+
+    @Override
+    public void open() {
+        this.hiveMetaStoreProxy = 
HiveMetaStoreProxy.getInstance(readonlyConfig);
+    }
+
+    @Override
+    public void handleSchemaSaveModeWithRestore() {
+        // For Hive, we use the same logic as handleSchemaSaveMode
+        handleSchemaSaveMode();
+    }
+
+    @Override
+    public TablePath getHandleTablePath() {
+        return tablePath;
+    }
+
+    @Override
+    public Catalog getHandleCatalog() {
+        // Hive doesn't use Catalog interface directly, return null
+        return null;
+    }
+
+    @Override
+    public SchemaSaveMode getSchemaSaveMode() {
+        return schemaSaveMode;
+    }
+
+    @Override
+    public DataSaveMode getDataSaveMode() {
+        // Hive uses OVERWRITE parameter for data handling
+        return DataSaveMode.APPEND_DATA;
+    }
+
+    @Override
+    public void close() throws Exception {
+        if (hiveMetaStoreProxy != null) {
+            hiveMetaStoreProxy.close();
+        }
+    }
+
+    @Override
+    public void handleSchemaSaveMode() {
+        try {
+            switch (schemaSaveMode) {
+                case RECREATE_SCHEMA:
+                    handleRecreateSchema();
+                    break;
+                case CREATE_SCHEMA_WHEN_NOT_EXIST:
+                    handleCreateSchemaWhenNotExist();
+                    break;
+                case ERROR_WHEN_SCHEMA_NOT_EXIST:
+                    handleErrorWhenSchemaNotExist();
+                    break;
+                case IGNORE:
+                    log.info(
+                            "Ignore schema save mode, skip schema handling for 
table {}.{}",
+                            dbName,
+                            tableName);
+                    break;
+                default:
+                    throw new HiveConnectorException(
+                            HiveConnectorErrorCode.CREATE_HIVE_TABLE_FAILED,
+                            "Unsupported schema save mode: " + schemaSaveMode);
+            }
+        } catch (Exception e) {
+            throw new HiveConnectorException(
+                    HiveConnectorErrorCode.CREATE_HIVE_TABLE_FAILED,
+                    "Failed to handle schema save mode: " + e.getMessage(),
+                    e);
+        }
+    }
+
+    @Override
+    public void handleDataSaveMode() {
+        // For Hive, data save mode is handled by the existing OVERWRITE 
parameter
+        // No additional data handling is needed here
+        log.info(
+                "Data save mode handling is managed by existing OVERWRITE 
parameter for table {}.{}",
+                dbName,
+                tableName);
+    }
+
+    private void handleRecreateSchema() throws TException {
+        log.info("Recreate schema mode: dropping and recreating table {}.{}", 
dbName, tableName);
+
+        // Create database if not exists
+        createDatabaseIfNotExists();
+
+        // Drop table if exists
+        if (hiveMetaStoreProxy.tableExists(dbName, tableName)) {
+            hiveMetaStoreProxy.dropTable(dbName, tableName);
+            log.info("Dropped existing table {}.{}", dbName, tableName);
+        }
+
+        // Create table
+        createTable();
+    }
+
+    private void handleCreateSchemaWhenNotExist() throws TException {
+        log.info("Create schema when not exist mode for table {}.{}", dbName, 
tableName);
+
+        // Create database if not exists
+        createDatabaseIfNotExists();
+
+        // Create table if not exists
+        if (!hiveMetaStoreProxy.tableExists(dbName, tableName)) {
+            createTable();
+        }
+    }
+
+    private void handleErrorWhenSchemaNotExist() throws TException {
+
+        // Check if database exists
+        if (!hiveMetaStoreProxy.databaseExists(dbName)) {
+            throw new HiveConnectorException(
+                    HiveConnectorErrorCode.CREATE_HIVE_TABLE_FAILED,
+                    "Database " + dbName + " does not exist");
+        }
+
+        if (!hiveMetaStoreProxy.tableExists(dbName, tableName)) {
+            throw new HiveConnectorException(
+                    HiveConnectorErrorCode.CREATE_HIVE_TABLE_FAILED,
+                    "Table " + dbName + "." + tableName + " does not exist");
+        }
+    }
+
+    private void createDatabaseIfNotExists() throws TException {
+        hiveMetaStoreProxy.createDatabaseIfNotExists(dbName);
+    }
+
+    private void createTable() throws TException {
+        String defaultTemplate = 
HiveSinkOptions.SAVE_MODE_CREATE_TEMPLATE.defaultValue();
+        boolean useCustomTemplate = !defaultTemplate.equals(createTemplate);
+
+        if (useCustomTemplate) {
+            createTableUsingTemplate();
+        } else {
+            createTableUsingAPI();
+        }
+    }
+
+    private void createTableUsingAPI() throws TException {
+        // Create table using Hive MetaStore API (more reliable than SQL)
+        Table table = buildTableFromSchema();
+        hiveMetaStoreProxy.createTableIfNotExists(table);
+    }
+
+    private void createTableUsingTemplate() throws TException {
+        processCreateTemplate();

Review Comment:
   I see a SQL template here, but the result not used. 
   Perhaps you meant to parse the sql result to a `Table`?



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