|
37 | 37 | public class JsonDebeziumDeserializationSchema implements DebeziumDeserializationSchema<String> {
|
38 | 38 |
|
39 | 39 | private static final long serialVersionUID = 1L;
|
40 |
| - private static final JsonConverter CONVERTER = new JsonConverter(); |
| 40 | + |
| 41 | + private transient JsonConverter jsonConverter; |
| 42 | + |
| 43 | + /** |
| 44 | + * Configuration whether to enable {@link JsonConverterConfig.SCHEMAS_ENABLE_CONFIG} to include |
| 45 | + * schema in messages. |
| 46 | + */ |
| 47 | + private final Boolean includeSchema; |
41 | 48 |
|
42 | 49 | public JsonDebeziumDeserializationSchema() {
|
43 | 50 | this(false);
|
44 | 51 | }
|
45 | 52 |
|
46 |
| - public JsonDebeziumDeserializationSchema(boolean includeSchema) { |
47 |
| - final HashMap<String, Object> configs = new HashMap<>(); |
48 |
| - configs.put(ConverterConfig.TYPE_CONFIG, ConverterType.VALUE.getName()); |
49 |
| - configs.put(JsonConverterConfig.SCHEMAS_ENABLE_CONFIG, includeSchema); |
50 |
| - CONVERTER.configure(configs); |
| 53 | + public JsonDebeziumDeserializationSchema(Boolean includeSchema) { |
| 54 | + this.includeSchema = includeSchema; |
51 | 55 | }
|
52 | 56 |
|
53 | 57 | @Override
|
54 | 58 | public void deserialize(SourceRecord record, Collector<String> out) throws Exception {
|
| 59 | + if (jsonConverter == null) { |
| 60 | + // initialize jsonConverter |
| 61 | + jsonConverter = new JsonConverter(); |
| 62 | + final HashMap<String, Object> configs = new HashMap<>(2); |
| 63 | + configs.put(ConverterConfig.TYPE_CONFIG, ConverterType.VALUE.getName()); |
| 64 | + configs.put(JsonConverterConfig.SCHEMAS_ENABLE_CONFIG, includeSchema); |
| 65 | + jsonConverter.configure(configs); |
| 66 | + } |
55 | 67 | byte[] bytes =
|
56 |
| - CONVERTER.fromConnectData(record.topic(), record.valueSchema(), record.value()); |
| 68 | + jsonConverter.fromConnectData(record.topic(), record.valueSchema(), record.value()); |
57 | 69 | out.collect(new String(bytes));
|
58 | 70 | }
|
59 | 71 |
|
|
0 commit comments