实际使用你肯定不会是console producer吧。或者你换java代码写kafka,方便控制些。

wei_yuze <wei_y...@qq.com.invalid> 于2023年2月8日周三 13:30写道:
>
> 非常感谢各位的回答!
>
>
>
> Weihua和飞雨正确定位出了问题。问题出在Flink 并发数大于Kafka分区数,导致部分Flink task slot 
> 接收不到数据,进而导致watermark(取所有task slot的最小值)无法推进。
>
>
> 我尝试了Weihua提供的两个解决方案后都可以推进watermark求得窗口聚合结果。
>
>
> 后来我想,理想的解决方式应该是使Flink的并发数接近于或等于Kafka的分区数。我的Kafka分区数为3,于是Flink setParallelism 
> 为3。后来发现又无法推进watermark。检查Kafka后发现,kafka Console Producer把所有的数据都推送到了第0号分区。
>
>
>
> 请问哪位能指点一下,让Kafka topic的每个分区都能收到数据?
>
>
>
>
>
> Best,
>
> Lucas
>
>
>
> Original Email
>
>
>
> Sender:"Weihua Hu"< huweihua....@gmail.com &gt;;
>
> Sent Time:2023/2/7 18:48
>
> To:"user-zh"< user-zh@flink.apache.org &gt;;
>
> Subject:Re: Kafka 数据源无法实现基于事件时间的窗口聚合
>
>
> Hi,
>
> 问题应该是 kafka source 配置了多并发运行,但数据量比较少(或者 topic 的 partition 数量小于 task
> 的并发数量),不是所有的 source task 都消费到了数据并产生 watermark,导致下游聚合算子无法对齐 watermark 触发计算。
> 可以尝试通过以下办法解决:
> 1. 将 source 并发控制为 1
> 2. 为 watermark 策略开始 idleness 处理,参考 [#1]
>
> fromElement 数据源会强制指定并发为 1
>
> [#1]
> https://nightlies.apache.org/flink/flink-docs-master/docs/dev/datastream/event-time/generating_watermarks/#dealing-with-idle-sources
>
>
> Best,
> Weihua
>
>
> On Tue, Feb 7, 2023 at 1:31 PM wei_yuze  wrote:
>
> &gt; 您好!
> &gt;
> &gt;
> &gt;
> &gt;
> &gt; 
> 我在进行基于事件时间的窗口聚合操作时,使用fromElement数据源可以实现,但替换为Kafka数据源就不行了,但程序并不报错。以下贴出代码。代码中给了两个数据源,分别命名为:streamSource
> &gt; 和 kafkaSource
> &gt; 。当使用streamSource生成watermarkedStream的时候,可以完成聚合计算并输出结果。但使用kafkaSource却不行。
> &gt;
> &gt;
> &gt;
> &gt;
> &gt; public class WindowReduceTest2 {&nbsp; &nbsp; public static void
> &gt; main(String[] args) throws Exception {
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; StreamExecutionEnvironment env =
> &gt; StreamExecutionEnvironment.getExecutionEnvironment();
> &gt;
> &gt;
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; // 使用fromElement数据源
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; DataStreamSource&gt; env.fromElements(
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new
> &gt; Event2("Alice", "./home", "2023-02-04 17:10:11"),
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Event2("Bob",
> &gt; "./cart", "2023-02-04 17:10:12"),
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new
> &gt; Event2("Alice", "./home", "2023-02-04 17:10:13"),
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new
> &gt; Event2("Alice", "./home", "2023-02-04 17:10:15"),
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new 
> Event2("Cary",
> &gt; "./home", "2023-02-04 17:10:16"),
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new 
> Event2("Cary",
> &gt; "./home", "2023-02-04 17:10:16")
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; );
> &gt;
> &gt;
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; // 使用Kafka数据源
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; JsonDeserializationSchema<Event2&gt;
> &gt; jsonFormat = new JsonDeserializationSchema<&gt;(Event2.class);
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; KafkaSource&gt; 
> KafkaSource.<Event2&gt;builder()
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; .setBootstrapServers(Config.KAFKA_BROKERS)
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; .setTopics(Config.KAFKA_TOPIC)
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; .setGroupId("my-group")
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; .setStartingOffsets(OffsetsInitializer.earliest())
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; .setValueOnlyDeserializer(jsonFormat)
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; DataStreamSource&gt; env.fromSource(source, 
> WatermarkStrategy.noWatermarks(), "Kafka Source");
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; kafkaSource.print();
> &gt;
> &gt;
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; // 生成watermark,从数据中提取时间作为事件时间
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; SingleOutputStreamOperator<Event2&gt;
> &gt; watermarkedStream =
> &gt; 
> kafkaSource.assignTimestampsAndWatermarks(WatermarkStrategy.<Event2&gt;forBoundedOutOfOrderness(Duration.ZERO)
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; .withTimestampAssigner(new SerializableTimestampAssigner&gt; &nbsp; 
> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; @Override
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; public long extractTimestamp(Event2 element, long recordTimestamp) {
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; &nbsp; &nbsp; SimpleDateFormat simpleDateFormat = new
> &gt; SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; &nbsp; &nbsp; Date date = null;
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; &nbsp; &nbsp; try {
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; date =
> &gt; simpleDateFormat.parse(element.getTime());
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; &nbsp; &nbsp; } catch (ParseException e) {
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException(e);
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; &nbsp; &nbsp; }
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; &nbsp; &nbsp; long time = date.getTime();
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; &nbsp; &nbsp; System.out.println(time);
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; &nbsp; &nbsp; return time;
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }));
> &gt;
> &gt;
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; // 窗口聚合
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; watermarkedStream.map(new MapFunction<Event2,
> &gt; Tuple2&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
> &nbsp; &nbsp;
> &gt; @Override
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; public Tuple2&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
> &nbsp; &nbsp; &nbsp;
> &gt; &nbsp; &nbsp; // 将数据转换成二元组,方便计算
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; &nbsp; &nbsp; return Tuple2.of(value.getUser(), 1L);
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .keyBy(r -&gt;
> &gt; r.f0)
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 设置滚动事件时间窗口
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; .window(TumblingEventTimeWindows.of(Time.seconds(5)))
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .reduce(new
> &gt; ReduceFunction<Tuple2&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
> &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; @Override
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; public Tuple2&gt; Tuple2&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
> &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; &nbsp; &nbsp; // 定义累加规则,窗口闭合时,向下游发送累加结果
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
> &gt; &nbsp; &nbsp; return Tuple2.of(value1.f0, value1.f1 + value2.f1);
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
> .print("Aggregated
> &gt; stream");
> &gt;
> &gt;
> &gt; &nbsp; &nbsp; &nbsp; &nbsp; env.execute();
> &gt; &nbsp; &nbsp; }
> &gt; }
> &gt;
> &gt;
> &gt;
> &gt;
> &gt;
> &gt;
> &gt; 值得注意的是,若将代码中的 TumblingEventTimeWindows 替换为 TumblingProcessingTimeWindows
> &gt; ,即使使用 Kafka 数据源也是可以完成聚合计算并输出结果的。
> &gt;
> &gt;
> &gt;
> &gt; 感谢您花时间查看这个问题!
> &gt; Lucas

回复