-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GCP Pub/Sub Source Connector (#1224)
* GCP Pub/Sub Source Connector * Reducing verbose logging * Changes following code review * Validate kcql on start * Bounce messages when queue full. * Providing unix epoch seconds and date * Applying formats * Revert double date * Update java-connectors/kafka-connect-common/src/main/java/io/lenses/streamreactor/common/config/base/KcqlSettings.java Co-authored-by: Mati Urban <[email protected]> Signed-off-by: David Sloan <[email protected]> * Review amendments * Use Map.of wherever possible --------- Signed-off-by: David Sloan <[email protected]> Co-authored-by: Mati Urban <[email protected]>
- Loading branch information
1 parent
b38f3a9
commit 2c565bd
Showing
81 changed files
with
3,493 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...connect-common/src/main/java/io/lenses/streamreactor/common/config/base/KcqlSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2017-2024 Lenses.io Ltd | ||
* | ||
* Licensed 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 io.lenses.streamreactor.common.config.base; | ||
|
||
import java.util.List; | ||
|
||
import lombok.Getter; | ||
import org.apache.kafka.common.config.ConfigDef; | ||
import org.apache.kafka.common.config.ConfigException; | ||
|
||
import io.lenses.kcql.Kcql; | ||
import io.lenses.streamreactor.common.config.base.model.ConnectorPrefix; | ||
import io.lenses.streamreactor.common.config.source.ConfigSource; | ||
import lombok.val; | ||
|
||
@Getter | ||
public class KcqlSettings implements ConfigSettings<List<Kcql>> { | ||
|
||
private static final String KCQL_DOC = | ||
"Contains the Kafka Connect Query Language describing data mappings from the source to the target system."; | ||
|
||
private final String kcqlSettingsKey; | ||
|
||
public KcqlSettings( | ||
ConnectorPrefix connectorPrefix) { | ||
kcqlSettingsKey = connectorPrefix.prefixKey("kcql"); | ||
} | ||
|
||
@Override | ||
public ConfigDef withSettings(ConfigDef configDef) { | ||
return configDef.define( | ||
kcqlSettingsKey, | ||
ConfigDef.Type.STRING, | ||
ConfigDef.Importance.HIGH, | ||
KCQL_DOC | ||
); | ||
} | ||
|
||
@Override | ||
public List<Kcql> parseFromConfig(ConfigSource configSource) { | ||
return Kcql.parseMultiple(getKCQLString(configSource)); | ||
} | ||
|
||
private String getKCQLString(ConfigSource configSource) { | ||
val raw = configSource.getString(kcqlSettingsKey); | ||
return raw.orElseThrow(() -> new ConfigException(String.format("Missing [%s]", kcqlSettingsKey))); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...nnect-common/src/main/java/io/lenses/streamreactor/common/config/base/intf/Converter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2017-2024 Lenses.io Ltd | ||
* | ||
* Licensed 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 io.lenses.streamreactor.common.config.base.intf; | ||
|
||
import org.apache.kafka.common.config.ConfigException; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Picks out the settings required from KCQL | ||
*/ | ||
public abstract class Converter<S, T> { | ||
|
||
public List<T> convertAll(List<S> source) throws ConfigException { | ||
return source.stream().map(this::convert).collect(Collectors.toList()); | ||
} | ||
|
||
protected abstract T convert(S source) throws ConfigException; | ||
} |
29 changes: 29 additions & 0 deletions
29
...t-common/src/main/java/io/lenses/streamreactor/common/config/base/intf/KcqlConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2017-2024 Lenses.io Ltd | ||
* | ||
* Licensed 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 io.lenses.streamreactor.common.config.base.intf; | ||
|
||
import io.lenses.kcql.Kcql; | ||
import org.apache.kafka.common.config.ConfigException; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Picks out the settings required from KCQL | ||
*/ | ||
public abstract class KcqlConverter<T> extends Converter<Kcql, T> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
.../kafka-connect-common/src/main/java/io/lenses/streamreactor/common/util/ListSplitter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2017-2024 Lenses.io Ltd | ||
* | ||
* Licensed 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 io.lenses.streamreactor.common.util; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* Utility class for List splitting. | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class ListSplitter { | ||
|
||
/** | ||
* Splits the given list into {@code maxN} sublists of roughly equal size. | ||
* If the list cannot be divided evenly, the remaining elements are distributed | ||
* among the sublists so that the size difference between any two sublists is at most 1. | ||
* | ||
* @param list the list to be split | ||
* @param maxN the number of sublists to create | ||
* @param <T> the type of elements in the list | ||
* @return a list of sublists, where each sublist contains a portion of the original list | ||
* @throws IllegalArgumentException if {@code maxN} is less than or equal to 0 | ||
*/ | ||
public static <T> List<List<T>> splitList(List<T> list, int maxN) { | ||
if (maxN <= 0) { | ||
throw new IllegalArgumentException("Number of parts must be greater than zero."); | ||
} | ||
|
||
int totalSize = list.size(); | ||
int partSize = totalSize / maxN; | ||
int remainder = totalSize % maxN; | ||
|
||
return IntStream.range(0, maxN) | ||
.mapToObj(i -> { | ||
int start = i * partSize + Math.min(i, remainder); | ||
int end = start + partSize + (i < remainder ? 1 : 0); | ||
return list.subList(start, Math.min(end, totalSize)); | ||
}) | ||
.filter(sublist -> !sublist.isEmpty()) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...tors/kafka-connect-common/src/main/java/io/lenses/streamreactor/common/util/MapUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2017-2024 Lenses.io Ltd | ||
* | ||
* Licensed 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 io.lenses.streamreactor.common.util; | ||
|
||
import java.util.Map; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* Utility class for map operations. | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class MapUtils { | ||
|
||
/** | ||
* Casts a map to a specified key and value type. | ||
* | ||
* @param map the map to cast | ||
* @param targetKeyType the class of the key type | ||
* @param targetValueType the class of the value type | ||
* @param <K> the target key type | ||
* @param <V> the target value type | ||
* @return the casted map | ||
* @throws IllegalArgumentException if the map contains keys or values of incorrect types | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <K, V> Map<K, V> castMap(Map<?, ?> map, Class<K> targetKeyType, Class<V> targetValueType) { | ||
map.forEach((key, value) -> { | ||
if (!isAssignable(key, targetKeyType) || !isAssignable(value, targetValueType)) { | ||
throw new IllegalArgumentException("Map contains invalid key or value type"); | ||
} | ||
}); | ||
return (Map<K, V>) map; | ||
} | ||
|
||
/** | ||
* Checks if an object is assignable to a specified type, allowing for null values. | ||
* | ||
* @param obj the object to check | ||
* @param type the target type | ||
* @param <T> the target type | ||
* @return true if the object is null or assignable to the type, false otherwise | ||
*/ | ||
private static <T> boolean isAssignable(Object obj, Class<T> type) { | ||
return obj == null || type.isAssignableFrom(obj.getClass()); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...kafka-connect-common/src/main/java/io/lenses/streamreactor/common/util/TasksSplitter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2017-2024 Lenses.io Ltd | ||
* | ||
* Licensed 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 io.lenses.streamreactor.common.util; | ||
|
||
import static io.lenses.kcql.Kcql.KCQL_MULTI_STATEMENT_SEPARATOR; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import io.lenses.streamreactor.common.config.base.KcqlSettings; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import lombok.val; | ||
|
||
/** | ||
* Utility class for splitting tasks based on KCQL statements. | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class TasksSplitter { | ||
|
||
/** | ||
* Splits tasks based on the KCQL statements provided in the properties map. | ||
* Each resulting map will contain the original properties and a subset of the KCQL statements. | ||
* | ||
* @param maxTasks the maximum number of tasks to split into | ||
* @param props the original properties map containing KCQL settings | ||
* @param kcqlSettings the KCQL settings object that provides the key for KCQL settings in the properties map | ||
* @return a list of maps, each containing the original properties and a subset of the KCQL statements | ||
*/ | ||
public static List<Map<String, String>> splitByKcqlStatements(int maxTasks, Map<String, String> props, | ||
KcqlSettings kcqlSettings) { | ||
val kcqlSettingsKey = kcqlSettings.getKcqlSettingsKey(); | ||
val kcqls = | ||
Arrays | ||
.stream(props.get(kcqlSettingsKey).split(KCQL_MULTI_STATEMENT_SEPARATOR)) | ||
.collect(Collectors.toList()); | ||
|
||
return ListSplitter | ||
.splitList(kcqls, maxTasks) | ||
.stream() | ||
.map(kcqlsForTask -> Stream.concat( | ||
props.entrySet().stream(), | ||
Stream.of(Map.entry(kcqlSettingsKey, String.join(";", kcqlsForTask))) | ||
).collect(Collectors.toUnmodifiableMap( | ||
Map.Entry::getKey, | ||
Map.Entry::getValue, | ||
(existing, replacement) -> replacement | ||
))) | ||
.collect(Collectors.toUnmodifiableList()); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
java-connectors/kafka-connect-common/src/main/resources/logback.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.