|
15 | 15 | */
|
16 | 16 | package com.github.jcustenborder.kafka.connect.transform.common;
|
17 | 17 |
|
| 18 | +import com.github.jcustenborder.kafka.connect.utils.data.SchemaHelper; |
18 | 19 | import org.apache.kafka.connect.connector.ConnectRecord;
|
| 20 | +import org.apache.kafka.connect.data.Decimal; |
| 21 | +import org.apache.kafka.connect.data.Schema; |
19 | 22 | import org.apache.kafka.connect.data.SchemaAndValue;
|
20 | 23 | import org.apache.kafka.connect.data.Struct;
|
| 24 | +import org.apache.kafka.connect.data.Time; |
| 25 | +import org.apache.kafka.connect.data.Timestamp; |
21 | 26 | import org.apache.kafka.connect.transforms.Transformation;
|
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
22 | 29 |
|
| 30 | +import java.math.BigDecimal; |
| 31 | +import java.util.Date; |
| 32 | +import java.util.List; |
23 | 33 | import java.util.Map;
|
24 | 34 |
|
25 | 35 | public abstract class BaseTransformation<R extends ConnectRecord<R>> implements Transformation<R> {
|
| 36 | + private static final Logger log = LoggerFactory.getLogger(BaseTransformation.class); |
26 | 37 |
|
27 |
| - protected abstract SchemaAndValue processStruct(R record, SchemaAndValue schemaAndValue); |
| 38 | + protected SchemaAndValue processMap(R record, Map<String, Object> input) { |
| 39 | + throw new UnsupportedOperationException("MAP is not a supported type."); |
| 40 | + } |
| 41 | + |
| 42 | + protected SchemaAndValue processStruct(R record, Schema inputSchema, Struct input) { |
| 43 | + throw new UnsupportedOperationException("STRUCT is not a supported type."); |
| 44 | + } |
| 45 | + |
| 46 | + protected SchemaAndValue processString(R record, Schema inputSchema, String input) { |
| 47 | + throw new UnsupportedOperationException("STRING is not a supported type."); |
| 48 | + } |
| 49 | + |
| 50 | + protected SchemaAndValue processBytes(R record, Schema inputSchema, byte[] input) { |
| 51 | + throw new UnsupportedOperationException("BYTES is not a supported type."); |
| 52 | + } |
| 53 | + |
| 54 | + protected SchemaAndValue processInt8(R record, Schema inputSchema, byte input) { |
| 55 | + throw new UnsupportedOperationException("INT8 is not a supported type."); |
| 56 | + } |
| 57 | + |
| 58 | + protected SchemaAndValue processInt16(R record, Schema inputSchema, short input) { |
| 59 | + throw new UnsupportedOperationException("INT16 is not a supported type."); |
| 60 | + } |
28 | 61 |
|
29 |
| - protected abstract SchemaAndValue processMap(R record, SchemaAndValue schemaAndValue); |
| 62 | + protected SchemaAndValue processInt32(R record, Schema inputSchema, int input) { |
| 63 | + throw new UnsupportedOperationException("INT32 is not a supported type."); |
| 64 | + } |
| 65 | + |
| 66 | + protected SchemaAndValue processInt64(R record, Schema inputSchema, long input) { |
| 67 | + throw new UnsupportedOperationException("INT64 is not a supported type."); |
| 68 | + } |
| 69 | + |
| 70 | + protected SchemaAndValue processBoolean(R record, Schema inputSchema, boolean input) { |
| 71 | + throw new UnsupportedOperationException("BOOLEAN is not a supported type."); |
| 72 | + } |
| 73 | + |
| 74 | + protected SchemaAndValue processTimestamp(R record, Schema inputSchema, Date input) { |
| 75 | + throw new UnsupportedOperationException("Timestamp is not a supported type."); |
| 76 | + } |
| 77 | + |
| 78 | + protected SchemaAndValue processDate(R record, Schema inputSchema, Date input) { |
| 79 | + throw new UnsupportedOperationException("Date is not a supported type."); |
| 80 | + } |
| 81 | + |
| 82 | + protected SchemaAndValue processTime(R record, Schema inputSchema, Date input) { |
| 83 | + throw new UnsupportedOperationException("Time is not a supported type."); |
| 84 | + } |
30 | 85 |
|
31 |
| - protected SchemaAndValue process(R record, SchemaAndValue schemaAndValue) { |
| 86 | + protected SchemaAndValue processDecimal(R record, Schema inputSchema, BigDecimal input) { |
| 87 | + throw new UnsupportedOperationException("Decimal is not a supported type."); |
| 88 | + } |
| 89 | + |
| 90 | + protected SchemaAndValue processFloat64(R record, Schema inputSchema, double input) { |
| 91 | + throw new UnsupportedOperationException("FLOAT64 is not a supported type."); |
| 92 | + } |
| 93 | + |
| 94 | + protected SchemaAndValue processFloat32(R record, Schema inputSchema, float input) { |
| 95 | + throw new UnsupportedOperationException("FLOAT32 is not a supported type."); |
| 96 | + } |
| 97 | + |
| 98 | + protected SchemaAndValue processArray(R record, Schema inputSchema, List<Object> input) { |
| 99 | + throw new UnsupportedOperationException("ARRAY is not a supported type."); |
| 100 | + } |
| 101 | + |
| 102 | + protected SchemaAndValue processMap(R record, Schema inputSchema, Map<Object, Object> input) { |
| 103 | + throw new UnsupportedOperationException("MAP is not a supported type."); |
| 104 | + } |
| 105 | + |
| 106 | + private static final Schema OPTIONAL_TIMESTAMP = Timestamp.builder().optional().build(); |
| 107 | + |
| 108 | + protected SchemaAndValue process(R record, Schema inputSchema, Object input) { |
32 | 109 | final SchemaAndValue result;
|
33 |
| - if (schemaAndValue.value() instanceof Struct) { |
34 |
| - result = processStruct(record, schemaAndValue); |
35 |
| - } else if (schemaAndValue.value() instanceof Map) { |
36 |
| - result = processMap(record, schemaAndValue); |
| 110 | + |
| 111 | + if (null == inputSchema && null == input) { |
| 112 | + return new SchemaAndValue( |
| 113 | + null, |
| 114 | + null |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + if (input instanceof Map) { |
| 119 | + log.trace("process() - Processing as map"); |
| 120 | + result = processMap(record, (Map<String, Object>) input); |
| 121 | + return result; |
| 122 | + } |
| 123 | + |
| 124 | + if (null == inputSchema) { |
| 125 | + log.trace("process() - Determining schema"); |
| 126 | + inputSchema = SchemaHelper.schema(input); |
| 127 | + } |
| 128 | + |
| 129 | + log.trace("process() - Input has as schema. schema = {}", inputSchema); |
| 130 | + if (Schema.Type.STRUCT == inputSchema.type()) { |
| 131 | + result = processStruct(record, inputSchema, (Struct) input); |
| 132 | + } else if (Timestamp.LOGICAL_NAME.equals(inputSchema.name())) { |
| 133 | + result = processTimestamp(record, inputSchema, (Date) input); |
| 134 | + } else if (org.apache.kafka.connect.data.Date.LOGICAL_NAME.equals(inputSchema.name())) { |
| 135 | + result = processDate(record, inputSchema, (Date) input); |
| 136 | + } else if (Time.LOGICAL_NAME.equals(inputSchema.name())) { |
| 137 | + result = processTime(record, inputSchema, (Date) input); |
| 138 | + } else if (Decimal.LOGICAL_NAME.equals(inputSchema.name())) { |
| 139 | + result = processDecimal(record, inputSchema, (BigDecimal) input); |
| 140 | + } else if (Schema.Type.STRING == inputSchema.type()) { |
| 141 | + result = processString(record, inputSchema, (String) input); |
| 142 | + } else if (Schema.Type.BYTES == inputSchema.type()) { |
| 143 | + result = processBytes(record, inputSchema, (byte[]) input); |
| 144 | + } else if (Schema.Type.INT8 == inputSchema.type()) { |
| 145 | + result = processInt8(record, inputSchema, (byte) input); |
| 146 | + } else if (Schema.Type.INT16 == inputSchema.type()) { |
| 147 | + result = processInt16(record, inputSchema, (short) input); |
| 148 | + } else if (Schema.Type.INT32 == inputSchema.type()) { |
| 149 | + result = processInt32(record, inputSchema, (int) input); |
| 150 | + } else if (Schema.Type.INT64 == inputSchema.type()) { |
| 151 | + result = processInt64(record, inputSchema, (long) input); |
| 152 | + } else if (Schema.Type.FLOAT32 == inputSchema.type()) { |
| 153 | + result = processFloat32(record, inputSchema, (float) input); |
| 154 | + } else if (Schema.Type.FLOAT64 == inputSchema.type()) { |
| 155 | + result = processFloat64(record, inputSchema, (double) input); |
| 156 | + } else if (Schema.Type.ARRAY == inputSchema.type()) { |
| 157 | + result = processArray(record, inputSchema, (List<Object>) input); |
| 158 | + } else if (Schema.Type.MAP == inputSchema.type()) { |
| 159 | + result = processMap(record, inputSchema, (Map<Object, Object>) input); |
37 | 160 | } else {
|
38 |
| - throw new UnsupportedOperationException(); |
| 161 | + throw new UnsupportedOperationException( |
| 162 | + String.format( |
| 163 | + "Schema is not supported. type='%s' name='%s'", |
| 164 | + inputSchema.type(), |
| 165 | + inputSchema.name() |
| 166 | + ) |
| 167 | + ); |
39 | 168 | }
|
| 169 | + |
40 | 170 | return result;
|
41 | 171 | }
|
| 172 | + |
| 173 | + |
42 | 174 | }
|
0 commit comments