yzeng1618 commented on code in PR #9743: URL: https://github.com/apache/seatunnel/pull/9743#discussion_r2384689984
########## seatunnel-connectors-v2/connector-hive/src/main/java/org/apache/seatunnel/connectors/seatunnel/hive/sink/HiveSaveModeHandler.java: ########## @@ -0,0 +1,420 @@ +/* + * 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.HiveMetaStoreCatalog; +import org.apache.seatunnel.connectors.seatunnel.hive.utils.HiveTableTemplateUtils; +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; + +@Slf4j +public class HiveSaveModeHandler implements SaveModeHandler, AutoCloseable { + + private final ReadonlyConfig readonlyConfig; + private final CatalogTable catalogTable; + private final SchemaSaveMode schemaSaveMode; + private final TablePath tablePath; + private final String dbName; + private final String tableName; + private final TableSchema tableSchema; + + private HiveMetaStoreCatalog hiveCatalog; + + public HiveSaveModeHandler( + ReadonlyConfig readonlyConfig, + CatalogTable catalogTable, + SchemaSaveMode schemaSaveMode) { + this.readonlyConfig = readonlyConfig; + this.catalogTable = catalogTable; + this.schemaSaveMode = schemaSaveMode; + this.tablePath = TablePath.of(readonlyConfig.get(HiveOptions.TABLE_NAME)); + this.dbName = tablePath.getDatabaseName(); + this.tableName = tablePath.getTableName(); + this.tableSchema = catalogTable.getTableSchema(); + } + + @Override + public void open() { + this.hiveCatalog = HiveMetaStoreCatalog.create(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() { + return hiveCatalog; + } + + @Override + public SchemaSaveMode getSchemaSaveMode() { + return schemaSaveMode; + } + + @Override + public DataSaveMode getDataSaveMode() { + DataSaveMode configured = readonlyConfig.get(HiveSinkOptions.DATA_SAVE_MODE); + boolean overwrite = readonlyConfig.get(HiveSinkOptions.OVERWRITE); + return overwrite ? DataSaveMode.DROP_DATA : configured; Review Comment: > Could you move it into `HiveSinkFactory`? > > ``` > Optional<Boolean> overwriteOptional = readonlyConfig.getOptional(HiveSinkOptions.OVERWRITE); > if (overwriteOptional.isPresent() && overwriteOptional.get()) { > // set data save mode to truncate > // ... > } > ``` The modification has been completed currently. -- 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]
