forked from apache/seatunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature][connector-jdbc]Add Save Mode function and Connector-JDBC (M…
…ySQL) connector has been realized (apache#5663)
- Loading branch information
Showing
34 changed files
with
1,474 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
seatunnel-api/src/main/java/org/apache/seatunnel/api/sink/DefaultSaveModeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* 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.api.sink; | ||
|
||
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.common.exception.SeaTunnelRuntimeException; | ||
|
||
import lombok.AllArgsConstructor; | ||
|
||
import static org.apache.seatunnel.api.common.SeaTunnelAPIErrorCode.SINK_TABLE_NOT_EXIST; | ||
import static org.apache.seatunnel.api.common.SeaTunnelAPIErrorCode.SOURCE_ALREADY_HAS_DATA; | ||
|
||
@AllArgsConstructor | ||
public class DefaultSaveModeHandler implements SaveModeHandler { | ||
|
||
public SchemaSaveMode schemaSaveMode; | ||
public DataSaveMode dataSaveMode; | ||
public Catalog catalog; | ||
public TablePath tablePath; | ||
public CatalogTable catalogTable; | ||
public String customSql; | ||
|
||
public DefaultSaveModeHandler( | ||
SchemaSaveMode schemaSaveMode, | ||
DataSaveMode dataSaveMode, | ||
Catalog catalog, | ||
CatalogTable catalogTable, | ||
String customSql) { | ||
this( | ||
schemaSaveMode, | ||
dataSaveMode, | ||
catalog, | ||
catalogTable.getTableId().toTablePath(), | ||
catalogTable, | ||
customSql); | ||
} | ||
|
||
@Override | ||
public void handleSchemaSaveMode() { | ||
switch (schemaSaveMode) { | ||
case RECREATE_SCHEMA: | ||
recreateSchema(); | ||
break; | ||
case CREATE_SCHEMA_WHEN_NOT_EXIST: | ||
createSchemaWhenNotExist(); | ||
break; | ||
case ERROR_WHEN_SCHEMA_NOT_EXIST: | ||
errorWhenSchemaNotExist(); | ||
break; | ||
default: | ||
throw new UnsupportedOperationException("Unsupported save mode: " + schemaSaveMode); | ||
} | ||
} | ||
|
||
@Override | ||
public void handleDataSaveMode() { | ||
switch (dataSaveMode) { | ||
case DROP_DATA: | ||
keepSchemaDropData(); | ||
break; | ||
case APPEND_DATA: | ||
keepSchemaAndData(); | ||
break; | ||
case CUSTOM_PROCESSING: | ||
customProcessing(); | ||
break; | ||
case ERROR_WHEN_DATA_EXISTS: | ||
errorWhenDataExists(); | ||
break; | ||
default: | ||
throw new UnsupportedOperationException("Unsupported save mode: " + dataSaveMode); | ||
} | ||
} | ||
|
||
protected void recreateSchema() { | ||
if (tableExists()) { | ||
dropTable(); | ||
} | ||
createTable(); | ||
} | ||
|
||
protected void createSchemaWhenNotExist() { | ||
if (!tableExists()) { | ||
createTable(); | ||
} | ||
} | ||
|
||
protected void errorWhenSchemaNotExist() { | ||
if (!tableExists()) { | ||
throw new SeaTunnelRuntimeException(SINK_TABLE_NOT_EXIST, "The sink table not exist"); | ||
} | ||
} | ||
|
||
protected void keepSchemaDropData() { | ||
if (tableExists()) { | ||
truncateTable(); | ||
} | ||
} | ||
|
||
protected void keepSchemaAndData() {} | ||
|
||
protected void customProcessing() { | ||
executeCustomSql(); | ||
} | ||
|
||
protected void errorWhenDataExists() { | ||
if (dataExists()) { | ||
throw new SeaTunnelRuntimeException( | ||
SOURCE_ALREADY_HAS_DATA, "The target data source already has data"); | ||
} | ||
} | ||
|
||
protected boolean tableExists() { | ||
return catalog.tableExists(tablePath); | ||
} | ||
|
||
protected void dropTable() { | ||
catalog.dropTable(tablePath, true); | ||
} | ||
|
||
protected void createTable() { | ||
catalog.createTable(tablePath, catalogTable, true); | ||
} | ||
|
||
protected void truncateTable() { | ||
catalog.truncateTable(tablePath, true); | ||
} | ||
|
||
protected boolean dataExists() { | ||
return catalog.isExistsData(tablePath); | ||
} | ||
|
||
protected void executeCustomSql() { | ||
catalog.executeSql(tablePath, customSql); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
catalog.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
seatunnel-api/src/main/java/org/apache/seatunnel/api/sink/SchemaSaveMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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.api.sink; | ||
|
||
public enum SchemaSaveMode { | ||
|
||
// Will create when the table does not exist, delete and rebuild when the table is saved | ||
RECREATE_SCHEMA, | ||
|
||
// Will Created when the table does not exist, skipped when the table is saved | ||
CREATE_SCHEMA_WHEN_NOT_EXIST, | ||
|
||
// Error will be reported when the table does not exist | ||
ERROR_WHEN_SCHEMA_NOT_EXIST, | ||
} |
27 changes: 27 additions & 0 deletions
27
seatunnel-api/src/main/java/org/apache/seatunnel/api/sink/SinkReplaceNameConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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.api.sink; | ||
|
||
public final class SinkReplaceNameConstant { | ||
|
||
public static final String REPLACE_TABLE_NAME_KEY = "${table_name}"; | ||
|
||
public static final String REPLACE_SCHEMA_NAME_KEY = "${schema_name}"; | ||
|
||
public static final String REPLACE_DATABASE_NAME_KEY = "${database_name}"; | ||
} |
31 changes: 31 additions & 0 deletions
31
seatunnel-api/src/main/java/org/apache/seatunnel/api/sink/SupportSaveMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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.api.sink; | ||
|
||
import java.util.Optional; | ||
|
||
/** The Sink Connectors which support schema and data SaveMode should implement this interface */ | ||
public interface SupportSaveMode { | ||
|
||
String DATA_SAVE_MODE_KEY = "data_save_mode"; | ||
|
||
String SCHEMA_SAVE_MODE_KEY = "schema_save_mode"; | ||
|
||
// This method defines the return of a specific save_mode handler | ||
Optional<SaveModeHandler> getSaveModeHandler(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.