|
| 1 | +/** |
| 2 | + * Copyright © 2017 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.fasterxml.jackson.databind.SerializationFeature; |
| 19 | +import com.github.jcustenborder.kafka.connect.utils.jackson.ObjectMapperFactory; |
| 20 | +import org.apache.kafka.common.config.ConfigDef; |
| 21 | +import org.apache.kafka.connect.connector.ConnectRecord; |
| 22 | +import org.apache.kafka.connect.sink.SinkRecord; |
| 23 | +import org.apache.kafka.connect.source.SourceRecord; |
| 24 | +import org.apache.kafka.connect.transforms.Transformation; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import java.util.LinkedHashMap; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +public class Debug<R extends ConnectRecord<R>> implements Transformation<R> { |
| 32 | + private static final Logger log = LoggerFactory.getLogger(Debug.class); |
| 33 | + |
| 34 | + void addConnectRecord(Map<String, Object> debugContent, ConnectRecord r) { |
| 35 | + debugContent.put("topic", r.topic()); |
| 36 | + debugContent.put("kafkaPartition", r.kafkaPartition()); |
| 37 | + debugContent.put("timestamp", r.timestamp()); |
| 38 | + } |
| 39 | + |
| 40 | + void addSinkRecord(Map<String, Object> debugContent, SinkRecord r) { |
| 41 | + debugContent.put("timestampType", r.timestampType()); |
| 42 | + debugContent.put("kafkaOffset", r.kafkaOffset()); |
| 43 | + } |
| 44 | + |
| 45 | + void addSourceRecord(Map<String, Object> debugContent, SourceRecord r) { |
| 46 | + debugContent.put("sourcePartition", r.sourcePartition()); |
| 47 | + debugContent.put("sourceOffset", r.sourceOffset()); |
| 48 | + } |
| 49 | + |
| 50 | + void addKey(Map<String, Object> debugContent, R record) { |
| 51 | + Object result = record.key(); |
| 52 | + debugContent.put("key", result); |
| 53 | + } |
| 54 | + |
| 55 | + void addValue(Map<String, Object> debugContent, R record) { |
| 56 | + Object result = record.value(); |
| 57 | + debugContent.put("value", result); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public R apply(R r) { |
| 62 | + try { |
| 63 | + Map<String, Object> debugContent = new LinkedHashMap<>(); |
| 64 | + addConnectRecord(debugContent, r); |
| 65 | + if (r instanceof SinkRecord) { |
| 66 | + SinkRecord sinkRecord = (SinkRecord) r; |
| 67 | + addSinkRecord(debugContent, sinkRecord); |
| 68 | + } else if (r instanceof SourceRecord) { |
| 69 | + SourceRecord sourceRecord = (SourceRecord) r; |
| 70 | + addSourceRecord(debugContent, sourceRecord); |
| 71 | + } |
| 72 | + addKey(debugContent, r); |
| 73 | + addValue(debugContent, r); |
| 74 | + log.info("\n{}", ObjectMapperFactory.INSTANCE.writeValueAsString(debugContent)); |
| 75 | + } catch (Exception ex) { |
| 76 | + log.error("Exception while generating debug content.", ex); |
| 77 | + } |
| 78 | + |
| 79 | + return r; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public ConfigDef config() { |
| 84 | + return new ConfigDef(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void close() { |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void configure(Map<String, ?> settings) { |
| 94 | + ObjectMapperFactory.INSTANCE.configure(SerializationFeature.INDENT_OUTPUT, true); |
| 95 | + } |
| 96 | +} |
0 commit comments