Unverified Commit 2b7a9929 authored by 金鑫's avatar 金鑫 Committed by GitHub

scan partition support datetime (#1272)

* Update cdcsource_statements.md cdcsource增加支持多目标库同步功能。 * partition分片增加支持时间类型字段下推. * partition分片增加支持时间类型字段下推. Co-authored-by: 's avatar金鑫 <jinixn@qdjinixn.net>
parent f112ccd8
...@@ -24,19 +24,35 @@ import org.apache.flink.connector.jdbc.JdbcExecutionOptions; ...@@ -24,19 +24,35 @@ import org.apache.flink.connector.jdbc.JdbcExecutionOptions;
import java.io.Serializable; import java.io.Serializable;
import java.util.Objects; import java.util.Objects;
/** Options for the JDBC lookup. */ /**
* Options for the JDBC lookup.
*/
public class JdbcLookupOptions implements Serializable { public class JdbcLookupOptions implements Serializable {
private final long cacheMaxSize; private final long cacheMaxSize;
private final long cacheExpireMs; private final long cacheExpireMs;
private final int maxRetryTimes; private final int maxRetryTimes;
private final String dataFilter; private final String dataFilter;
/**
* 是否是时间类型字段.
*/
private final boolean scanPartitionByDatetime;
public JdbcLookupOptions(long cacheMaxSize, long cacheExpireMs, int maxRetryTimes, String dataFilter) { public JdbcLookupOptions(long cacheMaxSize, long cacheExpireMs, int maxRetryTimes, String dataFilter) {
this.cacheMaxSize = cacheMaxSize; this.cacheMaxSize = cacheMaxSize;
this.cacheExpireMs = cacheExpireMs; this.cacheExpireMs = cacheExpireMs;
this.maxRetryTimes = maxRetryTimes; this.maxRetryTimes = maxRetryTimes;
this.dataFilter = dataFilter; this.dataFilter = dataFilter;
this.scanPartitionByDatetime = false;
}
public JdbcLookupOptions(long cacheMaxSize, long cacheExpireMs, int maxRetryTimes, String dataFilter,
boolean scanPartitionByDatetime) {
this.cacheMaxSize = cacheMaxSize;
this.cacheExpireMs = cacheExpireMs;
this.maxRetryTimes = maxRetryTimes;
this.dataFilter = dataFilter;
this.scanPartitionByDatetime = scanPartitionByDatetime;
} }
public long getCacheMaxSize() { public long getCacheMaxSize() {
...@@ -51,6 +67,10 @@ public class JdbcLookupOptions implements Serializable { ...@@ -51,6 +67,10 @@ public class JdbcLookupOptions implements Serializable {
return maxRetryTimes; return maxRetryTimes;
} }
public boolean isScanPartitionByDatetime() {
return scanPartitionByDatetime;
}
public static Builder builder() { public static Builder builder() {
return new Builder(); return new Builder();
} }
...@@ -76,34 +96,52 @@ public class JdbcLookupOptions implements Serializable { ...@@ -76,34 +96,52 @@ public class JdbcLookupOptions implements Serializable {
} }
} }
/** Builder of {@link JdbcLookupOptions}. */ /**
* Builder of {@link JdbcLookupOptions}.
*/
public static class Builder { public static class Builder {
private long cacheMaxSize = -1L; private long cacheMaxSize = -1L;
private long cacheExpireMs = -1L; private long cacheExpireMs = -1L;
private int maxRetryTimes = JdbcExecutionOptions.DEFAULT_MAX_RETRY_TIMES; private int maxRetryTimes = JdbcExecutionOptions.DEFAULT_MAX_RETRY_TIMES;
private String dataFilter = ""; private String dataFilter = "";
private boolean scanPartitionByDatetime = false;
/** optional, lookup cache max size, over this value, the old data will be eliminated. */ /**
* optional, lookup cache max size, over this value, the old data will be eliminated.
*/
public Builder setCacheMaxSize(long cacheMaxSize) { public Builder setCacheMaxSize(long cacheMaxSize) {
this.cacheMaxSize = cacheMaxSize; this.cacheMaxSize = cacheMaxSize;
return this; return this;
} }
/** optional, lookup cache expire mills, over this time, the old data will expire. */ /**
* optional, lookup cache expire mills, over this time, the old data will expire.
*/
public Builder setCacheExpireMs(long cacheExpireMs) { public Builder setCacheExpireMs(long cacheExpireMs) {
this.cacheExpireMs = cacheExpireMs; this.cacheExpireMs = cacheExpireMs;
return this; return this;
} }
/** optional, max retry times for jdbc connector. */ /**
* optional, max retry times for jdbc connector.
*/
public Builder setMaxRetryTimes(int maxRetryTimes) { public Builder setMaxRetryTimes(int maxRetryTimes) {
this.maxRetryTimes = maxRetryTimes; this.maxRetryTimes = maxRetryTimes;
return this; return this;
} }
/**
* optional, max retry times for jdbc connector.
*/
public Builder setScanPartitionByDatetime(boolean scanPartitionByDatetime) {
this.scanPartitionByDatetime = scanPartitionByDatetime;
return this;
}
public JdbcLookupOptions build() { public JdbcLookupOptions build() {
return new JdbcLookupOptions(cacheMaxSize, cacheExpireMs, maxRetryTimes, dataFilter); return new JdbcLookupOptions(cacheMaxSize, cacheExpireMs, maxRetryTimes, dataFilter,
scanPartitionByDatetime);
} }
} }
} }
/*
*
* 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.split;
import org.apache.flink.util.Preconditions;
import java.io.Serializable;
import java.sql.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* JdbcDatetimeBetweenParametersProvider
*
* @author 金鑫
* @since 2022/11/18 11:05
*/
public class JdbcDatetimeBetweenParametersProvider implements JdbcParameterValuesProvider {
protected static final Logger logger = LoggerFactory.getLogger(JdbcDatetimeBetweenParametersProvider.class);
private final long minVal;
private final long maxVal;
private long batchSize;
private int batchNum;
/**
* NumericBetweenParametersProviderJdbc constructor.
*
* @param minVal the lower bound of the produced "from" values
* @param maxVal the upper bound of the produced "to" values
*/
public JdbcDatetimeBetweenParametersProvider(long minVal, long maxVal) {
Preconditions.checkArgument(minVal <= maxVal, "minVal must not be larger than maxVal");
this.minVal = minVal;
this.maxVal = maxVal;
}
/**
* NumericBetweenParametersProviderJdbc constructor.
*
* @param fetchSize the max distance between the produced from/to pairs
* @param minVal the lower bound of the produced "from" values
* @param maxVal the upper bound of the produced "to" values
*/
public JdbcDatetimeBetweenParametersProvider(long fetchSize, long minVal, long maxVal) {
Preconditions.checkArgument(minVal <= maxVal, "minVal must not be larger than maxVal");
this.minVal = minVal;
this.maxVal = maxVal;
ofBatchSize(fetchSize);
}
public JdbcDatetimeBetweenParametersProvider ofBatchSize(long batchSize) {
Preconditions.checkArgument(batchSize > 0, "Batch size must be positive");
long maxElemCount = (maxVal - minVal) + 1;
if (batchSize > maxElemCount) {
batchSize = maxElemCount;
}
this.batchSize = batchSize;
this.batchNum = new Double(Math.ceil((double) maxElemCount / batchSize)).intValue();
return this;
}
public JdbcDatetimeBetweenParametersProvider ofBatchNum(int batchNum) {
Preconditions.checkArgument(batchNum > 0, "Batch number must be positive");
long maxElemCount = (maxVal - minVal) + 1;
if (batchNum > maxElemCount) {
batchNum = (int) maxElemCount;
}
this.batchNum = batchNum;
this.batchSize = new Double(Math.ceil((double) maxElemCount / batchNum)).longValue();
return this;
}
@Override
public Serializable[][] getParameterValues() {
Preconditions.checkState(
batchSize > 0,
"Batch size and batch number must be positive. Have you called `ofBatchSize` or `ofBatchNum`?");
long maxElemCount = (maxVal - minVal) + 1;
long bigBatchNum = maxElemCount - (batchSize - 1) * batchNum;
Serializable[][] parameters = new Serializable[batchNum][2];
long start = minVal;
for (int i = 0; i < batchNum; i++) {
long end = start + batchSize - 1 - (i >= bigBatchNum ? 1 : 0);
parameters[i] = new Date[]{new Date(start), new Date(end)};
start = end + 1;
}
logger.error("时间范围 {}", parameters);
return parameters;
}
}
...@@ -98,6 +98,12 @@ public class JdbcConnectorOptions { ...@@ -98,6 +98,12 @@ public class JdbcConnectorOptions {
.noDefaultValue() .noDefaultValue()
.withDescription("The largest value of the last partition."); .withDescription("The largest value of the last partition.");
public static final ConfigOption<Boolean> SCAN_PARTITION_BY_DATETIME =
ConfigOptions.key("scan.partition.by-datetime")
.booleanType()
.defaultValue(false)
.withDescription("partition by datetime.");
public static final ConfigOption<Integer> SCAN_FETCH_SIZE = public static final ConfigOption<Integer> SCAN_FETCH_SIZE =
ConfigOptions.key("scan.fetch-size") ConfigOptions.key("scan.fetch-size")
.intType() .intType()
......
...@@ -28,6 +28,7 @@ import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.MAX_RET ...@@ -28,6 +28,7 @@ import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.MAX_RET
import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.PASSWORD; import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.PASSWORD;
import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.SCAN_AUTO_COMMIT; import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.SCAN_AUTO_COMMIT;
import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.SCAN_FETCH_SIZE; import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.SCAN_FETCH_SIZE;
import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.SCAN_PARTITION_BY_DATETIME;
import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.SCAN_PARTITION_COLUMN; import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.SCAN_PARTITION_COLUMN;
import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.SCAN_PARTITION_LOWER_BOUND; import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.SCAN_PARTITION_LOWER_BOUND;
import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.SCAN_PARTITION_NUM; import static org.apache.flink.connector.jdbc.table.JdbcConnectorOptions.SCAN_PARTITION_NUM;
...@@ -148,7 +149,8 @@ public class JdbcDynamicTableFactory implements DynamicTableSourceFactory, Dynam ...@@ -148,7 +149,8 @@ public class JdbcDynamicTableFactory implements DynamicTableSourceFactory, Dynam
readableConfig.get(LOOKUP_CACHE_MAX_ROWS), readableConfig.get(LOOKUP_CACHE_MAX_ROWS),
readableConfig.get(LOOKUP_CACHE_TTL).toMillis(), readableConfig.get(LOOKUP_CACHE_TTL).toMillis(),
readableConfig.get(LOOKUP_MAX_RETRIES), readableConfig.get(LOOKUP_MAX_RETRIES),
readableConfig.get(DATA_FILTER)); readableConfig.get(DATA_FILTER),
readableConfig.get(SCAN_PARTITION_BY_DATETIME));
} }
private JdbcExecutionOptions getJdbcExecutionOptions(ReadableConfig config) { private JdbcExecutionOptions getJdbcExecutionOptions(ReadableConfig config) {
...@@ -207,6 +209,7 @@ public class JdbcDynamicTableFactory implements DynamicTableSourceFactory, Dynam ...@@ -207,6 +209,7 @@ public class JdbcDynamicTableFactory implements DynamicTableSourceFactory, Dynam
optionalOptions.add(SINK_PARALLELISM); optionalOptions.add(SINK_PARALLELISM);
optionalOptions.add(MAX_RETRY_TIMEOUT); optionalOptions.add(MAX_RETRY_TIMEOUT);
optionalOptions.add(DATA_FILTER); optionalOptions.add(DATA_FILTER);
optionalOptions.add(SCAN_PARTITION_BY_DATETIME);
return optionalOptions; return optionalOptions;
} }
......
...@@ -24,6 +24,7 @@ import org.apache.flink.connector.jdbc.dialect.JdbcDialect; ...@@ -24,6 +24,7 @@ import org.apache.flink.connector.jdbc.dialect.JdbcDialect;
import org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions; import org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions;
import org.apache.flink.connector.jdbc.internal.options.JdbcLookupOptions; import org.apache.flink.connector.jdbc.internal.options.JdbcLookupOptions;
import org.apache.flink.connector.jdbc.internal.options.JdbcReadOptions; import org.apache.flink.connector.jdbc.internal.options.JdbcReadOptions;
import org.apache.flink.connector.jdbc.split.JdbcDatetimeBetweenParametersProvider;
import org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider; import org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider;
import org.apache.flink.table.api.TableSchema; import org.apache.flink.table.api.TableSchema;
import org.apache.flink.table.connector.ChangelogMode; import org.apache.flink.table.connector.ChangelogMode;
...@@ -49,12 +50,12 @@ public class JdbcDynamicTableSource ...@@ -49,12 +50,12 @@ public class JdbcDynamicTableSource
SupportsProjectionPushDown, SupportsProjectionPushDown,
SupportsLimitPushDown { SupportsLimitPushDown {
private final JdbcConnectorOptions options; protected final JdbcConnectorOptions options;
private final JdbcReadOptions readOptions; protected final JdbcReadOptions readOptions;
private final JdbcLookupOptions lookupOptions; protected final JdbcLookupOptions lookupOptions;
private TableSchema physicalSchema; protected TableSchema physicalSchema;
private final String dialectName; private final String dialectName;
private long limit = -1; protected long limit = -1;
public JdbcDynamicTableSource( public JdbcDynamicTableSource(
JdbcConnectorOptions options, JdbcConnectorOptions options,
...@@ -111,9 +112,16 @@ public class JdbcDynamicTableSource ...@@ -111,9 +112,16 @@ public class JdbcDynamicTableSource
long lowerBound = readOptions.getPartitionLowerBound().get(); long lowerBound = readOptions.getPartitionLowerBound().get();
long upperBound = readOptions.getPartitionUpperBound().get(); long upperBound = readOptions.getPartitionUpperBound().get();
int numPartitions = readOptions.getNumPartitions().get(); int numPartitions = readOptions.getNumPartitions().get();
builder.setParametersProvider( if (!lookupOptions.isScanPartitionByDatetime()) {
builder.setParametersProvider(
new JdbcNumericBetweenParametersProvider(lowerBound, upperBound) new JdbcNumericBetweenParametersProvider(lowerBound, upperBound)
.ofBatchNum(numPartitions)); .ofBatchNum(numPartitions));
} else {
//这里将时间戳转化为日期类型
builder.setParametersProvider(
new JdbcDatetimeBetweenParametersProvider(lowerBound, upperBound)
.ofBatchNum(numPartitions));
}
if (lookupOptions.getPreFilterCondition().length > 0) { if (lookupOptions.getPreFilterCondition().length > 0) {
query += " AND "; query += " AND ";
} else { } else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment