-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature][Connector-V2][JDBC] support sqlite Source & Sink #3089
Merged
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
04e2395
refactor JDBC[SqLite] connector
nutsjian cf1ddc4
[Improve] [JDBC] [SQLite] Connector test more field types
nutsjian 2981c1a
add [JDBC] [SQLite] doc
nutsjian 0643e4a
fix [JDBC] [SQLite] e2e test case method name
nutsjian f3de959
[fix] class name error
nutsjian 6f3b792
[Hotfix] [JDBC] [SQLite] connector e2e test path problem
nutsjian e4bf558
[fix] [JDBC] [SQLite] flink e2e test docker container db file cannot …
nutsjian 45701f3
[fix] [JDBC] [SQLite] flink e2e test
nutsjian 7bd5125
Merge branch 'dev' into jdbc-sqlite
nutsjian e587104
[fix] [JDBC] [SQLite] flink e2e test database closed error
nutsjian 049f6c7
Merge branch 'dev' into jdbc-sqlite
nutsjian 803dec9
Merge branch 'dev' into jdbc-sqlite
nutsjian ce5ddf0
[fix] [JDBC] [SQLite] e2e test, rerun workflows commit
nutsjian eb321b0
Merge branch 'dev' into jdbc-sqlite
nutsjian 0118847
update [JDBC] [SQLite] add change log
nutsjian 7ef5bfe
Merge branch 'dev' into jdbc-sqlite
nutsjian d5ed310
update [JDBC] [SQLite] change log doc
nutsjian a861767
update [JDBC] [SQLite] connector map tinyint smallint to SeaTunnel Sh…
nutsjian 8fbaad8
refactor JDBC[SqLite] connector
nutsjian dfd660c
Merge branch 'dev' into jdbc-sqlite
nutsjian 93468b5
update, merge upstream dev, add e2e in flink/spark e2e module so that…
nutsjian 18cf506
update checkstyle
nutsjian 70494ae
Merge branch 'dev' into jdbc-sqlite
nutsjian 03fba9f
[checkstyle] remove unused import
nutsjian 976c5a6
Merge remote-tracking branch 'upstream' into jdbc-sqlite
nutsjian 6197196
Merge branch 'dev' of https://github.com/apache/incubator-seatunnel i…
nutsjian d03bf4d
update [JDBC] [SQLite] connector
nutsjian 5ad4b4c
Merge branch 'dev' into jdbc-sqlite
nutsjian fa00b72
Merge branch 'dev' into jdbc-sqlite
nutsjian 5c0b7e8
Merge branch 'dev' into jdbc-sqlite
nutsjian f4d4d33
fix [JDBC] [SQLite] connector, remove root pom.xml dup net.alchim31.m…
nutsjian 3204610
update [Connector] [JDBC] SQLite, remove typeAffinity option, impleme…
nutsjian File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
39 changes: 39 additions & 0 deletions
39
...org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/sqlite/SqliteDialect.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,39 @@ | ||
/* | ||
* 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.internal.dialect.sqlite; | ||
|
||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.converter.JdbcRowConverter; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialect; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialectTypeMapper; | ||
|
||
public class SqliteDialect implements JdbcDialect { | ||
@Override | ||
public String dialectName() { | ||
return "Sqlite"; | ||
} | ||
|
||
@Override | ||
public JdbcRowConverter getRowConverter() { | ||
return new SqliteJdbcRowConverter(); | ||
} | ||
|
||
@Override | ||
public JdbcDialectTypeMapper getJdbcDialectTypeMapper() { | ||
return new SqliteTypeMapper(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...che/seatunnel/connectors/seatunnel/jdbc/internal/dialect/sqlite/SqliteDialectFactory.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,40 @@ | ||
/* | ||
* 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.internal.dialect.sqlite; | ||
|
||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialect; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialectFactory; | ||
|
||
import com.google.auto.service.AutoService; | ||
|
||
/** | ||
* Factory for {@link SqliteDialect}. | ||
*/ | ||
|
||
@AutoService(JdbcDialectFactory.class) | ||
public class SqliteDialectFactory implements JdbcDialectFactory { | ||
@Override | ||
public boolean acceptsURL(String url) { | ||
return url.startsWith("jdbc:sqlite:"); | ||
} | ||
|
||
@Override | ||
public JdbcDialect create() { | ||
return new SqliteDialect(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...e/seatunnel/connectors/seatunnel/jdbc/internal/dialect/sqlite/SqliteJdbcRowConverter.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,39 @@ | ||
/* | ||
* 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.internal.dialect.sqlite; | ||
|
||
import org.apache.seatunnel.api.table.type.SeaTunnelRow; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelRowType; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.converter.AbstractJdbcRowConverter; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
|
||
public class SqliteJdbcRowConverter extends AbstractJdbcRowConverter { | ||
|
||
@Override | ||
public String converterName() { | ||
return "Sqlite"; | ||
} | ||
|
||
@Override | ||
public SeaTunnelRow toInternal(ResultSet rs, ResultSetMetaData metaData, SeaTunnelRowType typeInfo) throws SQLException { | ||
return super.toInternal(rs, metaData, typeInfo); | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
.../apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/sqlite/SqliteTypeMapper.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,127 @@ | ||
/* | ||
* 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.internal.dialect.sqlite; | ||
|
||
import org.apache.seatunnel.api.table.type.BasicType; | ||
import org.apache.seatunnel.api.table.type.PrimitiveByteArrayType; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelDataType; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialectTypeMapper; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
import java.sql.Types; | ||
|
||
@Slf4j | ||
public class SqliteTypeMapper implements JdbcDialectTypeMapper { | ||
|
||
private static final String OBJECT_CLASS = "java.lang.Object"; | ||
private static final String CHARACTER_CLASS = "java.lang.Character"; | ||
private static final String STRING_CLASS = "java.lang.String"; | ||
private static final String INTEGER_CLASS = "java.lang.Integer"; | ||
private static final String SHORT_CLASS = "java.lang.Short"; | ||
private static final String LONG_CLASS = "java.lang.Long"; | ||
private static final String FLOAT_CLASS = "java.lang.Float"; | ||
private static final String DOUBLE_CLASS = "java.lang.Double"; | ||
private static final String BOOLEAN_CLASS = "java.lang.Boolean"; | ||
private static final String BYTE_CLASS = "java.lang.Byte"; | ||
|
||
/** | ||
* because of sqlite's dynamic data type and affinity, use columnType(java.sql.Types) determine the SeaTunnel Data Types | ||
*/ | ||
@SuppressWarnings("checkstyle:MagicNumber") | ||
@Override | ||
public SeaTunnelDataType<?> mapping(ResultSetMetaData metadata, int colIndex) throws SQLException { | ||
int columnType = metadata.getColumnType(colIndex); | ||
String columnClassName = metadata.getColumnClassName(colIndex); | ||
// sqlite data type is dynamic and type affinity, see https://www.sqlite.org/datatype3.html | ||
// 1. if columnClassName is java.lang.Object, this column has no data, use columnType determine the SeaTunnel Data Types | ||
// 2. otherwise we just use columnClassName to determine the SeaTunnel Data Types | ||
if (columnClassName.equalsIgnoreCase(OBJECT_CLASS)) { // case 1. | ||
switch (columnType) { | ||
case Types.CHAR: | ||
case Types.NCHAR: | ||
case Types.VARCHAR: | ||
case Types.NVARCHAR: | ||
case Types.LONGVARCHAR: | ||
case Types.LONGNVARCHAR: | ||
return BasicType.STRING_TYPE; | ||
case Types.INTEGER: | ||
return BasicType.INT_TYPE; | ||
case Types.TINYINT: | ||
case Types.SMALLINT: | ||
return BasicType.SHORT_TYPE; | ||
case Types.BIGINT: | ||
case Types.DATE: | ||
case Types.TIME: | ||
case Types.TIMESTAMP: | ||
case Types.TIME_WITH_TIMEZONE: | ||
case Types.TIMESTAMP_WITH_TIMEZONE: | ||
return BasicType.LONG_TYPE; | ||
case Types.FLOAT: | ||
return BasicType.FLOAT_TYPE; | ||
case Types.DOUBLE: | ||
case Types.REAL: | ||
case Types.NUMERIC: | ||
case Types.DECIMAL: | ||
return BasicType.DOUBLE_TYPE; | ||
case Types.BOOLEAN: | ||
case Types.BIT: | ||
return BasicType.BOOLEAN_TYPE; | ||
case Types.BLOB: | ||
case Types.BINARY: | ||
case Types.VARBINARY: | ||
case Types.LONGVARBINARY: | ||
return PrimitiveByteArrayType.INSTANCE; | ||
default: | ||
final String jdbcColumnName = metadata.getColumnName(colIndex); | ||
throw new UnsupportedOperationException( | ||
String.format( | ||
"Doesn't support sql type '%s' on column '%s' yet.", | ||
columnType, jdbcColumnName)); | ||
} | ||
} else { // case 2 | ||
switch (columnClassName) { | ||
case INTEGER_CLASS: | ||
return BasicType.INT_TYPE; | ||
case SHORT_CLASS: | ||
return BasicType.SHORT_TYPE; | ||
case LONG_CLASS: | ||
return BasicType.LONG_TYPE; | ||
case FLOAT_CLASS: | ||
return BasicType.FLOAT_TYPE; | ||
case DOUBLE_CLASS: | ||
return BasicType.DOUBLE_TYPE; | ||
case STRING_CLASS: | ||
case CHARACTER_CLASS: | ||
return BasicType.STRING_TYPE; | ||
case BOOLEAN_CLASS: | ||
return BasicType.BOOLEAN_TYPE; | ||
case BYTE_CLASS: | ||
return BasicType.BYTE_TYPE; | ||
default: | ||
final String jdbcColumnName = metadata.getColumnName(colIndex); | ||
throw new UnsupportedOperationException( | ||
String.format( | ||
"Doesn't support sql type '%s' on column '%s' yet.", | ||
columnType, jdbcColumnName)); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ | |
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialect; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialectLoader; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialectTypeMapper; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.sqlite.SqliteDialect; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.state.JdbcSourceState; | ||
|
||
import org.apache.seatunnel.shade.com.typesafe.config.Config; | ||
|
@@ -51,6 +52,7 @@ | |
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
@AutoService(SeaTunnelSource.class) | ||
public class JdbcSource implements SeaTunnelSource<SeaTunnelRow, JdbcSourceSplit, JdbcSourceState> { | ||
|
@@ -137,15 +139,31 @@ private SeaTunnelRowType initTableField(Connection conn) { | |
JdbcDialectTypeMapper jdbcDialectTypeMapper = jdbcDialect.getJdbcDialectTypeMapper(); | ||
ArrayList<SeaTunnelDataType<?>> seaTunnelDataTypes = new ArrayList<>(); | ||
ArrayList<String> fieldNames = new ArrayList<>(); | ||
ResultSet resultSet = null; | ||
try { | ||
PreparedStatement ps = conn.prepareStatement(jdbcSourceOptions.getJdbcConnectionOptions().getQuery()); | ||
ResultSetMetaData resultSetMetaData = ps.getMetaData(); | ||
ResultSetMetaData resultSetMetaData; | ||
if (jdbcSourceOptions.getJdbcConnectionOptions().isTypeAffinity() && jdbcDialect instanceof SqliteDialect) { | ||
PreparedStatement ps = conn.prepareStatement("select t.* from (" + jdbcSourceOptions.getJdbcConnectionOptions().getQuery() + ") as t limit 1, 1"); | ||
resultSet = ps.executeQuery(); | ||
resultSetMetaData = resultSet.getMetaData(); | ||
} else { | ||
PreparedStatement ps = conn.prepareStatement(jdbcSourceOptions.getJdbcConnectionOptions().getQuery()); | ||
resultSetMetaData = ps.getMetaData(); | ||
} | ||
for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) { | ||
fieldNames.add(resultSetMetaData.getColumnName(i)); | ||
seaTunnelDataTypes.add(jdbcDialectTypeMapper.mapping(resultSetMetaData, i)); | ||
} | ||
} catch (Exception e) { | ||
LOG.warn("get row type info exception", e); | ||
} finally { | ||
if (Objects.nonNull(resultSet)) { | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the role of this code? |
||
resultSet.close(); | ||
} catch (SQLException e) { | ||
LOG.warn("get row type info exception", e); | ||
} | ||
} | ||
} | ||
return new SeaTunnelRowType(fieldNames.toArray(new String[fieldNames.size()]), seaTunnelDataTypes.toArray(new SeaTunnelDataType<?>[seaTunnelDataTypes.size()])); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert it.