|
| 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.sink.kafka; |
| 20 | + |
| 21 | +import com.dtstack.flink.sql.sink.IStreamSinkGener; |
| 22 | +import com.dtstack.flink.sql.sink.kafka.table.KafkaSinkTableInfo; |
| 23 | +import com.dtstack.flink.sql.table.TargetTableInfo; |
| 24 | +import org.apache.flink.api.common.serialization.SerializationSchema; |
| 25 | +import org.apache.flink.api.common.typeinfo.TypeInformation; |
| 26 | +import org.apache.flink.api.java.typeutils.RowTypeInfo; |
| 27 | +import org.apache.flink.formats.json.JsonRowSerializationSchema; |
| 28 | +import org.apache.flink.streaming.api.datastream.DataStream; |
| 29 | +import org.apache.flink.streaming.connectors.kafka.KafkaTableSink; |
| 30 | +import org.apache.flink.table.sinks.AppendStreamTableSink; |
| 31 | +import org.apache.flink.table.sinks.TableSink; |
| 32 | +import org.apache.flink.types.Row; |
| 33 | +import java.util.Properties; |
| 34 | + |
| 35 | +/** |
| 36 | + * Date: 2018/12/18 |
| 37 | + * Company: www.dtstack.com |
| 38 | + * |
| 39 | + * @author DocLi |
| 40 | + * @modifyer maqi |
| 41 | + */ |
| 42 | +public class KafkaSink implements AppendStreamTableSink<Row>, IStreamSinkGener<KafkaSink> { |
| 43 | + |
| 44 | + protected String[] fieldNames; |
| 45 | + |
| 46 | + protected TypeInformation<?>[] fieldTypes; |
| 47 | + |
| 48 | + protected String topic; |
| 49 | + |
| 50 | + protected Properties properties; |
| 51 | + |
| 52 | + /** Serialization schema for encoding records to Kafka. */ |
| 53 | + protected SerializationSchema serializationSchema; |
| 54 | + |
| 55 | + @Override |
| 56 | + public KafkaSink genStreamSink(TargetTableInfo targetTableInfo) { |
| 57 | + KafkaSinkTableInfo kafka09SinkTableInfo = (KafkaSinkTableInfo) targetTableInfo; |
| 58 | + this.topic = kafka09SinkTableInfo.getTopic(); |
| 59 | + this.fieldNames = kafka09SinkTableInfo.getFields(); |
| 60 | + TypeInformation[] types = new TypeInformation[kafka09SinkTableInfo.getFields().length]; |
| 61 | + for (int i = 0; i < kafka09SinkTableInfo.getFieldClasses().length; i++) { |
| 62 | + types[i] = TypeInformation.of(kafka09SinkTableInfo.getFieldClasses()[i]); |
| 63 | + } |
| 64 | + this.fieldTypes = types; |
| 65 | + |
| 66 | + properties = new Properties(); |
| 67 | + properties.setProperty("bootstrap.servers", kafka09SinkTableInfo.getBootstrapServers()); |
| 68 | + |
| 69 | + this.serializationSchema = new JsonRowSerializationSchema(getOutputType()); |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void emitDataStream(DataStream<Row> dataStream) { |
| 75 | + KafkaTableSink kafkaTableSink = new CustomerKafka09JsonTableSink( |
| 76 | + topic, |
| 77 | + properties, |
| 78 | + serializationSchema |
| 79 | + ); |
| 80 | + |
| 81 | + kafkaTableSink.emitDataStream(dataStream); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public TypeInformation<Row> getOutputType() { |
| 86 | + return new RowTypeInfo(fieldTypes, fieldNames); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String[] getFieldNames() { |
| 91 | + return fieldNames; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public TypeInformation<?>[] getFieldTypes() { |
| 96 | + return fieldTypes; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public TableSink<Row> configure(String[] fieldNames, TypeInformation<?>[] fieldTypes) { |
| 101 | + this.fieldNames = fieldNames; |
| 102 | + this.fieldTypes = fieldTypes; |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments