Skip to content

Commit 53718d7

Browse files
Added transformation to copy the schema name to the topic. Fixes #48. (#49)
1 parent 0abfc24 commit 53718d7

2 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright © 2019 Jeremy Custenborder (jcustenborder@gmail.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.jcustenborder.kafka.connect.transform.common;
17+
18+
import com.github.jcustenborder.kafka.connect.utils.config.Description;
19+
import com.github.jcustenborder.kafka.connect.utils.config.Title;
20+
import org.apache.kafka.common.config.ConfigDef;
21+
import org.apache.kafka.connect.connector.ConnectRecord;
22+
import org.apache.kafka.connect.transforms.Transformation;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
import java.util.Map;
27+
28+
@Title("SchemaNameToTopic")
29+
@Description("This transformation is used to take the name from the schema for the key or value and" +
30+
" replace the topic with this value.")
31+
public abstract class SchemaNameToTopic<R extends ConnectRecord<R>> implements Transformation<R> {
32+
private static final Logger log = LoggerFactory.getLogger(SchemaNameToTopic.class);
33+
34+
@Override
35+
public ConfigDef config() {
36+
return new ConfigDef();
37+
}
38+
39+
@Override
40+
public void close() {
41+
42+
}
43+
44+
@Override
45+
public void configure(Map<String, ?> map) {
46+
47+
}
48+
49+
public static class Key<R extends ConnectRecord<R>> extends SchemaNameToTopic<R> {
50+
@Override
51+
public R apply(R r) {
52+
53+
return r.newRecord(
54+
r.keySchema().name(),
55+
r.kafkaPartition(),
56+
r.keySchema(),
57+
r.key(),
58+
r.valueSchema(),
59+
r.value(),
60+
r.timestamp(),
61+
r.headers()
62+
);
63+
}
64+
}
65+
66+
67+
public static class Value<R extends ConnectRecord<R>> extends SchemaNameToTopic<R> {
68+
@Override
69+
public R apply(R r) {
70+
71+
return r.newRecord(
72+
r.valueSchema().name(),
73+
r.kafkaPartition(),
74+
r.keySchema(),
75+
r.key(),
76+
r.valueSchema(),
77+
r.value(),
78+
r.timestamp(),
79+
r.headers()
80+
);
81+
}
82+
}
83+
84+
85+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.github.jcustenborder.kafka.connect.transform.common;
2+
3+
import com.google.common.base.Strings;
4+
import org.apache.kafka.connect.data.Field;
5+
import org.apache.kafka.connect.data.Schema;
6+
import org.apache.kafka.connect.data.SchemaBuilder;
7+
import org.apache.kafka.connect.data.Struct;
8+
import org.apache.kafka.connect.sink.SinkRecord;
9+
import org.apache.kafka.connect.transforms.Transformation;
10+
import org.junit.jupiter.api.Test;
11+
12+
import java.util.Arrays;
13+
import java.util.LinkedHashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertNotNull;
19+
20+
public class SchemaNameToTopicTest {
21+
Transformation<SinkRecord> transformation = new SchemaNameToTopic.Value<>();
22+
SinkRecord exampleRecord(Schema schema) {
23+
Struct struct = new Struct(schema);
24+
for (Field field : schema.fields()) {
25+
struct.put(field, Strings.repeat("x", 50));
26+
}
27+
return new SinkRecord(
28+
"test",
29+
0,
30+
null,
31+
null,
32+
schema,
33+
struct,
34+
1234L
35+
);
36+
37+
}
38+
39+
Schema exampleSchema(List<String> fieldNames, final int version) {
40+
SchemaBuilder builder = SchemaBuilder.struct()
41+
.name(this.getClass().getName());
42+
for (String fieldName : fieldNames) {
43+
builder.field(fieldName, Schema.STRING_SCHEMA);
44+
}
45+
builder.version(version);
46+
return builder.build();
47+
}
48+
49+
@Test
50+
public void apply() {
51+
Schema schema = SchemaBuilder.struct()
52+
.name("com.foo.bar.whatever.ASDF")
53+
.field("firstName", Schema.OPTIONAL_STRING_SCHEMA)
54+
.build();
55+
SinkRecord input = exampleRecord(schema);
56+
SinkRecord actual = this.transformation.apply(input);
57+
assertNotNull(actual);
58+
assertEquals(schema.name(), actual.topic());
59+
60+
61+
}
62+
63+
64+
}

0 commit comments

Comments
 (0)