|
| 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 | + |
| 19 | +package com.dtstack.flink.sql.source.kafka.deserialization; |
| 20 | + |
| 21 | +import com.dtstack.flink.sql.format.FormatType; |
| 22 | +import com.dtstack.flink.sql.format.dtnest.DtNestRowDeserializationSchema; |
| 23 | +import com.dtstack.flink.sql.source.kafka.table.KafkaSourceTableInfo; |
| 24 | +import org.apache.commons.lang3.StringUtils; |
| 25 | +import org.apache.flink.api.common.serialization.DeserializationSchema; |
| 26 | +import org.apache.flink.api.common.typeinfo.TypeInformation; |
| 27 | +import org.apache.flink.formats.avro.AvroRowDeserializationSchema; |
| 28 | +import org.apache.flink.formats.csv.CsvRowDeserializationSchema; |
| 29 | +import org.apache.flink.formats.json.DTJsonRowDeserializationSchema; |
| 30 | +import org.apache.flink.types.Row; |
| 31 | + |
| 32 | +/** |
| 33 | + * Date: 2021/05/25 Company: www.dtstack.com |
| 34 | + * |
| 35 | + * @author tiezhu |
| 36 | + */ |
| 37 | +public class DeserializationSchemaFactory { |
| 38 | + |
| 39 | + public static DeserializationSchema<Row> createDeserializationSchema( |
| 40 | + KafkaSourceTableInfo kafkaSourceTableInfo, TypeInformation<Row> typeInformation) { |
| 41 | + DeserializationSchema<Row> deserializationSchema = null; |
| 42 | + if (FormatType.DT_NEST.name().equalsIgnoreCase(kafkaSourceTableInfo.getSourceDataType())) { |
| 43 | + deserializationSchema = |
| 44 | + new DtNestRowDeserializationSchema( |
| 45 | + typeInformation, |
| 46 | + kafkaSourceTableInfo.getPhysicalFields(), |
| 47 | + kafkaSourceTableInfo.getFieldExtraInfoList(), |
| 48 | + kafkaSourceTableInfo.getCharsetName()); |
| 49 | + } else if (FormatType.JSON |
| 50 | + .name() |
| 51 | + .equalsIgnoreCase(kafkaSourceTableInfo.getSourceDataType())) { |
| 52 | + |
| 53 | + if (StringUtils.isNotBlank(kafkaSourceTableInfo.getSchemaString())) { |
| 54 | + deserializationSchema = |
| 55 | + new DTJsonRowDeserializationSchema(kafkaSourceTableInfo.getSchemaString()); |
| 56 | + } else if (typeInformation != null && typeInformation.getArity() != 0) { |
| 57 | + deserializationSchema = new DTJsonRowDeserializationSchema(typeInformation); |
| 58 | + } else { |
| 59 | + throw new IllegalArgumentException( |
| 60 | + "sourceDataType:" |
| 61 | + + FormatType.JSON.name() |
| 62 | + + " must set schemaString(JSON Schema)or TypeInformation<Row>"); |
| 63 | + } |
| 64 | + |
| 65 | + } else if (FormatType.CSV |
| 66 | + .name() |
| 67 | + .equalsIgnoreCase(kafkaSourceTableInfo.getSourceDataType())) { |
| 68 | + |
| 69 | + if (StringUtils.isBlank(kafkaSourceTableInfo.getFieldDelimiter())) { |
| 70 | + throw new IllegalArgumentException( |
| 71 | + "sourceDataType:" + FormatType.CSV.name() + " must set fieldDelimiter"); |
| 72 | + } |
| 73 | + |
| 74 | + final CsvRowDeserializationSchema.Builder deserSchemaBuilder = |
| 75 | + new CsvRowDeserializationSchema.Builder(typeInformation); |
| 76 | + deserSchemaBuilder.setFieldDelimiter( |
| 77 | + kafkaSourceTableInfo.getFieldDelimiter().toCharArray()[0]); |
| 78 | + deserializationSchema = deserSchemaBuilder.build(); |
| 79 | + |
| 80 | + } else if (FormatType.AVRO |
| 81 | + .name() |
| 82 | + .equalsIgnoreCase(kafkaSourceTableInfo.getSourceDataType())) { |
| 83 | + |
| 84 | + if (StringUtils.isBlank(kafkaSourceTableInfo.getSchemaString())) { |
| 85 | + throw new IllegalArgumentException( |
| 86 | + "sourceDataType:" + FormatType.AVRO.name() + " must set schemaString"); |
| 87 | + } |
| 88 | + |
| 89 | + deserializationSchema = |
| 90 | + new AvroRowDeserializationSchema(kafkaSourceTableInfo.getSchemaString()); |
| 91 | + } |
| 92 | + |
| 93 | + if (null == deserializationSchema) { |
| 94 | + throw new UnsupportedOperationException( |
| 95 | + "FormatType:" + kafkaSourceTableInfo.getSourceDataType()); |
| 96 | + } |
| 97 | + |
| 98 | + return deserializationSchema; |
| 99 | + } |
| 100 | +} |
0 commit comments