You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "RocMarshal (via GitHub)" <gi...@apache.org> on 2023/11/05 09:26:06 UTC
[PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
RocMarshal opened a new pull request, #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78
# Main change
- [FLINK-33459][Connector/JDBC] Add dependency items about flink-connector-base and testing into the pom.xml
- [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format
- [FLINK-33459][Connector/JDBC] Mark the old JdbcInputFormat as Deprecated.
# Testing
Add some unit test cased and integration test cases.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "Jiabao-Sun (via GitHub)" <gi...@apache.org>.
Jiabao-Sun commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1448844127
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSourceOptions.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.configuration.ConfigOption;
+import org.apache.flink.configuration.ConfigOptions;
+
+import java.io.Serializable;
+import java.sql.ResultSet;
+
+/** JDBC source options. */
+public class JdbcSourceOptions implements Serializable {
+
+ private JdbcSourceOptions() {}
+
+ public static final ConfigOption<Integer> READER_FETCH_BATCH_SIZE =
+ ConfigOptions.key("connectors.jdbc.split-reader.fetch-batch-size")
+ .intType()
+ .defaultValue(1024);
+ public static final ConfigOption<Integer> RESULTSET_TYPE =
+ ConfigOptions.key("connectors.jdbc.split-reader.resultSetType")
+ .intType()
+ .defaultValue(ResultSet.TYPE_FORWARD_ONLY);
+ public static final ConfigOption<Integer> RESULTSET_CONCURRENCY =
+ ConfigOptions.key("connectors.jdbc.split-reader.resultSetConcurrency")
+ .intType()
+ .defaultValue(ResultSet.CONCUR_READ_ONLY);
+ public static final ConfigOption<Integer> RESULTSET_FETCH_SIZE =
+ ConfigOptions.key("connectors.jdbc.split-reader.resultSetFetchSize")
+ .intType()
+ .defaultValue(0);
+ public static final ConfigOption<Boolean> AUTO_COMMIT =
+ ConfigOptions.key("connectors.jdbc.split-reader.autoCommit")
+ .booleanType()
+ .defaultValue(true);
Review Comment:
How about to use lowercase hyphenated form for the configuration name?
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "eskabetxe (via GitHub)" <gi...@apache.org>.
eskabetxe commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1405917676
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/Utils.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.connector.jdbc.source.split.CheckpointedOffset;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplit;
+import org.apache.flink.util.InstantiationUtil;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.Serializable;
+
+/** Utils class to hold common static methods. */
+public class Utils {
+ private Utils() {}
+
+ public static void serializeJdbcSourceSplit(DataOutputStream out, JdbcSourceSplit sourceSplit)
Review Comment:
I would say that this should be on JdbcSourceSplitSerializer.java, and we should instantiate that on JdbcSourceEnumStateSerializer.
This because any change to this code will affect the state and should be versioned, and is more easy to understand if is there.
Other option is to have this on JdbcSourceSplit, but I think that the same problem of versioning will happen.
But we should avoid this kind of utility.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "davidradl (via GitHub)" <gi...@apache.org>.
davidradl commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1397218900
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/JdbcInputFormat.java:
##########
@@ -99,7 +99,11 @@
* @see JdbcParameterValuesProvider
* @see PreparedStatement
* @see DriverManager
+ * @deprecated Please use {@link org.apache.flink.connector.jdbc.source.JdbcSource} instead. The
+ * builder utils and parameters passing could be view {@link
Review Comment:
I am not sure what this sentence means. Is it something to do with jdbc views or relating to the viewing the link
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "eskabetxe (via GitHub)" <gi...@apache.org>.
eskabetxe commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1441475081
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSource.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.typeinfo.TypeHint;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.source.Boundedness;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.api.connector.source.SourceReader;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.api.connector.source.SplitEnumerator;
+import org.apache.flink.api.connector.source.SplitEnumeratorContext;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumStateSerializer;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumerator;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumeratorState;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSqlSplitEnumeratorBase;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceReader;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceSplitReader;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplit;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplitSerializer;
+import org.apache.flink.core.io.SimpleVersionedSerializer;
+import org.apache.flink.util.Preconditions;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Objects;
+
+/** JDBC source. */
+@PublicEvolving
+public class JdbcSource<OUT>
+ implements Source<OUT, JdbcSourceSplit, JdbcSourceEnumeratorState>,
+ ResultTypeQueryable<OUT> {
+
+ private final Boundedness boundedness;
+ private final TypeInformation<OUT> typeInformation;
+
+ private final Configuration configuration;
+ private final JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider;
+
+ protected JdbcConnectionProvider connectionProvider;
+ private final ResultExtractor<OUT> resultExtractor;
+ private final DeliveryGuarantee deliveryGuarantee;
+
+ JdbcSource(
+ Configuration configuration,
+ JdbcConnectionProvider connectionProvider,
+ JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider,
+ ResultExtractor<OUT> resultExtractor,
+ @Nullable TypeInformation<OUT> typeInformation,
+ @Nullable DeliveryGuarantee deliveryGuarantee) {
Review Comment:
The second one (without DeliveryGuarantee) could call the first one, with the DeliveryGuarantee.NONE.
this way we could remove the @Nullable, and not duplicate the constructor logic
```
JdbcSource(
Configuration configuration,
JdbcConnectionProvider connectionProvider,
JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider,
ResultExtractor<OUT> resultExtractor,
TypeInformation<OUT> typeInformation) {
this(configuration,
connectionProvider,
sqlSplitEnumeratorProvider,
resultExtractor,
typeInformation,
DeliveryGuarantee.NONE);
}
JdbcSource(
Configuration configuration,
JdbcConnectionProvider connectionProvider,
JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider,
ResultExtractor<OUT> resultExtractor,
TypeInformation<OUT> typeInformation,
DeliveryGuarantee deliveryGuarantee) {
this.configuration = Preconditions.checkNotNull(configuration);
this.connectionProvider = Preconditions.checkNotNull(connectionProvider);
this.sqlSplitEnumeratorProvider = Preconditions.checkNotNull(sqlSplitEnumeratorProvider);
this.resultExtractor = Preconditions.checkNotNull(resultExtractor);
this.deliveryGuarantee = Preconditions.checkNotNull(configuration);
this.typeInformation = Preconditions.checkNotNull(deliveryGuarantee);
this.boundedness = Boundedness.BOUNDED;
}
```
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#issuecomment-1876318089
Hi, @eskabetxe Thank you very much for the review. And I left some discussion items for your comments.
Please let me know what's your opinion~ :)
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#issuecomment-1793683582
Thanks for opening this pull request! Please check out our contributing guidelines. (https://flink.apache.org/contributing/how-to-contribute.html)
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin merged PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#issuecomment-2042297264
Thank you @eskabetxe @caicancai very much for the review.
CC @snuyanzin
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#issuecomment-2005697292
Hi, @snuyanzin would you mind helping take a look ? thank you very much~
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1441484531
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSource.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.typeinfo.TypeHint;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.source.Boundedness;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.api.connector.source.SourceReader;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.api.connector.source.SplitEnumerator;
+import org.apache.flink.api.connector.source.SplitEnumeratorContext;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumStateSerializer;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumerator;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumeratorState;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSqlSplitEnumeratorBase;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceReader;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceSplitReader;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplit;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplitSerializer;
+import org.apache.flink.core.io.SimpleVersionedSerializer;
+import org.apache.flink.util.Preconditions;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Objects;
+
+/** JDBC source. */
+@PublicEvolving
+public class JdbcSource<OUT>
+ implements Source<OUT, JdbcSourceSplit, JdbcSourceEnumeratorState>,
+ ResultTypeQueryable<OUT> {
+
+ private final Boundedness boundedness;
+ private final TypeInformation<OUT> typeInformation;
+
+ private final Configuration configuration;
+ private final JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider;
+
+ protected JdbcConnectionProvider connectionProvider;
+ private final ResultExtractor<OUT> resultExtractor;
+ private final DeliveryGuarantee deliveryGuarantee;
+
+ JdbcSource(
+ Configuration configuration,
+ JdbcConnectionProvider connectionProvider,
+ JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider,
+ ResultExtractor<OUT> resultExtractor,
+ @Nullable TypeInformation<OUT> typeInformation,
+ @Nullable DeliveryGuarantee deliveryGuarantee) {
Review Comment:
Good proposal! 👍
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1397395729
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/JdbcInputFormat.java:
##########
@@ -99,7 +99,11 @@
* @see JdbcParameterValuesProvider
* @see PreparedStatement
* @see DriverManager
+ * @deprecated Please use {@link org.apache.flink.connector.jdbc.source.JdbcSource} instead. The
+ * builder utils and parameters passing could be view {@link
Review Comment:
hi, @davidradl
Thank you very much for your comment.
Your concerns are reasonable.
Firstly, there are two questions:
1. There may be syntax issues with the expression. I just want to declare here that this method has expired and requires the use of a new API for development, with the new class declared here. I'll update the sentence.
2. A comprehensive document is indeed needed here to guide developers.
As https://issues.apache.org/jira/browse/FLINK-25420 mentioned in, there are currently no plans to introduce relevant documents.
Of course, if you are willing to write usage documentation for this new API, we would greatly appreciate it.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1441299767
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSourceBuilder.java:
##########
@@ -0,0 +1,206 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.enumerator.SqlTemplateSplitEnumerator;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.split.JdbcParameterValuesProvider;
+import org.apache.flink.util.Preconditions;
+import org.apache.flink.util.StringUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+
+import java.sql.ResultSet;
+
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.AUTO_COMMIT;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.READER_FETCH_BATCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_CONCURRENCY;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_FETCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_TYPE;
+
+/** A tool is used to build {@link JdbcSource} quickly. */
+@PublicEvolving
+public class JdbcSourceBuilder<OUT> {
+
+ public static final Logger LOG = LoggerFactory.getLogger(JdbcSourceBuilder.class);
+
+ private final Configuration configuration;
+
+ private int splitReaderFetchBatchSize;
+ private int resultSetType;
+ private int resultSetConcurrency;
+ private int resultSetFetchSize;
+ // Boolean to distinguish between default value and explicitly set autoCommit mode.
+ private Boolean autoCommit;
+
+ // TODO It would need a builder method to render after introducing streaming semantic.
+ private DeliveryGuarantee deliveryGuarantee;
+
+ private TypeInformation<OUT> typeInformation;
+
+ private final JdbcConnectionOptions.JdbcConnectionOptionsBuilder connOptionsBuilder;
+ private String sql;
+ private JdbcParameterValuesProvider jdbcParameterValuesProvider;
+ private ResultExtractor<OUT> resultExtractor;
+
+ private JdbcConnectionProvider connectionProvider;
+
+ JdbcSourceBuilder() {
+ this.configuration = new Configuration();
+ this.connOptionsBuilder = new JdbcConnectionOptions.JdbcConnectionOptionsBuilder();
+ this.splitReaderFetchBatchSize = 1024;
+ this.resultSetType = ResultSet.TYPE_FORWARD_ONLY;
+ this.resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
+ this.deliveryGuarantee = DeliveryGuarantee.NONE;
+ // Boolean to distinguish between default value and explicitly set autoCommit mode.
+ this.autoCommit = true;
+ }
+
+ public JdbcSourceBuilder<OUT> setSql(@Nonnull String sql) {
+ Preconditions.checkArgument(
+ !StringUtils.isNullOrWhitespaceOnly(sql), "It's required to set the sql.");
+ this.sql = sql;
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setResultExtractor(ResultExtractor<OUT> resultExtractor) {
+ this.resultExtractor =
+ Preconditions.checkNotNull(resultExtractor, "resultExtractor must not be null.");
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setUsername(String username) {
+ Preconditions.checkArgument(
+ !StringUtils.isNullOrWhitespaceOnly(username),
+ "It's required to set the 'username'.");
+ connOptionsBuilder.withUsername(username);
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setPassword(String password) {
+ connOptionsBuilder.withPassword(password);
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setDriverName(String driverName) {
+ Preconditions.checkArgument(
+ !StringUtils.isNullOrWhitespaceOnly(driverName),
+ "It's required to set the 'driverName'.");
+ connOptionsBuilder.withDriverName(driverName);
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setDBUrl(String dbURL) {
+ Preconditions.checkArgument(
+ !StringUtils.isNullOrWhitespaceOnly(dbURL), "It's required to set the 'dbURL'.");
+ connOptionsBuilder.withUrl(dbURL);
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setTypeInformation(
+ @Nonnull TypeInformation<OUT> typeInformation) {
+ this.typeInformation = Preconditions.checkNotNull(typeInformation);
+ return this;
+ }
+
+ // ------ Optional ------------------------------------------------------------------
+
+ public JdbcSourceBuilder<OUT> setJdbcParameterValuesProvider(
+ @Nonnull JdbcParameterValuesProvider parameterValuesProvider) {
+ this.jdbcParameterValuesProvider = Preconditions.checkNotNull(parameterValuesProvider);
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setSplitReaderFetchBatchSize(int splitReaderFetchBatchSize) {
+ Preconditions.checkArgument(
+ splitReaderFetchBatchSize > 0,
+ "'splitReaderFetchBatchSize' must be in range (0, %s]",
+ Integer.MAX_VALUE);
+ this.splitReaderFetchBatchSize = splitReaderFetchBatchSize;
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setResultSetType(int resultSetType) {
+ this.resultSetType = resultSetType;
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setResultSetConcurrency(int resultSetConcurrency) {
+ this.resultSetConcurrency = resultSetConcurrency;
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setAutoCommit(boolean autoCommit) {
+ this.autoCommit = autoCommit;
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setResultSetFetchSize(int resultSetFetchSize) {
+ Preconditions.checkArgument(
+ resultSetFetchSize == Integer.MIN_VALUE || resultSetFetchSize > 0,
+ "Illegal value %s for fetchSize, has to be positive or Integer.MIN_VALUE.",
+ resultSetFetchSize);
+ this.resultSetFetchSize = resultSetFetchSize;
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setConnectionProvider(
+ @Nonnull JdbcConnectionProvider connectionProvider) {
+ this.connectionProvider = Preconditions.checkNotNull(connectionProvider);
+ return this;
+ }
+
+ public JdbcSource<OUT> build() {
+ this.connectionProvider = new SimpleJdbcConnectionProvider(connOptionsBuilder.build());
+ if (resultSetFetchSize > 0) {
+ this.configuration.set(RESULTSET_FETCH_SIZE, resultSetFetchSize);
+ }
+ this.configuration.set(RESULTSET_CONCURRENCY, resultSetConcurrency);
+ this.configuration.set(RESULTSET_TYPE, resultSetType);
+ this.configuration.set(READER_FETCH_BATCH_SIZE, splitReaderFetchBatchSize);
+ this.configuration.set(AUTO_COMMIT, autoCommit);
+
+ if (StringUtils.isNullOrWhitespaceOnly(sql)) {
Review Comment:
thx for the catching. And I'll add the test case like the followed for validating the line.
```
assertThatThrownBy(() -> JdbcSource.builder().setDBUrl(dbUrl).build())
.isInstanceOf(IllegalStateException.class);
```
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "eskabetxe (via GitHub)" <gi...@apache.org>.
eskabetxe commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1441475081
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSource.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.typeinfo.TypeHint;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.source.Boundedness;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.api.connector.source.SourceReader;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.api.connector.source.SplitEnumerator;
+import org.apache.flink.api.connector.source.SplitEnumeratorContext;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumStateSerializer;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumerator;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumeratorState;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSqlSplitEnumeratorBase;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceReader;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceSplitReader;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplit;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplitSerializer;
+import org.apache.flink.core.io.SimpleVersionedSerializer;
+import org.apache.flink.util.Preconditions;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Objects;
+
+/** JDBC source. */
+@PublicEvolving
+public class JdbcSource<OUT>
+ implements Source<OUT, JdbcSourceSplit, JdbcSourceEnumeratorState>,
+ ResultTypeQueryable<OUT> {
+
+ private final Boundedness boundedness;
+ private final TypeInformation<OUT> typeInformation;
+
+ private final Configuration configuration;
+ private final JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider;
+
+ protected JdbcConnectionProvider connectionProvider;
+ private final ResultExtractor<OUT> resultExtractor;
+ private final DeliveryGuarantee deliveryGuarantee;
+
+ JdbcSource(
+ Configuration configuration,
+ JdbcConnectionProvider connectionProvider,
+ JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider,
+ ResultExtractor<OUT> resultExtractor,
+ @Nullable TypeInformation<OUT> typeInformation,
+ @Nullable DeliveryGuarantee deliveryGuarantee) {
Review Comment:
The second one (without DeliveryGuarantee) could call the first one, with the DeliveryGuarantee.NONE.
this way we could remove the @Nullable, and not duplicate the constructor logic
`
JdbcSource(
Configuration configuration,
JdbcConnectionProvider connectionProvider,
JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider,
ResultExtractor<OUT> resultExtractor,
@Nonnull TypeInformation<OUT> typeInformation) {
this(configuration,
connectionProvider,
sqlSplitEnumeratorProvider,
resultExtractor,
typeInformation,
DeliveryGuarantee.NONE);
}
JdbcSource(
Configuration configuration,
JdbcConnectionProvider connectionProvider,
JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider,
ResultExtractor<OUT> resultExtractor,
@Nonnull TypeInformation<OUT> typeInformation,
DeliveryGuarantee deliveryGuarantee) {
this.configuration = Preconditions.checkNotNull(configuration);
this.connectionProvider = Preconditions.checkNotNull(connectionProvider);
this.sqlSplitEnumeratorProvider = Preconditions.checkNotNull(sqlSplitEnumeratorProvider);
this.resultExtractor = Preconditions.checkNotNull(resultExtractor);
this.deliveryGuarantee = Preconditions.checkNotNull(configuration);
this.typeInformation = Preconditions.checkNotNull(deliveryGuarantee);
this.boundedness = Boundedness.BOUNDED;
}
`
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSource.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.typeinfo.TypeHint;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.source.Boundedness;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.api.connector.source.SourceReader;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.api.connector.source.SplitEnumerator;
+import org.apache.flink.api.connector.source.SplitEnumeratorContext;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumStateSerializer;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumerator;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumeratorState;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSqlSplitEnumeratorBase;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceReader;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceSplitReader;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplit;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplitSerializer;
+import org.apache.flink.core.io.SimpleVersionedSerializer;
+import org.apache.flink.util.Preconditions;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Objects;
+
+/** JDBC source. */
+@PublicEvolving
+public class JdbcSource<OUT>
+ implements Source<OUT, JdbcSourceSplit, JdbcSourceEnumeratorState>,
+ ResultTypeQueryable<OUT> {
+
+ private final Boundedness boundedness;
+ private final TypeInformation<OUT> typeInformation;
+
+ private final Configuration configuration;
+ private final JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider;
+
+ protected JdbcConnectionProvider connectionProvider;
+ private final ResultExtractor<OUT> resultExtractor;
+ private final DeliveryGuarantee deliveryGuarantee;
+
+ JdbcSource(
+ Configuration configuration,
+ JdbcConnectionProvider connectionProvider,
+ JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider,
+ ResultExtractor<OUT> resultExtractor,
+ @Nullable TypeInformation<OUT> typeInformation,
+ @Nullable DeliveryGuarantee deliveryGuarantee) {
Review Comment:
The second one (without DeliveryGuarantee) could call the first one, with the DeliveryGuarantee.NONE.
this way we could remove the @Nullable, and not duplicate the constructor logic
```
JdbcSource(
Configuration configuration,
JdbcConnectionProvider connectionProvider,
JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider,
ResultExtractor<OUT> resultExtractor,
@Nonnull TypeInformation<OUT> typeInformation) {
this(configuration,
connectionProvider,
sqlSplitEnumeratorProvider,
resultExtractor,
typeInformation,
DeliveryGuarantee.NONE);
}
JdbcSource(
Configuration configuration,
JdbcConnectionProvider connectionProvider,
JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider,
ResultExtractor<OUT> resultExtractor,
@Nonnull TypeInformation<OUT> typeInformation,
DeliveryGuarantee deliveryGuarantee) {
this.configuration = Preconditions.checkNotNull(configuration);
this.connectionProvider = Preconditions.checkNotNull(connectionProvider);
this.sqlSplitEnumeratorProvider = Preconditions.checkNotNull(sqlSplitEnumeratorProvider);
this.resultExtractor = Preconditions.checkNotNull(resultExtractor);
this.deliveryGuarantee = Preconditions.checkNotNull(configuration);
this.typeInformation = Preconditions.checkNotNull(deliveryGuarantee);
this.boundedness = Boundedness.BOUNDED;
}
```
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#issuecomment-2042575379
Awesome work, congrats on your first merged pull request!
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1441300360
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/reader/extractor/ResultExtractor.java:
##########
@@ -0,0 +1,57 @@
+/*
+ * 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.flink.connector.jdbc.source.reader.extractor;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.types.Row;
+
+import java.io.Serializable;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * The Extractor to extract the data from {@link ResultSet}.
+ *
+ * @param <T> The target data type.
+ */
+@PublicEvolving
+public interface ResultExtractor<T> extends Serializable {
+
+ /**
+ * Extract the data from the current point line of the result.
+ *
+ * @param resultSet Result set queried from a sql.
+ * @return The data object filled by the current line of the resultSet.
+ * @throws SQLException SQL exception.
+ */
+ T extract(ResultSet resultSet) throws SQLException;
+
+ /**
+ * The identifier of the extractor.
+ *
+ * @return identifier in {@link String} type.
+ */
+ default String identifier() {
Review Comment:
What do you think if we change the default implementation to this way
```
default String identifier() {
return this.getClass().getSimpleName();
}
```
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1535498978
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSourceBuilder.java:
##########
@@ -0,0 +1,253 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.enumerator.SqlTemplateSplitEnumerator;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.split.JdbcParameterValuesProvider;
+import org.apache.flink.types.Row;
+import org.apache.flink.util.Preconditions;
+import org.apache.flink.util.StringUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.AUTO_COMMIT;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.READER_FETCH_BATCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_CONCURRENCY;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_FETCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_TYPE;
+
+/**
+ * A tool is used to build {@link JdbcSource} quickly.
+ *
+ * <pre><code>
+ * JdbcSource<Row> source = JdbcSource.<Row>builder()
+ * .setSql(validSql)
+ * .setResultExtractor(new RowResultExtractor())
+ * .setDBUrl(dbUrl)
+ * .setDriverName(driverName)
+ * .setTypeInformation(new TypeHint<Row>() {}.getTypeInfo())
+ * .setPassword(password)
+ * .setUsername(username)
+ * .build();
+ * </code></pre>
+ *
+ * <p>In order to query the JDBC source in parallel, you need to provide a parameterized query
+ * template (i.e. a valid {@link PreparedStatement}) and a {@link JdbcParameterValuesProvider} which
+ * provides binding values for the query parameters. E.g.:
+ *
+ * <pre><code>
+ *
+ * Serializable[][] queryParameters = new String[2][1];
+ * queryParameters[0] = new String[]{"Kumar"};
+ * queryParameters[1] = new String[]{"Tan Ah Teck"};
+ *
+ * JdbcSource<Row> jdbcSource = JdbcSource.<Row>builder()
+ * .setResultExtractor(new RowResultExtractor())
+ * .setTypeInformation(new TypeHint<Row>() {}.getTypeInfo())
+ * .setPassword(password)
+ * .setUsername(username)
+ * .setDriverName("org.apache.derby.jdbc.EmbeddedDriver")
+ * .setDBUrl("jdbc:derby:memory:ebookshop")
+ * .setSql("select * from books WHERE author = ?")
+ * .setJdbcParameterValuesProvider(new JdbcGenericParameterValuesProvider(queryParameters))
+ * .build();
+ * </code></pre>
+ *
+ * @see Row
+ * @see JdbcParameterValuesProvider
+ * @see PreparedStatement
+ * @see DriverManager
+ * @see JdbcSource
+ */
+@PublicEvolving
+public class JdbcSourceBuilder<OUT> {
+
+ public static final Logger LOG = LoggerFactory.getLogger(JdbcSourceBuilder.class);
+
+ private final Configuration configuration;
+
+ private int splitReaderFetchBatchSize;
+ private int resultSetType;
+ private int resultSetConcurrency;
+ private int resultSetFetchSize;
+ // Boolean to distinguish between default value and explicitly set autoCommit mode.
+ private Boolean autoCommit;
+
+ // TODO It would need a builder method to render after introducing streaming semantic.
Review Comment:
should we fix this TODO first?
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "davidradl (via GitHub)" <gi...@apache.org>.
davidradl commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1397421543
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSource.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.typeinfo.TypeHint;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.source.Boundedness;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.api.connector.source.SourceReader;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.api.connector.source.SplitEnumerator;
+import org.apache.flink.api.connector.source.SplitEnumeratorContext;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumStateSerializer;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumerator;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumeratorState;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSqlSplitEnumeratorBase;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceReader;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceSplitReader;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplit;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplitSerializer;
+import org.apache.flink.core.io.SimpleVersionedSerializer;
+import org.apache.flink.util.Preconditions;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Objects;
+
+/** JDBC source. */
+@Internal
Review Comment:
I wonder why this is not tagged with @experimental as the JdbcSourceBuild is experimental. Also JdbcSourceBuild is tagged @internal. I suspect they both should be tagged consistently
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "davidradl (via GitHub)" <gi...@apache.org>.
davidradl commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1397421543
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSource.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.typeinfo.TypeHint;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.source.Boundedness;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.api.connector.source.SourceReader;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.api.connector.source.SplitEnumerator;
+import org.apache.flink.api.connector.source.SplitEnumeratorContext;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumStateSerializer;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumerator;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumeratorState;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSqlSplitEnumeratorBase;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceReader;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceSplitReader;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplit;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplitSerializer;
+import org.apache.flink.core.io.SimpleVersionedSerializer;
+import org.apache.flink.util.Preconditions;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Objects;
+
+/** JDBC source. */
+@Internal
Review Comment:
I wonder why this is not tagged with @experimental as the JdbcSourceBuild is experimental
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#issuecomment-1870955620
Hi, @Jiabao-Sun Would you mind helping take a look on this pr ?
thank you~
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "eskabetxe (via GitHub)" <gi...@apache.org>.
eskabetxe commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1440197286
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSourceBuilder.java:
##########
@@ -0,0 +1,206 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.enumerator.SqlTemplateSplitEnumerator;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.split.JdbcParameterValuesProvider;
+import org.apache.flink.util.Preconditions;
+import org.apache.flink.util.StringUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+
+import java.sql.ResultSet;
+
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.AUTO_COMMIT;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.READER_FETCH_BATCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_CONCURRENCY;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_FETCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_TYPE;
+
+/** A tool is used to build {@link JdbcSource} quickly. */
+@PublicEvolving
+public class JdbcSourceBuilder<OUT> {
+
+ public static final Logger LOG = LoggerFactory.getLogger(JdbcSourceBuilder.class);
+
+ private final Configuration configuration;
+
+ private int splitReaderFetchBatchSize;
+ private int resultSetType;
+ private int resultSetConcurrency;
+ private int resultSetFetchSize;
+ // Boolean to distinguish between default value and explicitly set autoCommit mode.
+ private Boolean autoCommit;
+
+ // TODO It would need a builder method to render after introducing streaming semantic.
+ private DeliveryGuarantee deliveryGuarantee;
+
+ private TypeInformation<OUT> typeInformation;
+
+ private final JdbcConnectionOptions.JdbcConnectionOptionsBuilder connOptionsBuilder;
+ private String sql;
+ private JdbcParameterValuesProvider jdbcParameterValuesProvider;
+ private ResultExtractor<OUT> resultExtractor;
+
+ private JdbcConnectionProvider connectionProvider;
+
+ JdbcSourceBuilder() {
+ this.configuration = new Configuration();
+ this.connOptionsBuilder = new JdbcConnectionOptions.JdbcConnectionOptionsBuilder();
+ this.splitReaderFetchBatchSize = 1024;
+ this.resultSetType = ResultSet.TYPE_FORWARD_ONLY;
+ this.resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
+ this.deliveryGuarantee = DeliveryGuarantee.NONE;
+ // Boolean to distinguish between default value and explicitly set autoCommit mode.
+ this.autoCommit = true;
+ }
+
+ public JdbcSourceBuilder<OUT> setSql(@Nonnull String sql) {
+ Preconditions.checkArgument(
+ !StringUtils.isNullOrWhitespaceOnly(sql), "It's required to set the sql.");
+ this.sql = sql;
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setResultExtractor(ResultExtractor<OUT> resultExtractor) {
+ this.resultExtractor =
+ Preconditions.checkNotNull(resultExtractor, "resultExtractor must not be null.");
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setUsername(String username) {
+ Preconditions.checkArgument(
+ !StringUtils.isNullOrWhitespaceOnly(username),
+ "It's required to set the 'username'.");
+ connOptionsBuilder.withUsername(username);
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setPassword(String password) {
+ connOptionsBuilder.withPassword(password);
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setDriverName(String driverName) {
+ Preconditions.checkArgument(
+ !StringUtils.isNullOrWhitespaceOnly(driverName),
+ "It's required to set the 'driverName'.");
+ connOptionsBuilder.withDriverName(driverName);
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setDBUrl(String dbURL) {
+ Preconditions.checkArgument(
+ !StringUtils.isNullOrWhitespaceOnly(dbURL), "It's required to set the 'dbURL'.");
+ connOptionsBuilder.withUrl(dbURL);
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setTypeInformation(
+ @Nonnull TypeInformation<OUT> typeInformation) {
+ this.typeInformation = Preconditions.checkNotNull(typeInformation);
+ return this;
+ }
+
+ // ------ Optional ------------------------------------------------------------------
+
+ public JdbcSourceBuilder<OUT> setJdbcParameterValuesProvider(
+ @Nonnull JdbcParameterValuesProvider parameterValuesProvider) {
+ this.jdbcParameterValuesProvider = Preconditions.checkNotNull(parameterValuesProvider);
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setSplitReaderFetchBatchSize(int splitReaderFetchBatchSize) {
+ Preconditions.checkArgument(
+ splitReaderFetchBatchSize > 0,
+ "'splitReaderFetchBatchSize' must be in range (0, %s]",
+ Integer.MAX_VALUE);
+ this.splitReaderFetchBatchSize = splitReaderFetchBatchSize;
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setResultSetType(int resultSetType) {
+ this.resultSetType = resultSetType;
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setResultSetConcurrency(int resultSetConcurrency) {
+ this.resultSetConcurrency = resultSetConcurrency;
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setAutoCommit(boolean autoCommit) {
+ this.autoCommit = autoCommit;
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setResultSetFetchSize(int resultSetFetchSize) {
+ Preconditions.checkArgument(
+ resultSetFetchSize == Integer.MIN_VALUE || resultSetFetchSize > 0,
+ "Illegal value %s for fetchSize, has to be positive or Integer.MIN_VALUE.",
+ resultSetFetchSize);
+ this.resultSetFetchSize = resultSetFetchSize;
+ return this;
+ }
+
+ public JdbcSourceBuilder<OUT> setConnectionProvider(
+ @Nonnull JdbcConnectionProvider connectionProvider) {
+ this.connectionProvider = Preconditions.checkNotNull(connectionProvider);
+ return this;
+ }
+
+ public JdbcSource<OUT> build() {
+ this.connectionProvider = new SimpleJdbcConnectionProvider(connOptionsBuilder.build());
+ if (resultSetFetchSize > 0) {
+ this.configuration.set(RESULTSET_FETCH_SIZE, resultSetFetchSize);
+ }
+ this.configuration.set(RESULTSET_CONCURRENCY, resultSetConcurrency);
+ this.configuration.set(RESULTSET_TYPE, resultSetType);
+ this.configuration.set(READER_FETCH_BATCH_SIZE, splitReaderFetchBatchSize);
+ this.configuration.set(AUTO_COMMIT, autoCommit);
+
+ if (StringUtils.isNullOrWhitespaceOnly(sql)) {
Review Comment:
We are checking that sql is not null, but this is not used?
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/reader/extractor/ResultExtractor.java:
##########
@@ -0,0 +1,57 @@
+/*
+ * 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.flink.connector.jdbc.source.reader.extractor;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.types.Row;
+
+import java.io.Serializable;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * The Extractor to extract the data from {@link ResultSet}.
+ *
+ * @param <T> The target data type.
+ */
+@PublicEvolving
+public interface ResultExtractor<T> extends Serializable {
+
+ /**
+ * Extract the data from the current point line of the result.
+ *
+ * @param resultSet Result set queried from a sql.
+ * @return The data object filled by the current line of the resultSet.
+ * @throws SQLException SQL exception.
+ */
+ T extract(ResultSet resultSet) throws SQLException;
+
+ /**
+ * The identifier of the extractor.
+ *
+ * @return identifier in {@link String} type.
+ */
+ default String identifier() {
Review Comment:
do we need a "default" ?
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/reader/JdbcSourceSplitReader.java:
##########
@@ -0,0 +1,291 @@
+/*
+ * 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.flink.connector.jdbc.source.reader;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.base.source.reader.RecordsBySplits;
+import org.apache.flink.connector.base.source.reader.RecordsWithSplitIds;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitReader;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitsChange;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplit;
+import org.apache.flink.util.Preconditions;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayDeque;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Queue;
+
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.AUTO_COMMIT;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.READER_FETCH_BATCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_CONCURRENCY;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_FETCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_TYPE;
+
+/**
+ * The JDBC source reader to read data from jdbc splits.
+ *
+ * @param <T> The type of the record read from the source.
+ */
+public class JdbcSourceSplitReader<T>
+ implements SplitReader<RecordAndOffset<T>, JdbcSourceSplit>, ResultTypeQueryable<T> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(JdbcSourceSplitReader.class);
+
+ private final Configuration config;
+ @Nullable private JdbcSourceSplit currentSplit;
+ private final Queue<JdbcSourceSplit> splits;
+ private final TypeInformation<T> typeInformation;
+ private final JdbcConnectionProvider connectionProvider;
+ private transient Connection connection;
+ private transient PreparedStatement statement;
+ private transient ResultSet resultSet;
+
+ private final ResultExtractor<T> resultExtractor;
+ protected boolean hasNextRecordCurrentSplit;
+ private final DeliveryGuarantee deliveryGuarantee;
+
+ private final int splitReaderFetchBatchSize;
+
+ private final int resultSetType;
+ private final int resultSetConcurrency;
+ private final int resultSetFetchSize;
+ // Boolean to distinguish between default value and explicitly set autoCommit mode.
+ private final Boolean autoCommit;
+ private int currentSplitOffset;
+
+ private final SourceReaderContext context;
+
+ public JdbcSourceSplitReader(
+ SourceReaderContext context,
+ Configuration config,
+ TypeInformation<T> typeInformation,
+ JdbcConnectionProvider connectionProvider,
+ DeliveryGuarantee deliveryGuarantee,
+ ResultExtractor<T> resultExtractor) {
+ this.context = Preconditions.checkNotNull(context);
+ this.config = Preconditions.checkNotNull(config);
+ this.typeInformation = Preconditions.checkNotNull(typeInformation);
+ this.connectionProvider = Preconditions.checkNotNull(connectionProvider);
+ this.resultSetType = config.getInteger(RESULTSET_TYPE);
+ this.resultSetConcurrency = config.getInteger(RESULTSET_CONCURRENCY);
+ this.resultSetFetchSize = config.getInteger(RESULTSET_FETCH_SIZE);
+ this.autoCommit = config.getBoolean(AUTO_COMMIT);
+ this.deliveryGuarantee = Preconditions.checkNotNull(deliveryGuarantee);
+ this.splits = new ArrayDeque<>();
+ this.hasNextRecordCurrentSplit = false;
+ this.currentSplit = null;
+ int splitReaderFetchBatchSize = config.getInteger(READER_FETCH_BATCH_SIZE);
+ Preconditions.checkArgument(
+ splitReaderFetchBatchSize > 0 && splitReaderFetchBatchSize < Integer.MAX_VALUE);
+ this.splitReaderFetchBatchSize = splitReaderFetchBatchSize;
+ this.resultExtractor = Preconditions.checkNotNull(resultExtractor);
+ this.currentSplitOffset = 0;
+ }
+
+ @Override
+ public RecordsWithSplitIds<RecordAndOffset<T>> fetch() throws IOException {
+
+ checkSplitOrStartNext();
+
+ if (!hasNextRecordCurrentSplit) {
+ return finishSplit();
+ }
+
+ RecordsBySplits.Builder<RecordAndOffset<T>> recordAndOffsetBuilder =
+ new RecordsBySplits.Builder<>();
+ Preconditions.checkState(currentSplit != null, "currentSplit");
+ int batch = this.splitReaderFetchBatchSize;
+ while (batch > 0 && hasNextRecordCurrentSplit) {
+ try {
+ T ele = resultExtractor.extract(resultSet);
+ recordAndOffsetBuilder.add(
+ currentSplit, new RecordAndOffset<>(ele, ++currentSplitOffset, 0));
+ batch--;
+ hasNextRecordCurrentSplit = resultSet.next();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ if (!hasNextRecordCurrentSplit) {
+ currentSplitOffset = 0;
+ recordAndOffsetBuilder.addFinishedSplit(currentSplit.splitId());
+ closeResultSetAndStatement();
+ }
+ return recordAndOffsetBuilder.build();
+ }
+
+ private RecordsWithSplitIds<RecordAndOffset<T>> finishSplit() {
+
+ closeResultSetAndStatement();
+
+ RecordsBySplits.Builder<RecordAndOffset<T>> builder = new RecordsBySplits.Builder<>();
+ Preconditions.checkState(currentSplit != null, "currentSplit");
+ builder.addFinishedSplit(currentSplit.splitId());
+ currentSplit = null;
+ return builder.build();
+ }
+
+ private void closeResultSetAndStatement() {
+ try {
+ if (resultSet != null && !resultSet.isClosed()) {
+ resultSet.close();
+ }
+ if (statement != null && !statement.isClosed()) {
+ statement.close();
+ }
+ resultSet = null;
+ statement = null;
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public void handleSplitsChanges(SplitsChange<JdbcSourceSplit> splitsChanges) {
+ if (!(splitsChanges instanceof SplitsAddition)) {
+ throw new UnsupportedOperationException(
+ String.format(
+ "The SplitChange type of %s is not supported.",
+ splitsChanges.getClass()));
+ }
+
+ LOG.debug("Handling split change {}", splitsChanges);
+ splits.addAll(splitsChanges.splits());
+ }
+
+ @Override
+ public void wakeUp() {}
+
+ @Override
+ public void close() throws Exception {
+ closeResultSetAndStatement();
+ if (connection != null && !connection.isClosed()) {
+ connection.close();
+ }
+ connection = null;
+ currentSplit = null;
+ }
+
+ @Override
+ public TypeInformation<T> getProducedType() {
+ return typeInformation;
+ }
+
+ private void checkSplitOrStartNext() {
+
+ try {
+ if (hasNextRecordCurrentSplit && resultSet != null) {
+ return;
+ }
+
+ final JdbcSourceSplit nextSplit = splits.poll();
+ if (nextSplit == null) {
+ throw new IOException("Cannot fetch from another split - no split remaining");
+ }
+ currentSplit = nextSplit;
+ openResultSetForSplit(currentSplit);
+ } catch (SQLException | ClassNotFoundException | IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private void discardSplit(JdbcSourceSplit split) throws SQLException {
+ if (split.getOffset() != 0) {
+ hasNextRecordCurrentSplit = false;
+ currentSplitOffset = 0;
+ if (resultSet != null && !resultSet.isClosed()) {
+ resultSet.close();
+ }
+ if (statement != null && !statement.isClosed()) {
+ statement.close();
+ }
+ resultSet = null;
+ statement = null;
+ currentSplit = null;
+ }
+ }
+
+ private void reOpenConnectionIfNeeded() throws SQLException, ClassNotFoundException {
Review Comment:
getOrEstablishConnection ?
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSource.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.typeinfo.TypeHint;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.source.Boundedness;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.api.connector.source.SourceReader;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.api.connector.source.SplitEnumerator;
+import org.apache.flink.api.connector.source.SplitEnumeratorContext;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumStateSerializer;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumerator;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumeratorState;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSqlSplitEnumeratorBase;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceReader;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceSplitReader;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplit;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplitSerializer;
+import org.apache.flink.core.io.SimpleVersionedSerializer;
+import org.apache.flink.util.Preconditions;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Objects;
+
+/** JDBC source. */
+@PublicEvolving
+public class JdbcSource<OUT>
+ implements Source<OUT, JdbcSourceSplit, JdbcSourceEnumeratorState>,
+ ResultTypeQueryable<OUT> {
+
+ private final Boundedness boundedness;
+ private final TypeInformation<OUT> typeInformation;
+
+ private final Configuration configuration;
+ private final JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider;
+
+ protected JdbcConnectionProvider connectionProvider;
+ private final ResultExtractor<OUT> resultExtractor;
+ private final DeliveryGuarantee deliveryGuarantee;
+
+ JdbcSource(
+ Configuration configuration,
+ JdbcConnectionProvider connectionProvider,
+ JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider,
+ ResultExtractor<OUT> resultExtractor,
+ @Nullable TypeInformation<OUT> typeInformation,
+ @Nullable DeliveryGuarantee deliveryGuarantee) {
Review Comment:
Should we create another constructor without this two vars, and here expect that are not null?
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/reader/JdbcSourceSplitReader.java:
##########
@@ -0,0 +1,291 @@
+/*
+ * 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.flink.connector.jdbc.source.reader;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.base.source.reader.RecordsBySplits;
+import org.apache.flink.connector.base.source.reader.RecordsWithSplitIds;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitReader;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitsChange;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplit;
+import org.apache.flink.util.Preconditions;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayDeque;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Queue;
+
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.AUTO_COMMIT;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.READER_FETCH_BATCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_CONCURRENCY;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_FETCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_TYPE;
+
+/**
+ * The JDBC source reader to read data from jdbc splits.
+ *
+ * @param <T> The type of the record read from the source.
+ */
+public class JdbcSourceSplitReader<T>
+ implements SplitReader<RecordAndOffset<T>, JdbcSourceSplit>, ResultTypeQueryable<T> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(JdbcSourceSplitReader.class);
+
+ private final Configuration config;
+ @Nullable private JdbcSourceSplit currentSplit;
+ private final Queue<JdbcSourceSplit> splits;
+ private final TypeInformation<T> typeInformation;
+ private final JdbcConnectionProvider connectionProvider;
+ private transient Connection connection;
+ private transient PreparedStatement statement;
+ private transient ResultSet resultSet;
+
+ private final ResultExtractor<T> resultExtractor;
+ protected boolean hasNextRecordCurrentSplit;
+ private final DeliveryGuarantee deliveryGuarantee;
+
+ private final int splitReaderFetchBatchSize;
+
+ private final int resultSetType;
+ private final int resultSetConcurrency;
+ private final int resultSetFetchSize;
+ // Boolean to distinguish between default value and explicitly set autoCommit mode.
+ private final Boolean autoCommit;
+ private int currentSplitOffset;
+
+ private final SourceReaderContext context;
+
+ public JdbcSourceSplitReader(
+ SourceReaderContext context,
+ Configuration config,
+ TypeInformation<T> typeInformation,
+ JdbcConnectionProvider connectionProvider,
+ DeliveryGuarantee deliveryGuarantee,
+ ResultExtractor<T> resultExtractor) {
+ this.context = Preconditions.checkNotNull(context);
+ this.config = Preconditions.checkNotNull(config);
+ this.typeInformation = Preconditions.checkNotNull(typeInformation);
+ this.connectionProvider = Preconditions.checkNotNull(connectionProvider);
+ this.resultSetType = config.getInteger(RESULTSET_TYPE);
+ this.resultSetConcurrency = config.getInteger(RESULTSET_CONCURRENCY);
+ this.resultSetFetchSize = config.getInteger(RESULTSET_FETCH_SIZE);
+ this.autoCommit = config.getBoolean(AUTO_COMMIT);
+ this.deliveryGuarantee = Preconditions.checkNotNull(deliveryGuarantee);
+ this.splits = new ArrayDeque<>();
+ this.hasNextRecordCurrentSplit = false;
+ this.currentSplit = null;
+ int splitReaderFetchBatchSize = config.getInteger(READER_FETCH_BATCH_SIZE);
+ Preconditions.checkArgument(
+ splitReaderFetchBatchSize > 0 && splitReaderFetchBatchSize < Integer.MAX_VALUE);
+ this.splitReaderFetchBatchSize = splitReaderFetchBatchSize;
+ this.resultExtractor = Preconditions.checkNotNull(resultExtractor);
+ this.currentSplitOffset = 0;
+ }
+
+ @Override
+ public RecordsWithSplitIds<RecordAndOffset<T>> fetch() throws IOException {
+
+ checkSplitOrStartNext();
+
+ if (!hasNextRecordCurrentSplit) {
+ return finishSplit();
+ }
+
+ RecordsBySplits.Builder<RecordAndOffset<T>> recordAndOffsetBuilder =
+ new RecordsBySplits.Builder<>();
+ Preconditions.checkState(currentSplit != null, "currentSplit");
+ int batch = this.splitReaderFetchBatchSize;
+ while (batch > 0 && hasNextRecordCurrentSplit) {
+ try {
+ T ele = resultExtractor.extract(resultSet);
Review Comment:
ele -> record
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1441298453
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSource.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.typeinfo.TypeHint;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.source.Boundedness;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.api.connector.source.SourceReader;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.api.connector.source.SplitEnumerator;
+import org.apache.flink.api.connector.source.SplitEnumeratorContext;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumStateSerializer;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumerator;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumeratorState;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSqlSplitEnumeratorBase;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceReader;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceSplitReader;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplit;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplitSerializer;
+import org.apache.flink.core.io.SimpleVersionedSerializer;
+import org.apache.flink.util.Preconditions;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Objects;
+
+/** JDBC source. */
+@PublicEvolving
+public class JdbcSource<OUT>
+ implements Source<OUT, JdbcSourceSplit, JdbcSourceEnumeratorState>,
+ ResultTypeQueryable<OUT> {
+
+ private final Boundedness boundedness;
+ private final TypeInformation<OUT> typeInformation;
+
+ private final Configuration configuration;
+ private final JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider;
+
+ protected JdbcConnectionProvider connectionProvider;
+ private final ResultExtractor<OUT> resultExtractor;
+ private final DeliveryGuarantee deliveryGuarantee;
+
+ JdbcSource(
+ Configuration configuration,
+ JdbcConnectionProvider connectionProvider,
+ JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider,
+ ResultExtractor<OUT> resultExtractor,
+ @Nullable TypeInformation<OUT> typeInformation,
+ @Nullable DeliveryGuarantee deliveryGuarantee) {
Review Comment:
Thx for the comment.
I have rechecked the logic of this section and come to the following conclusion in my limited read:
- `typeInformation ` must not be null, because when `typeInformation ` is null, we currently cannot provide true type information through type inference.
- `deliveryGuarantee` could be null.
So how about retaining the following two construction methods?
```
JdbcSource(
Configuration configuration,
JdbcConnectionProvider connectionProvider,
JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider,
ResultExtractor<OUT> resultExtractor,
@Nonnull TypeInformation<OUT> typeInformation,
@Nullable DeliveryGuarantee deliveryGuarantee) {
this.configuration = Preconditions.checkNotNull(configuration);
this.connectionProvider = Preconditions.checkNotNull(connectionProvider);
this.sqlSplitEnumeratorProvider = Preconditions.checkNotNull(sqlSplitEnumeratorProvider);
this.resultExtractor = Preconditions.checkNotNull(resultExtractor);
this.deliveryGuarantee = deliveryGuarantee == null ? DeliveryGuarantee.NONE : deliveryGuarantee;
this.typeInformation = Preconditions.checkNotNull(typeInformation);
this.boundedness = Boundedness.BOUNDED;
}
JdbcSource(
Configuration configuration,
JdbcConnectionProvider connectionProvider,
JdbcSqlSplitEnumeratorBase.Provider<JdbcSourceSplit> sqlSplitEnumeratorProvider,
ResultExtractor<OUT> resultExtractor,
@Nonnull TypeInformation<OUT> typeInformation) {
this.configuration = Preconditions.checkNotNull(configuration);
this.connectionProvider = Preconditions.checkNotNull(connectionProvider);
this.sqlSplitEnumeratorProvider = Preconditions.checkNotNull(sqlSplitEnumeratorProvider);
this.resultExtractor = Preconditions.checkNotNull(resultExtractor);
this.deliveryGuarantee = DeliveryGuarantee.NONE;
this.typeInformation = Preconditions.checkNotNull(typeInformation);
this.boundedness = Boundedness.BOUNDED;
}
```
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#issuecomment-2016325794
Thank you @snuyanzin for the review.
I made some change based on your comments~
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#issuecomment-2020733326
@davidradl @eskabetxe could you please have another look here?
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#issuecomment-2042712930
Thanks everybody related for the pr. :)
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#issuecomment-2042573923
thanks @RocMarshal for the hard work
thanks a lot everyone for the review
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1536549280
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/reader/JdbcSourceSplitReader.java:
##########
@@ -0,0 +1,291 @@
+/*
+ * 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.flink.connector.jdbc.source.reader;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.base.source.reader.RecordsBySplits;
+import org.apache.flink.connector.base.source.reader.RecordsWithSplitIds;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitReader;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitsChange;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplit;
+import org.apache.flink.util.Preconditions;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayDeque;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Queue;
+
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.AUTO_COMMIT;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.READER_FETCH_BATCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_CONCURRENCY;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_FETCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_TYPE;
+
+/**
+ * The JDBC source reader to read data from jdbc splits.
+ *
+ * @param <T> The type of the record read from the source.
+ */
+public class JdbcSourceSplitReader<T>
+ implements SplitReader<RecordAndOffset<T>, JdbcSourceSplit>, ResultTypeQueryable<T> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(JdbcSourceSplitReader.class);
+
+ private final Configuration config;
+ @Nullable private JdbcSourceSplit currentSplit;
+ private final Queue<JdbcSourceSplit> splits;
+ private final TypeInformation<T> typeInformation;
+ private final JdbcConnectionProvider connectionProvider;
+ private transient Connection connection;
+ private transient PreparedStatement statement;
+ private transient ResultSet resultSet;
+
+ private final ResultExtractor<T> resultExtractor;
+ protected boolean hasNextRecordCurrentSplit;
+ private final DeliveryGuarantee deliveryGuarantee;
+
+ private final int splitReaderFetchBatchSize;
+
+ private final int resultSetType;
+ private final int resultSetConcurrency;
+ private final int resultSetFetchSize;
+ // Boolean to distinguish between default value and explicitly set autoCommit mode.
+ private final Boolean autoCommit;
+ private int currentSplitOffset;
+
+ private final SourceReaderContext context;
+
+ public JdbcSourceSplitReader(
+ SourceReaderContext context,
+ Configuration config,
+ TypeInformation<T> typeInformation,
+ JdbcConnectionProvider connectionProvider,
+ DeliveryGuarantee deliveryGuarantee,
+ ResultExtractor<T> resultExtractor) {
+ this.context = Preconditions.checkNotNull(context);
+ this.config = Preconditions.checkNotNull(config);
+ this.typeInformation = Preconditions.checkNotNull(typeInformation);
+ this.connectionProvider = Preconditions.checkNotNull(connectionProvider);
+ this.resultSetType = config.getInteger(RESULTSET_TYPE);
+ this.resultSetConcurrency = config.getInteger(RESULTSET_CONCURRENCY);
+ this.resultSetFetchSize = config.getInteger(RESULTSET_FETCH_SIZE);
+ this.autoCommit = config.getBoolean(AUTO_COMMIT);
+ this.deliveryGuarantee = Preconditions.checkNotNull(deliveryGuarantee);
+ this.splits = new ArrayDeque<>();
+ this.hasNextRecordCurrentSplit = false;
+ this.currentSplit = null;
+ int splitReaderFetchBatchSize = config.getInteger(READER_FETCH_BATCH_SIZE);
+ Preconditions.checkArgument(
+ splitReaderFetchBatchSize > 0 && splitReaderFetchBatchSize < Integer.MAX_VALUE);
+ this.splitReaderFetchBatchSize = splitReaderFetchBatchSize;
+ this.resultExtractor = Preconditions.checkNotNull(resultExtractor);
+ this.currentSplitOffset = 0;
+ }
+
+ @Override
+ public RecordsWithSplitIds<RecordAndOffset<T>> fetch() throws IOException {
+
+ checkSplitOrStartNext();
+
+ if (!hasNextRecordCurrentSplit) {
+ return finishSplit();
+ }
+
+ RecordsBySplits.Builder<RecordAndOffset<T>> recordAndOffsetBuilder =
+ new RecordsBySplits.Builder<>();
+ Preconditions.checkState(currentSplit != null, "currentSplit");
+ int batch = this.splitReaderFetchBatchSize;
+ while (batch > 0 && hasNextRecordCurrentSplit) {
+ try {
+ T record = resultExtractor.extract(resultSet);
+ recordAndOffsetBuilder.add(
+ currentSplit, new RecordAndOffset<>(record, ++currentSplitOffset, 0));
+ batch--;
+ hasNextRecordCurrentSplit = resultSet.next();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ if (!hasNextRecordCurrentSplit) {
+ currentSplitOffset = 0;
+ recordAndOffsetBuilder.addFinishedSplit(currentSplit.splitId());
+ closeResultSetAndStatement();
+ }
+ return recordAndOffsetBuilder.build();
+ }
+
+ private RecordsWithSplitIds<RecordAndOffset<T>> finishSplit() {
+
+ closeResultSetAndStatement();
+
+ RecordsBySplits.Builder<RecordAndOffset<T>> builder = new RecordsBySplits.Builder<>();
+ Preconditions.checkState(currentSplit != null, "currentSplit");
+ builder.addFinishedSplit(currentSplit.splitId());
+ currentSplit = null;
+ return builder.build();
+ }
+
+ private void closeResultSetAndStatement() {
+ try {
+ if (resultSet != null && !resultSet.isClosed()) {
+ resultSet.close();
+ }
+ if (statement != null && !statement.isClosed()) {
+ statement.close();
+ }
+ resultSet = null;
+ statement = null;
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public void handleSplitsChanges(SplitsChange<JdbcSourceSplit> splitsChanges) {
+ if (!(splitsChanges instanceof SplitsAddition)) {
+ throw new UnsupportedOperationException(
+ String.format(
+ "The SplitChange type of %s is not supported.",
+ splitsChanges.getClass()));
+ }
+
+ LOG.debug("Handling split change {}", splitsChanges);
+ splits.addAll(splitsChanges.splits());
+ }
+
+ @Override
+ public void wakeUp() {}
+
+ @Override
+ public void close() throws Exception {
+ closeResultSetAndStatement();
+ if (connection != null && !connection.isClosed()) {
+ connection.close();
+ }
+ connection = null;
+ currentSplit = null;
+ }
+
+ @Override
+ public TypeInformation<T> getProducedType() {
+ return typeInformation;
+ }
+
+ private void checkSplitOrStartNext() {
+
+ try {
+ if (hasNextRecordCurrentSplit && resultSet != null) {
+ return;
+ }
+
+ final JdbcSourceSplit nextSplit = splits.poll();
+ if (nextSplit == null) {
+ throw new IOException("Cannot fetch from another split - no split remaining");
+ }
+ currentSplit = nextSplit;
+ openResultSetForSplit(currentSplit);
+ } catch (SQLException | ClassNotFoundException | IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private void discardSplit(JdbcSourceSplit split) throws SQLException {
+ if (split.getOffset() != 0) {
+ hasNextRecordCurrentSplit = false;
+ currentSplitOffset = 0;
+ if (resultSet != null && !resultSet.isClosed()) {
+ resultSet.close();
+ }
+ if (statement != null && !statement.isClosed()) {
+ statement.close();
+ }
+ resultSet = null;
+ statement = null;
+ currentSplit = null;
+ }
+ }
+
+ private void getOrEstablishConnection() throws SQLException, ClassNotFoundException {
+ connection = connectionProvider.getOrEstablishConnection();
+ if (autoCommit == null) {
+ return;
+ }
+ if (autoCommit != connection.getAutoCommit()) {
+ connection.setAutoCommit(autoCommit);
+ }
+ }
+
+ private void openResultSetForSplit(JdbcSourceSplit split)
+ throws SQLException, ClassNotFoundException {
+ getOrEstablishConnection();
+ statement =
+ connection.prepareStatement(
+ split.getSqlTemplate(), resultSetType, resultSetConcurrency);
+ if (split.getParameters() != null) {
+ Object[] objs = split.getParameters();
+ for (int i = 0; i < objs.length; i++) {
+ statement.setObject(i + 1, objs[i]);
+ }
+ }
+ statement.setFetchSize(resultSetFetchSize);
+ resultSet = statement.executeQuery();
+ hasNextRecordCurrentSplit = resultSet.next();
+ // TODO fine-grained EXACTLY_ONCE/AT_MOST_ONCE/AT_LEAST_ONCE will be introduced in the
Review Comment:
deleted.
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSourceBuilder.java:
##########
@@ -0,0 +1,253 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.enumerator.SqlTemplateSplitEnumerator;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.split.JdbcParameterValuesProvider;
+import org.apache.flink.types.Row;
+import org.apache.flink.util.Preconditions;
+import org.apache.flink.util.StringUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.AUTO_COMMIT;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.READER_FETCH_BATCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_CONCURRENCY;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_FETCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_TYPE;
+
+/**
+ * A tool is used to build {@link JdbcSource} quickly.
+ *
+ * <pre><code>
+ * JdbcSource<Row> source = JdbcSource.<Row>builder()
+ * .setSql(validSql)
+ * .setResultExtractor(new RowResultExtractor())
+ * .setDBUrl(dbUrl)
+ * .setDriverName(driverName)
+ * .setTypeInformation(new TypeHint<Row>() {}.getTypeInfo())
+ * .setPassword(password)
+ * .setUsername(username)
+ * .build();
+ * </code></pre>
+ *
+ * <p>In order to query the JDBC source in parallel, you need to provide a parameterized query
+ * template (i.e. a valid {@link PreparedStatement}) and a {@link JdbcParameterValuesProvider} which
+ * provides binding values for the query parameters. E.g.:
+ *
+ * <pre><code>
+ *
+ * Serializable[][] queryParameters = new String[2][1];
+ * queryParameters[0] = new String[]{"Kumar"};
+ * queryParameters[1] = new String[]{"Tan Ah Teck"};
+ *
+ * JdbcSource<Row> jdbcSource = JdbcSource.<Row>builder()
+ * .setResultExtractor(new RowResultExtractor())
+ * .setTypeInformation(new TypeHint<Row>() {}.getTypeInfo())
+ * .setPassword(password)
+ * .setUsername(username)
+ * .setDriverName("org.apache.derby.jdbc.EmbeddedDriver")
+ * .setDBUrl("jdbc:derby:memory:ebookshop")
+ * .setSql("select * from books WHERE author = ?")
+ * .setJdbcParameterValuesProvider(new JdbcGenericParameterValuesProvider(queryParameters))
+ * .build();
+ * </code></pre>
+ *
+ * @see Row
+ * @see JdbcParameterValuesProvider
+ * @see PreparedStatement
+ * @see DriverManager
+ * @see JdbcSource
+ */
+@PublicEvolving
+public class JdbcSourceBuilder<OUT> {
+
+ public static final Logger LOG = LoggerFactory.getLogger(JdbcSourceBuilder.class);
+
+ private final Configuration configuration;
+
+ private int splitReaderFetchBatchSize;
+ private int resultSetType;
+ private int resultSetConcurrency;
+ private int resultSetFetchSize;
+ // Boolean to distinguish between default value and explicitly set autoCommit mode.
+ private Boolean autoCommit;
+
+ // TODO It would need a builder method to render after introducing streaming semantic.
Review Comment:
Updated.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1535501299
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/reader/JdbcSourceSplitReader.java:
##########
@@ -0,0 +1,291 @@
+/*
+ * 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.flink.connector.jdbc.source.reader;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.base.source.reader.RecordsBySplits;
+import org.apache.flink.connector.base.source.reader.RecordsWithSplitIds;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitReader;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitsChange;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplit;
+import org.apache.flink.util.Preconditions;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayDeque;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Queue;
+
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.AUTO_COMMIT;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.READER_FETCH_BATCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_CONCURRENCY;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_FETCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_TYPE;
+
+/**
+ * The JDBC source reader to read data from jdbc splits.
+ *
+ * @param <T> The type of the record read from the source.
+ */
+public class JdbcSourceSplitReader<T>
+ implements SplitReader<RecordAndOffset<T>, JdbcSourceSplit>, ResultTypeQueryable<T> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(JdbcSourceSplitReader.class);
+
+ private final Configuration config;
+ @Nullable private JdbcSourceSplit currentSplit;
+ private final Queue<JdbcSourceSplit> splits;
+ private final TypeInformation<T> typeInformation;
+ private final JdbcConnectionProvider connectionProvider;
+ private transient Connection connection;
+ private transient PreparedStatement statement;
+ private transient ResultSet resultSet;
+
+ private final ResultExtractor<T> resultExtractor;
+ protected boolean hasNextRecordCurrentSplit;
+ private final DeliveryGuarantee deliveryGuarantee;
+
+ private final int splitReaderFetchBatchSize;
+
+ private final int resultSetType;
+ private final int resultSetConcurrency;
+ private final int resultSetFetchSize;
+ // Boolean to distinguish between default value and explicitly set autoCommit mode.
+ private final Boolean autoCommit;
+ private int currentSplitOffset;
+
+ private final SourceReaderContext context;
+
+ public JdbcSourceSplitReader(
+ SourceReaderContext context,
+ Configuration config,
+ TypeInformation<T> typeInformation,
+ JdbcConnectionProvider connectionProvider,
+ DeliveryGuarantee deliveryGuarantee,
+ ResultExtractor<T> resultExtractor) {
+ this.context = Preconditions.checkNotNull(context);
+ this.config = Preconditions.checkNotNull(config);
+ this.typeInformation = Preconditions.checkNotNull(typeInformation);
+ this.connectionProvider = Preconditions.checkNotNull(connectionProvider);
+ this.resultSetType = config.getInteger(RESULTSET_TYPE);
+ this.resultSetConcurrency = config.getInteger(RESULTSET_CONCURRENCY);
+ this.resultSetFetchSize = config.getInteger(RESULTSET_FETCH_SIZE);
+ this.autoCommit = config.getBoolean(AUTO_COMMIT);
+ this.deliveryGuarantee = Preconditions.checkNotNull(deliveryGuarantee);
+ this.splits = new ArrayDeque<>();
+ this.hasNextRecordCurrentSplit = false;
+ this.currentSplit = null;
+ int splitReaderFetchBatchSize = config.getInteger(READER_FETCH_BATCH_SIZE);
+ Preconditions.checkArgument(
+ splitReaderFetchBatchSize > 0 && splitReaderFetchBatchSize < Integer.MAX_VALUE);
+ this.splitReaderFetchBatchSize = splitReaderFetchBatchSize;
+ this.resultExtractor = Preconditions.checkNotNull(resultExtractor);
+ this.currentSplitOffset = 0;
+ }
+
+ @Override
+ public RecordsWithSplitIds<RecordAndOffset<T>> fetch() throws IOException {
+
+ checkSplitOrStartNext();
+
+ if (!hasNextRecordCurrentSplit) {
+ return finishSplit();
+ }
+
+ RecordsBySplits.Builder<RecordAndOffset<T>> recordAndOffsetBuilder =
+ new RecordsBySplits.Builder<>();
+ Preconditions.checkState(currentSplit != null, "currentSplit");
+ int batch = this.splitReaderFetchBatchSize;
+ while (batch > 0 && hasNextRecordCurrentSplit) {
+ try {
+ T record = resultExtractor.extract(resultSet);
+ recordAndOffsetBuilder.add(
+ currentSplit, new RecordAndOffset<>(record, ++currentSplitOffset, 0));
+ batch--;
+ hasNextRecordCurrentSplit = resultSet.next();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ if (!hasNextRecordCurrentSplit) {
+ currentSplitOffset = 0;
+ recordAndOffsetBuilder.addFinishedSplit(currentSplit.splitId());
+ closeResultSetAndStatement();
+ }
+ return recordAndOffsetBuilder.build();
+ }
+
+ private RecordsWithSplitIds<RecordAndOffset<T>> finishSplit() {
+
+ closeResultSetAndStatement();
+
+ RecordsBySplits.Builder<RecordAndOffset<T>> builder = new RecordsBySplits.Builder<>();
+ Preconditions.checkState(currentSplit != null, "currentSplit");
+ builder.addFinishedSplit(currentSplit.splitId());
+ currentSplit = null;
+ return builder.build();
+ }
+
+ private void closeResultSetAndStatement() {
+ try {
+ if (resultSet != null && !resultSet.isClosed()) {
+ resultSet.close();
+ }
+ if (statement != null && !statement.isClosed()) {
+ statement.close();
+ }
+ resultSet = null;
+ statement = null;
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public void handleSplitsChanges(SplitsChange<JdbcSourceSplit> splitsChanges) {
+ if (!(splitsChanges instanceof SplitsAddition)) {
+ throw new UnsupportedOperationException(
+ String.format(
+ "The SplitChange type of %s is not supported.",
+ splitsChanges.getClass()));
+ }
+
+ LOG.debug("Handling split change {}", splitsChanges);
+ splits.addAll(splitsChanges.splits());
+ }
+
+ @Override
+ public void wakeUp() {}
+
+ @Override
+ public void close() throws Exception {
+ closeResultSetAndStatement();
+ if (connection != null && !connection.isClosed()) {
+ connection.close();
+ }
+ connection = null;
+ currentSplit = null;
+ }
+
+ @Override
+ public TypeInformation<T> getProducedType() {
+ return typeInformation;
+ }
+
+ private void checkSplitOrStartNext() {
+
+ try {
+ if (hasNextRecordCurrentSplit && resultSet != null) {
+ return;
+ }
+
+ final JdbcSourceSplit nextSplit = splits.poll();
+ if (nextSplit == null) {
+ throw new IOException("Cannot fetch from another split - no split remaining");
+ }
+ currentSplit = nextSplit;
+ openResultSetForSplit(currentSplit);
+ } catch (SQLException | ClassNotFoundException | IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private void discardSplit(JdbcSourceSplit split) throws SQLException {
+ if (split.getOffset() != 0) {
+ hasNextRecordCurrentSplit = false;
+ currentSplitOffset = 0;
+ if (resultSet != null && !resultSet.isClosed()) {
+ resultSet.close();
+ }
+ if (statement != null && !statement.isClosed()) {
+ statement.close();
+ }
+ resultSet = null;
+ statement = null;
+ currentSplit = null;
+ }
+ }
+
+ private void getOrEstablishConnection() throws SQLException, ClassNotFoundException {
+ connection = connectionProvider.getOrEstablishConnection();
+ if (autoCommit == null) {
+ return;
+ }
+ if (autoCommit != connection.getAutoCommit()) {
+ connection.setAutoCommit(autoCommit);
+ }
+ }
+
+ private void openResultSetForSplit(JdbcSourceSplit split)
+ throws SQLException, ClassNotFoundException {
+ getOrEstablishConnection();
+ statement =
+ connection.prepareStatement(
+ split.getSqlTemplate(), resultSetType, resultSetConcurrency);
+ if (split.getParameters() != null) {
+ Object[] objs = split.getParameters();
+ for (int i = 0; i < objs.length; i++) {
+ statement.setObject(i + 1, objs[i]);
+ }
+ }
+ statement.setFetchSize(resultSetFetchSize);
+ resultSet = statement.executeQuery();
+ hasNextRecordCurrentSplit = resultSet.next();
+ // TODO fine-grained EXACTLY_ONCE/AT_MOST_ONCE/AT_LEAST_ONCE will be introduced in the
Review Comment:
I'm not sure having such TODO will help
probably it's better to have a dedicated jira issue and mention it here
At least we could track it then
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "eskabetxe (via GitHub)" <gi...@apache.org>.
eskabetxe commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1405924661
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSource.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.typeinfo.TypeHint;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.source.Boundedness;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.api.connector.source.SourceReader;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.api.connector.source.SplitEnumerator;
+import org.apache.flink.api.connector.source.SplitEnumeratorContext;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumStateSerializer;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumerator;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumeratorState;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSqlSplitEnumeratorBase;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceReader;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceSplitReader;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplit;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplitSerializer;
+import org.apache.flink.core.io.SimpleVersionedSerializer;
+import org.apache.flink.util.Preconditions;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Objects;
+
+/** JDBC source. */
+@Internal
Review Comment:
I think this should be PublicEvolving
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSourceBuilder.java:
##########
@@ -0,0 +1,206 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.annotation.Experimental;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.enumerator.SqlTemplateSplitEnumerator;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.split.JdbcParameterValuesProvider;
+import org.apache.flink.util.Preconditions;
+import org.apache.flink.util.StringUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+
+import java.sql.ResultSet;
+
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.AUTO_COMMIT;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.READER_FETCH_BATCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_CONCURRENCY;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_FETCH_SIZE;
+import static org.apache.flink.connector.jdbc.source.JdbcSourceOptions.RESULTSET_TYPE;
+
+/** A tool is used to build {@link JdbcSource} quickly. */
+@Experimental
Review Comment:
should not be PublicEvolving?
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "eskabetxe (via GitHub)" <gi...@apache.org>.
eskabetxe commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1405924661
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/JdbcSource.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.flink.connector.jdbc.source;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.typeinfo.TypeHint;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.source.Boundedness;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.api.connector.source.SourceReader;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.api.connector.source.SplitEnumerator;
+import org.apache.flink.api.connector.source.SplitEnumeratorContext;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.base.DeliveryGuarantee;
+import org.apache.flink.connector.jdbc.internal.connection.JdbcConnectionProvider;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumStateSerializer;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumerator;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSourceEnumeratorState;
+import org.apache.flink.connector.jdbc.source.enumerator.JdbcSqlSplitEnumeratorBase;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceReader;
+import org.apache.flink.connector.jdbc.source.reader.JdbcSourceSplitReader;
+import org.apache.flink.connector.jdbc.source.reader.extractor.ResultExtractor;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplit;
+import org.apache.flink.connector.jdbc.source.split.JdbcSourceSplitSerializer;
+import org.apache.flink.core.io.SimpleVersionedSerializer;
+import org.apache.flink.util.Preconditions;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Objects;
+
+/** JDBC source. */
+@Internal
Review Comment:
should not be PublicEvolving?
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1441300360
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/reader/extractor/ResultExtractor.java:
##########
@@ -0,0 +1,57 @@
+/*
+ * 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.flink.connector.jdbc.source.reader.extractor;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.types.Row;
+
+import java.io.Serializable;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * The Extractor to extract the data from {@link ResultSet}.
+ *
+ * @param <T> The target data type.
+ */
+@PublicEvolving
+public interface ResultExtractor<T> extends Serializable {
+
+ /**
+ * Extract the data from the current point line of the result.
+ *
+ * @param resultSet Result set queried from a sql.
+ * @return The data object filled by the current line of the resultSet.
+ * @throws SQLException SQL exception.
+ */
+ T extract(ResultSet resultSet) throws SQLException;
+
+ /**
+ * The identifier of the extractor.
+ *
+ * @return identifier in {@link String} type.
+ */
+ default String identifier() {
Review Comment:
What do you think if we change the default implementation to this way (and remove the implementation of `RowResultExtractor`)
```
default String identifier() {
return this.getClass().getSimpleName();
}
```
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-33459][Connector/JDBC] Support the new source that keeps the same functionality as the original JDBC input format [flink-connector-jdbc]
Posted by "RocMarshal (via GitHub)" <gi...@apache.org>.
RocMarshal commented on code in PR #78:
URL: https://github.com/apache/flink-connector-jdbc/pull/78#discussion_r1441300360
##########
flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/source/reader/extractor/ResultExtractor.java:
##########
@@ -0,0 +1,57 @@
+/*
+ * 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.flink.connector.jdbc.source.reader.extractor;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.types.Row;
+
+import java.io.Serializable;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * The Extractor to extract the data from {@link ResultSet}.
+ *
+ * @param <T> The target data type.
+ */
+@PublicEvolving
+public interface ResultExtractor<T> extends Serializable {
+
+ /**
+ * Extract the data from the current point line of the result.
+ *
+ * @param resultSet Result set queried from a sql.
+ * @return The data object filled by the current line of the resultSet.
+ * @throws SQLException SQL exception.
+ */
+ T extract(ResultSet resultSet) throws SQLException;
+
+ /**
+ * The identifier of the extractor.
+ *
+ * @return identifier in {@link String} type.
+ */
+ default String identifier() {
Review Comment:
What do you think if we change the default implementation to this way (and remove the implementation/override method of `RowResultExtractor`)
```
default String identifier() {
return this.getClass().getSimpleName();
}
```
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org