All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.kafka.connect.transforms.TimestampRouter Maven / Gradle / Ivy

There is a newer version: 0.10.2.0-bkbase.3
Show newest version
/**
 * 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.kafka.connect.transforms; 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.util.SimpleConfig; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import java.util.TimeZone; public class TimestampRouter> implements Transformation { public static final String OVERVIEW_DOC = "Update the record's topic field as a function of the original topic value and the record timestamp." + "

" + "This is mainly useful for sink connectors, since the topic field is often used to determine the equivalent entity name in the destination system" + "(e.g. database table or search index name)."; public static final ConfigDef CONFIG_DEF = new ConfigDef() .define(ConfigName.TOPIC_FORMAT, ConfigDef.Type.STRING, "${topic}-${timestamp}", ConfigDef.Importance.HIGH, "Format string which can contain ${topic} and ${timestamp} as placeholders for the topic and timestamp, respectively.") .define(ConfigName.TIMESTAMP_FORMAT, ConfigDef.Type.STRING, "yyyyMMdd", ConfigDef.Importance.HIGH, "Format string for the timestamp that is compatible with java.text.SimpleDateFormat."); private interface ConfigName { String TOPIC_FORMAT = "topic.format"; String TIMESTAMP_FORMAT = "timestamp.format"; } private String topicFormat; private ThreadLocal timestampFormat; @Override public void configure(Map props) { final SimpleConfig config = new SimpleConfig(CONFIG_DEF, props); topicFormat = config.getString(ConfigName.TOPIC_FORMAT); final String timestampFormatStr = config.getString(ConfigName.TIMESTAMP_FORMAT); timestampFormat = new ThreadLocal() { @Override protected SimpleDateFormat initialValue() { final SimpleDateFormat fmt = new SimpleDateFormat(timestampFormatStr); fmt.setTimeZone(TimeZone.getTimeZone("UTC")); return fmt; } }; } @Override public R apply(R record) { final Long timestamp = record.timestamp(); if (timestamp == null) { throw new DataException("Timestamp missing on record: " + record); } final String formattedTimestamp = timestampFormat.get().format(new Date(timestamp)); final String updatedTopic = topicFormat.replace("${topic}", record.topic()).replace("${timestamp}", formattedTimestamp); return record.newRecord( updatedTopic, record.kafkaPartition(), record.keySchema(), record.key(), record.valueSchema(), record.value(), record.timestamp() ); } @Override public void close() { timestampFormat = null; } @Override public ConfigDef config() { return CONFIG_DEF; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy