-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation for new SMT ExtractTopicFromValueSchema and tests
- Loading branch information
1 parent
9e9456e
commit 08c0396
Showing
6 changed files
with
502 additions
and
10 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
121 changes: 121 additions & 0 deletions
121
...ntegration-test/java/io/aiven/kafka/connect/transforms/TopicFromValueSchemaConnector.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,121 @@ | ||
/* | ||
* Copyright 2023 Aiven Oy | ||
* | ||
* 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.aiven.kafka.connect.transforms; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.kafka.common.config.ConfigDef; | ||
import org.apache.kafka.connect.connector.Task; | ||
import org.apache.kafka.connect.data.Schema; | ||
import org.apache.kafka.connect.data.SchemaBuilder; | ||
import org.apache.kafka.connect.data.Struct; | ||
import org.apache.kafka.connect.source.SourceConnector; | ||
import org.apache.kafka.connect.source.SourceRecord; | ||
import org.apache.kafka.connect.source.SourceTask; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A connector needed for testing of ExtractTopicFromValueSchema. | ||
* | ||
* <p>It just produces a fixed number of struct records with value schema name set. | ||
*/ | ||
public class TopicFromValueSchemaConnector extends SourceConnector { | ||
static final int MESSAGES_TO_PRODUCE = 10; | ||
|
||
private static final Logger log = LoggerFactory.getLogger(TopicFromValueSchemaConnector.class); | ||
static final String TOPIC = "topic-for-value-schema-connector-test"; | ||
static final String FIELD = "field-0"; | ||
|
||
static final String NAME = "com.acme.schema.SchemaNameToTopic"; | ||
|
||
@Override | ||
public void start(final Map<String, String> props) { | ||
} | ||
|
||
@Override | ||
public Class<? extends Task> taskClass() { | ||
return TopicFromValueSchemaConnectorTask.class; | ||
} | ||
|
||
@Override | ||
public List<Map<String, String>> taskConfigs(final int maxTasks) { | ||
return Collections.singletonList(Collections.emptyMap()); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
} | ||
|
||
@Override | ||
public ConfigDef config() { | ||
return new ConfigDef(); | ||
} | ||
|
||
@Override | ||
public String version() { | ||
return null; | ||
} | ||
|
||
public static class TopicFromValueSchemaConnectorTask extends SourceTask { | ||
private int counter = 0; | ||
|
||
private final Schema valueSchema = SchemaBuilder.struct() | ||
.field(FIELD, SchemaBuilder.STRING_SCHEMA) | ||
.name(NAME) | ||
.schema(); | ||
private final Struct value = new Struct(valueSchema).put(FIELD, "Data"); | ||
|
||
@Override | ||
public void start(final Map<String, String> props) { | ||
log.info("Started TopicFromValueSchemaConnector!!!"); | ||
} | ||
|
||
@Override | ||
public List<SourceRecord> poll() throws InterruptedException { | ||
if (counter >= MESSAGES_TO_PRODUCE) { | ||
return null; // indicate pause | ||
} | ||
|
||
final Map<String, String> sourcePartition = new HashMap<>(); | ||
sourcePartition.put("partition", "0"); | ||
final Map<String, String> sourceOffset = new HashMap<>(); | ||
sourceOffset.put("offset", Integer.toString(counter)); | ||
|
||
counter += 1; | ||
|
||
return Collections.singletonList( | ||
new SourceRecord(sourcePartition, sourceOffset, | ||
TOPIC, | ||
valueSchema, value) | ||
); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
} | ||
|
||
@Override | ||
public String version() { | ||
return null; | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/main/java/io/aiven/kafka/connect/transforms/ExtractTopicFromValueSchema.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,107 @@ | ||
/* | ||
* Copyright 2023 Aiven Oy | ||
* | ||
* 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.aiven.kafka.connect.transforms; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.kafka.common.config.ConfigDef; | ||
import org.apache.kafka.connect.connector.ConnectRecord; | ||
import org.apache.kafka.connect.errors.DataException; | ||
import org.apache.kafka.connect.transforms.Transformation; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public abstract class ExtractTopicFromValueSchema<R extends ConnectRecord<R>> implements Transformation<R> { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ExtractTopicFromValueSchema.class); | ||
|
||
private ExtractTopicFromValueSchemaConfig config; | ||
private Optional<String> regex; | ||
|
||
private Optional<Map<String, String>> schemaNameToTopicMap; | ||
private Pattern pattern; | ||
|
||
@Override | ||
public ConfigDef config() { | ||
return ExtractTopicFromValueSchemaConfig.config(); | ||
} | ||
|
||
@Override | ||
public void configure(final Map<String, ?> configs) { | ||
this.config = new ExtractTopicFromValueSchemaConfig(configs); | ||
schemaNameToTopicMap = config.schemaNameToTopicMap(); | ||
regex = config.regEx(); | ||
if (regex.isPresent()) { | ||
pattern = Pattern.compile(regex.get()); | ||
} | ||
} | ||
|
||
@Override | ||
public R apply(final R record) { | ||
|
||
if (null == record.valueSchema() || null == record.valueSchema().name()) { | ||
throw new DataException(" value schema name can't be null: " + record); | ||
} | ||
// If no extra configs use record.valueSchema().name() -> newTopic | ||
if (null == config) { | ||
return createConnectRecord(record, Optional.ofNullable(record.valueSchema().name()).orElse(record.topic())); | ||
} | ||
// First check schema value name -> desired topic name mapping and use that if it is set. | ||
if (schemaNameToTopicMap.isPresent() && schemaNameToTopicMap.get().containsKey(record.valueSchema().name())) { | ||
return createConnectRecord(record, schemaNameToTopicMap.get().get(record.valueSchema().name())); | ||
} | ||
// Secondly check if regex parsing from schema value name is set and use that. | ||
final Optional<String> regex = config.regEx(); | ||
if (regex.isPresent()) { | ||
final Matcher matcher = pattern.matcher(record.valueSchema().name()); | ||
if (matcher.find() && matcher.groupCount() == 1) { | ||
return createConnectRecord(record, matcher.group(1)); | ||
} | ||
log.trace("No match with pattern {} from schema name {}", pattern.pattern(), record.valueSchema().name()); | ||
} | ||
// If no other configurations are set use value schema name as new topic name. | ||
return createConnectRecord(record, record.valueSchema().name()); | ||
} | ||
|
||
private R createConnectRecord(final R record, final String newTopicName) { | ||
return record.newRecord( | ||
newTopicName, | ||
record.kafkaPartition(), | ||
record.keySchema(), | ||
record.key(), | ||
record.valueSchema(), | ||
record.value(), | ||
record.timestamp(), | ||
record.headers() | ||
); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
|
||
public static class Name<R extends ConnectRecord<R>> extends ExtractTopicFromValueSchema<R> { | ||
|
||
@Override | ||
public void close() { | ||
} | ||
} | ||
} |
Oops, something went wrong.