-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR introduces a new SMT which allows picking a field from the payload and use it as source for introducing the headers for time-based object key values in Lenses datalakes connectors
- Loading branch information
stheppi
committed
Mar 28, 2024
1 parent
9a5b46a
commit 4dced00
Showing
16 changed files
with
1,655 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.lenses.connect.smt.header; | ||
|
||
enum FieldType { | ||
KEY, | ||
VALUE, | ||
TIMESTAMP | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/io/lenses/connect/smt/header/FieldTypeConstants.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,7 @@ | ||
package io.lenses.connect.smt.header; | ||
|
||
public class FieldTypeConstants { | ||
public static final String KEY_FIELD = "_key"; | ||
public static final String VALUE_FIELD = "_value"; | ||
public static final String TIMESTAMP_FIELD = "_timestamp"; | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/io/lenses/connect/smt/header/FieldTypeUtils.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,54 @@ | ||
package io.lenses.connect.smt.header; | ||
|
||
import java.util.Arrays; | ||
import org.apache.kafka.common.config.ConfigException; | ||
|
||
class FieldTypeUtils { | ||
public static FieldTypeAndFields extractFieldTypeAndFields(String from) { | ||
FieldType fieldType; | ||
String[] fields = from.split("\\."); | ||
if (fields.length > 0) { | ||
if (fields[0].equalsIgnoreCase(FieldTypeConstants.KEY_FIELD)) { | ||
fieldType = FieldType.KEY; | ||
// drop the first element | ||
fields = Arrays.copyOfRange(fields, 1, fields.length); | ||
} else if (fields[0].equalsIgnoreCase(FieldTypeConstants.TIMESTAMP_FIELD)) { | ||
fieldType = FieldType.TIMESTAMP; | ||
// if fields length is > 1, then it is an error since the timestamp is a primitive | ||
if (fields.length > 1) { | ||
throw new ConfigException( | ||
"When using the record timestamp field, the field path should only be '_timestamp'."); | ||
} | ||
fields = new String[0]; | ||
} else { | ||
fieldType = FieldType.VALUE; | ||
if (fields[0].equalsIgnoreCase(FieldTypeConstants.VALUE_FIELD)) { | ||
// drop the first element | ||
fields = Arrays.copyOfRange(fields, 1, fields.length); | ||
} | ||
} | ||
} else { | ||
fieldType = FieldType.VALUE; | ||
} | ||
|
||
return new FieldTypeAndFields(fieldType, fields); | ||
} | ||
|
||
static class FieldTypeAndFields { | ||
private final FieldType fieldType; | ||
private final String[] fields; | ||
|
||
public FieldTypeAndFields(FieldType fieldType, String[] fields) { | ||
this.fieldType = fieldType; | ||
this.fields = fields; | ||
} | ||
|
||
public FieldType getFieldType() { | ||
return fieldType; | ||
} | ||
|
||
public String[] getFields() { | ||
return fields; | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/io/lenses/connect/smt/header/InsertFieldTimestampHeaders.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,51 @@ | ||
/** | ||
* 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 io.lenses.connect.smt.header; | ||
|
||
import java.time.Instant; | ||
import org.apache.kafka.common.config.ConfigDef; | ||
import org.apache.kafka.connect.connector.ConnectRecord; | ||
import org.apache.kafka.connect.transforms.util.SimpleConfig; | ||
|
||
/** | ||
* A transformer which takes a record filed of type timestamp and inserts a header year, month, day, | ||
* hour, minute, second and date. | ||
* | ||
* @param <R> the record type | ||
*/ | ||
public class InsertFieldTimestampHeaders<R extends ConnectRecord<R>> | ||
extends InsertTimestampHeaders<R> { | ||
|
||
public static ConfigDef CONFIG_DEF = | ||
RecordFieldTimestamp.extendConfigDef(InsertTimestampHeaders.CONFIG_DEF); | ||
private RecordFieldTimestamp<R> fieldTimestamp; | ||
|
||
public InsertFieldTimestampHeaders() { | ||
super(InsertRecordTimestampHeaders.CONFIG_DEF); | ||
} | ||
|
||
@Override | ||
protected Instant getInstant(R r) { | ||
return fieldTimestamp.getInstant(r); | ||
} | ||
|
||
@Override | ||
public ConfigDef config() { | ||
return CONFIG_DEF; | ||
} | ||
|
||
@Override | ||
protected void configureInternal(SimpleConfig config) { | ||
|
||
super.configureInternal(config); | ||
fieldTimestamp = RecordFieldTimestamp.create(config, timeZone, locale); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/io/lenses/connect/smt/header/InsertRollingFieldTimestampHeaders.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,51 @@ | ||
/** | ||
* 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 io.lenses.connect.smt.header; | ||
|
||
import java.time.Instant; | ||
import org.apache.kafka.common.config.ConfigDef; | ||
import org.apache.kafka.connect.connector.ConnectRecord; | ||
import org.apache.kafka.connect.transforms.util.SimpleConfig; | ||
|
||
/** | ||
* A transformer which takes a record filed of type timestamp and inserts a header year, month, day, | ||
* hour, minute, second and date. | ||
* | ||
* @param <R> the record type | ||
*/ | ||
public class InsertRollingFieldTimestampHeaders<R extends ConnectRecord<R>> | ||
extends InsertRollingTimestampHeaders<R> { | ||
|
||
private RecordFieldTimestamp<R> fieldTimestamp; | ||
|
||
public static ConfigDef CONFIG_DEF = | ||
RecordFieldTimestamp.extendConfigDef(InsertRollingTimestampHeaders.CONFIG_DEF); | ||
|
||
public InsertRollingFieldTimestampHeaders() { | ||
super(); | ||
} | ||
|
||
@Override | ||
protected Instant getInstantInternal(R r) { | ||
return fieldTimestamp.getInstant(r); | ||
} | ||
|
||
@Override | ||
public ConfigDef config() { | ||
return CONFIG_DEF; | ||
} | ||
|
||
@Override | ||
protected void configureInternal(SimpleConfig config) { | ||
super.configureInternal(config); | ||
fieldTimestamp = RecordFieldTimestamp.create(config, timeZone, locale); | ||
} | ||
} |
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.