curcur commented on a change in pull request #11725: URL: https://github.com/apache/flink/pull/11725#discussion_r426343364
########## File path: flink-connectors/flink-connector-kafka/src/main/java/org/apache/flink/streaming/connectors/kafka/shuffle/FlinkKafkaShuffle.java ########## @@ -0,0 +1,351 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.streaming.connectors.kafka.shuffle; + +import org.apache.flink.annotation.Experimental; +import org.apache.flink.api.common.operators.Keys; +import org.apache.flink.api.common.serialization.TypeInformationSerializationSchema; +import org.apache.flink.api.common.typeinfo.BasicArrayTypeInfo; +import org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.java.functions.KeySelector; +import org.apache.flink.api.java.tuple.Tuple; +import org.apache.flink.runtime.state.KeyGroupRangeAssignment; +import org.apache.flink.streaming.api.TimeCharacteristic; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.streaming.api.datastream.DataStreamUtils; +import org.apache.flink.streaming.api.datastream.KeyedStream; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.streaming.api.functions.source.SourceFunction; +import org.apache.flink.streaming.api.transformations.SinkTransformation; +import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer; +import org.apache.flink.streaming.util.keys.KeySelectorUtil; +import org.apache.flink.util.Preconditions; +import org.apache.flink.util.PropertiesUtil; + +import java.util.Properties; + +/** + * {@link FlinkKafkaShuffle} uses Kafka as a message bus to shuffle and persist data at the same time. + * + * <p>Persisting shuffle data is useful when + * - you would like to reuse the shuffle data and/or, + * - you would like to avoid a full restart of a pipeline during failure recovery + * + * <p>Persisting shuffle is achieved by wrapping a {@link FlinkKafkaShuffleProducer} and + * a {@link FlinkKafkaShuffleConsumer} together into a {@link FlinkKafkaShuffle}. + * Here is an example how to use a {@link FlinkKafkaShuffle}. + * + * <p><pre>{@code + * StreamExecutionEnvironment env = ... // create execution environment + * DataStream<X> source = env.addSource(...) // add data stream source + * DataStream<Y> dataStream = ... // some transformation(s) based on source + * + * KeyedStream<Y, KEY> keyedStream = FlinkKafkaShuffle + * .persistentKeyBy( // keyBy shuffle through kafka + * dataStream, // data stream to be shuffled + * topic, // Kafka topic written to + * producerParallelism, // the number of tasks of a Kafka Producer + * numberOfPartitions, // the number of partitions of the Kafka topic written to + * kafkaProperties, // kafka properties for Kafka Producer and Consumer + * keySelector<Y, KEY>); // key selector to retrieve key from `dataStream' + * + * keyedStream.transform... // some other transformation(s) + * + * KeyedStream<Y, KEY> keyedStreamReuse = FlinkKafkaShuffle + * .readKeyBy( // Read the Kafka shuffle data again for other usages + * topic, // the topic of Kafka where data is persisted + * env, // execution environment, and it can be a new environment + * typeInformation<Y>, // type information of the data persisted in Kafka + * kafkaProperties, // kafka properties for Kafka Consumer + * keySelector<Y, KEY>); // key selector to retrieve key + * + * keyedStreamReuse.transform... // some other transformation(s) + * }</pre> + * + * <p>Usage of {@link FlinkKafkaShuffle#persistentKeyBy} is similar to {@link DataStream#keyBy(KeySelector)}. + * The differences are: + * + * <p>1). Partitioning is done through {@link FlinkKafkaShuffleProducer}. {@link FlinkKafkaShuffleProducer} decides + * which partition a key goes when writing to Kafka + * + * <p>2). Shuffle data can be reused through {@link FlinkKafkaShuffle#readKeyBy}, as shown in the example above. Review comment: I can change reuse -> read again? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org