|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package com.dtstack.flink.sql.source.kafka.deserializer; |
| 19 | + |
| 20 | +import com.dtstack.flink.sql.source.kafka.table.KafkaSourceTableInfo; |
| 21 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | +import org.apache.kafka.common.errors.SerializationException; |
| 23 | +import org.apache.kafka.common.header.Headers; |
| 24 | +import org.apache.kafka.common.serialization.Deserializer; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +/** |
| 31 | + * Date: 2021/04/20 |
| 32 | + * Company: www.dtstack.com |
| 33 | + * |
| 34 | + * @author tudou |
| 35 | + */ |
| 36 | +public class DtKafkaDeserializer<T> implements Deserializer<byte[]> { |
| 37 | + protected ObjectMapper objectMapper = new ObjectMapper(); |
| 38 | + private Deserializer<T> deserializer; |
| 39 | + |
| 40 | + @Override |
| 41 | + @SuppressWarnings("unchecked") |
| 42 | + public void configure(Map<String, ?> configs, boolean isKey) { |
| 43 | + String deserializerClassName; |
| 44 | + if(isKey){ |
| 45 | + deserializerClassName = (String)configs.get(KafkaSourceTableInfo.DT_KEY_DESERIALIZER); |
| 46 | + }else{ |
| 47 | + deserializerClassName = (String)configs.get(KafkaSourceTableInfo.DT_VALUE_DESERIALIZER); |
| 48 | + } |
| 49 | + try { |
| 50 | + this.deserializer = (Deserializer<T>)Class.forName(deserializerClassName).newInstance(); |
| 51 | + } catch (Exception e) { |
| 52 | + throw new RuntimeException("Can't create instance: " + deserializerClassName, e); |
| 53 | + } |
| 54 | + this.deserializer.configure(configs, isKey); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public byte[] deserialize(String topic, Headers headers, byte[] data) { |
| 59 | + return toBytes(deserializer.deserialize(topic, headers, data)); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public byte[] deserialize(String topic, byte[] data) { |
| 64 | + return toBytes(deserializer.deserialize(topic, data)); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * T value to byte[] |
| 69 | + * @param value |
| 70 | + * @return |
| 71 | + */ |
| 72 | + private byte[] toBytes(T value){ |
| 73 | + if(value instanceof byte[]){ |
| 74 | + return (byte[])value; |
| 75 | + }else{ |
| 76 | + try { |
| 77 | + return this.objectMapper.readValue(this.objectMapper.writeValueAsBytes(value), String.class).getBytes(StandardCharsets.UTF_8); |
| 78 | + } catch (IOException e) { |
| 79 | + throw new SerializationException("Can't deserialize data", e); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void close() { |
| 86 | + this.deserializer.close(); |
| 87 | + } |
| 88 | +} |
0 commit comments