|
| 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 | + * <p> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p> |
| 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.sink.kafka; |
| 19 | + |
| 20 | +import org.apache.flink.api.common.serialization.SerializationSchema; |
| 21 | +import org.apache.flink.streaming.api.datastream.DataStream; |
| 22 | +import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer010; |
| 23 | +import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducerBase; |
| 24 | +import org.apache.flink.streaming.connectors.kafka.Kafka09JsonTableSink; |
| 25 | +import org.apache.flink.streaming.connectors.kafka.KafkaJsonTableSink; |
| 26 | +import org.apache.flink.streaming.connectors.kafka.partitioner.FlinkFixedPartitioner; |
| 27 | +import org.apache.flink.streaming.connectors.kafka.partitioner.FlinkKafkaDelegatePartitioner; |
| 28 | +import org.apache.flink.streaming.connectors.kafka.partitioner.FlinkKafkaPartitioner; |
| 29 | +import org.apache.flink.streaming.connectors.kafka.partitioner.KafkaPartitioner; |
| 30 | +import org.apache.flink.table.util.TableConnectorUtil; |
| 31 | +import org.apache.flink.types.Row; |
| 32 | + |
| 33 | +import java.util.Properties; |
| 34 | + |
| 35 | +/** |
| 36 | + * Reason: add schema info |
| 37 | + * Date: 2019/4/8 |
| 38 | + * Company: www.dtstack.com |
| 39 | + * |
| 40 | + * @author maqi |
| 41 | + */ |
| 42 | +public class CustomerKafka10JsonTableSink extends KafkaJsonTableSink { |
| 43 | + |
| 44 | + |
| 45 | + protected SerializationSchema schema; |
| 46 | + |
| 47 | + public CustomerKafka10JsonTableSink(String topic, Properties properties, SerializationSchema schema) { |
| 48 | + super(topic, properties, new FlinkFixedPartitioner<>()); |
| 49 | + this.schema = schema; |
| 50 | + } |
| 51 | + |
| 52 | + public CustomerKafka10JsonTableSink(String topic, Properties properties, FlinkKafkaPartitioner<Row> partitioner, SerializationSchema schema) { |
| 53 | + super(topic, properties, partitioner); |
| 54 | + this.schema = schema; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + @Deprecated |
| 59 | + public CustomerKafka10JsonTableSink(String topic, Properties properties, KafkaPartitioner<Row> partitioner, SerializationSchema schema) { |
| 60 | + super(topic, properties, new FlinkKafkaDelegatePartitioner<>(partitioner)); |
| 61 | + this.schema = schema; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected FlinkKafkaProducerBase<Row> createKafkaProducer(String topic, Properties properties, SerializationSchema<Row> serializationSchema, FlinkKafkaPartitioner<Row> partitioner) { |
| 66 | + return new FlinkKafkaProducer010<Row>(topic, serializationSchema, properties, partitioner); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected Kafka09JsonTableSink createCopy() { |
| 71 | + return new Kafka09JsonTableSink(topic, properties, partitioner); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void emitDataStream(DataStream<Row> dataStream) { |
| 76 | + FlinkKafkaProducerBase<Row> kafkaProducer = createKafkaProducer(topic, properties, schema, partitioner); |
| 77 | + // always enable flush on checkpoint to achieve at-least-once if query runs with checkpointing enabled. |
| 78 | + kafkaProducer.setFlushOnCheckpoint(true); |
| 79 | + dataStream.addSink(kafkaProducer).name(TableConnectorUtil.generateRuntimeName(this.getClass(), fieldNames)); |
| 80 | + } |
| 81 | +} |
0 commit comments