Hisoka-X commented on code in PR #5645: URL: https://github.com/apache/seatunnel/pull/5645#discussion_r1364805285
########## seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/utils/JdbcCatalogUtils.java: ########## @@ -0,0 +1,58 @@ +/* + * 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.jdbc.utils; + +import org.apache.seatunnel.api.configuration.ReadonlyConfig; +import org.apache.seatunnel.api.table.catalog.Catalog; +import org.apache.seatunnel.api.table.factory.CatalogFactory; +import org.apache.seatunnel.connectors.seatunnel.jdbc.catalog.JdbcCatalogOptions; +import org.apache.seatunnel.connectors.seatunnel.jdbc.catalog.utils.CatalogFactorySelector; +import org.apache.seatunnel.connectors.seatunnel.jdbc.config.JdbcConnectionConfig; + +import lombok.extern.slf4j.Slf4j; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +@Slf4j +public class JdbcCatalogUtils { + private static final String DEFAULT_CATALOG = "default"; + + private static ReadonlyConfig extractCatalogConfig(JdbcConnectionConfig config) { + Map<String, Object> catalogConfig = new HashMap<>(); + catalogConfig.put(JdbcCatalogOptions.BASE_URL.key(), config.getUrl()); + config.getUsername() + .ifPresent(val -> catalogConfig.put(JdbcCatalogOptions.USERNAME.key(), val)); + config.getPassword() + .ifPresent(val -> catalogConfig.put(JdbcCatalogOptions.PASSWORD.key(), val)); + return ReadonlyConfig.fromMap(catalogConfig); + } + + public static Optional<Catalog> findCatalog(JdbcConnectionConfig config) { + Optional<CatalogFactory> catalogFactory = CatalogFactorySelector.select(config.getUrl()); + if (catalogFactory.isPresent()) { + ReadonlyConfig catalogConfig = extractCatalogConfig(config); + Catalog catalog = catalogFactory.get().createCatalog(DEFAULT_CATALOG, catalogConfig); + return Optional.of(catalog); + } Review Comment: I think you can directly use `FactoryUtil.createOptionalCatalog` with `JdbcDielect::name` to create catalog . Create a mapping for catalog to jdbc dialect is unnecessary. You can refer https://github.com/apache/seatunnel/blob/dev/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/source/JdbcSourceFactory.java#L104 ########## seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/sink/JdbcSink.java: ########## @@ -195,44 +191,37 @@ public DataSaveMode getUserConfigSaveMode() { @Override public void handleSaveMode(DataSaveMode saveMode) { if (catalogTable != null) { - Map<String, String> catalogOptions = config.get(CatalogOptions.CATALOG_OPTIONS); - if (catalogOptions != null) { - String factoryId = catalogOptions.get(CommonOptions.FACTORY_ID.key()); - if (StringUtils.isBlank(jdbcSinkConfig.getDatabase())) { - return; - } - CatalogFactory catalogFactory = - discoverFactory( - Thread.currentThread().getContextClassLoader(), - CatalogFactory.class, - factoryId); - if (catalogFactory != null) { - try (Catalog catalog = - catalogFactory.createCatalog( - catalogFactory.factoryIdentifier(), - ReadonlyConfig.fromMap(new HashMap<>(catalogOptions)))) { - catalog.open(); - FieldIdeEnum fieldIdeEnumEnum = config.get(JdbcOptions.FIELD_IDE); - String fieldIde = - fieldIdeEnumEnum == null - ? FieldIdeEnum.ORIGINAL.getValue() - : fieldIdeEnumEnum.getValue(); - TablePath tablePath = - TablePath.of( - jdbcSinkConfig.getDatabase() - + "." - + CatalogUtils.quoteTableIdentifier( - jdbcSinkConfig.getTable(), fieldIde)); - if (!catalog.databaseExists(jdbcSinkConfig.getDatabase())) { - catalog.createDatabase(tablePath, true); - } - catalogTable.getOptions().put("fieldIde", fieldIde); - if (!catalog.tableExists(tablePath)) { - catalog.createTable(tablePath, catalogTable, true); - } - } catch (Exception e) { - throw new JdbcConnectorException(HANDLE_SAVE_MODE_FAILED, e); + if (StringUtils.isBlank(jdbcSinkConfig.getDatabase())) { + return; + } + Optional<Catalog> catalogOptional = + JdbcCatalogUtils.findCatalog(jdbcSinkConfig.getJdbcConnectionConfig()); + if (catalogOptional.isPresent()) { + try (Catalog catalog = catalogOptional.get()) { + catalog.open(); + FieldIdeEnum fieldIdeEnumEnum = config.get(JdbcOptions.FIELD_IDE); + String fieldIde = + fieldIdeEnumEnum == null + ? FieldIdeEnum.ORIGINAL.getValue() + : fieldIdeEnumEnum.getValue(); + TablePath tablePath = + TablePath.of( + jdbcSinkConfig.getDatabase() + + "." + + CatalogUtils.quoteTableIdentifier( + jdbcSinkConfig.getTable(), fieldIde)); + if (!catalog.databaseExists(jdbcSinkConfig.getDatabase())) { + catalog.createDatabase(tablePath, true); + } + catalogTable.getOptions().put("fieldIde", fieldIde); + if (!catalog.tableExists(tablePath)) { + catalog.createTable(tablePath, catalogTable, true); } + } catch (UnsupportedOperationException | CatalogException e) { + // TODO Temporary fix, this feature has been changed in this pr + // https://github.com/apache/seatunnel/pull/5645 Review Comment: Should add a warning msg? -- 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]
