reswqa commented on code in PR #26118: URL: https://github.com/apache/flink/pull/26118#discussion_r1946022534
########## flink-examples/flink-examples-streaming/src/main/java/org/apache/flink/streaming/examples/dsv2/windowing/CountProductSalesWindowing.java: ########## @@ -0,0 +1,235 @@ +/* + * 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.examples.dsv2.windowing; + +import org.apache.flink.api.common.serialization.SimpleStringEncoder; +import org.apache.flink.api.common.state.StateDeclaration; +import org.apache.flink.api.common.state.StateDeclarations; +import org.apache.flink.api.common.state.ValueStateDeclaration; +import org.apache.flink.api.common.state.v2.ValueState; +import org.apache.flink.api.common.typeinfo.TypeDescriptors; +import org.apache.flink.api.connector.dsv2.DataStreamV2SourceUtils; +import org.apache.flink.api.connector.dsv2.WrappedSink; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.api.java.tuple.Tuple3; +import org.apache.flink.configuration.MemorySize; +import org.apache.flink.connector.file.sink.FileSink; +import org.apache.flink.core.fs.Path; +import org.apache.flink.datastream.api.ExecutionEnvironment; +import org.apache.flink.datastream.api.builtin.BuiltinFuncs; +import org.apache.flink.datastream.api.common.Collector; +import org.apache.flink.datastream.api.context.PartitionedContext; +import org.apache.flink.datastream.api.extension.eventtime.EventTimeExtension; +import org.apache.flink.datastream.api.extension.window.context.OneInputWindowContext; +import org.apache.flink.datastream.api.extension.window.function.OneInputWindowStreamProcessFunction; +import org.apache.flink.datastream.api.extension.window.strategy.WindowStrategy; +import org.apache.flink.datastream.api.stream.NonKeyedPartitionStream; +import org.apache.flink.streaming.api.functions.sink.PrintSink; +import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy; +import org.apache.flink.streaming.examples.dsv2.windowing.util.CountProductSalesWindowingData; +import org.apache.flink.util.ParameterTool; + +import java.time.Duration; +import java.util.Arrays; +import java.util.Set; + +/** + * Example illustrating how to use Window to count the sales of each product in each hour by + * DataStream API V2. + * + * <p>The input is a [list of] order, each order is a tuple of productId and orderTime. Review Comment: ```suggestion * <p>The input is a list of order, each order is a tuple of productId and orderTime. ``` ########## flink-examples/flink-examples-streaming/src/main/java/org/apache/flink/streaming/examples/dsv2/eventtime/util/StatisticNewsClickNumberData.java: ########## @@ -0,0 +1,631 @@ +/* + * 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.examples.dsv2.eventtime.util; + +import org.apache.flink.streaming.examples.dsv2.eventtime.StatisticNewsClickNumber.NewsEvent; +import org.apache.flink.streaming.examples.dsv2.eventtime.StatisticNewsClickNumber.NewsEventType; + +public class StatisticNewsClickNumberData { Review Comment: java doc? ########## flink-examples/flink-examples-streaming/src/main/java/org/apache/flink/streaming/examples/dsv2/eventtime/StatisticNewsClickNumber.java: ########## @@ -0,0 +1,278 @@ +/* + * 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.examples.dsv2.eventtime; + +import org.apache.flink.api.common.serialization.SimpleStringEncoder; +import org.apache.flink.api.connector.dsv2.DataStreamV2SourceUtils; +import org.apache.flink.api.connector.dsv2.WrappedSink; +import org.apache.flink.configuration.MemorySize; +import org.apache.flink.connector.file.sink.FileSink; +import org.apache.flink.core.fs.Path; +import org.apache.flink.datastream.api.ExecutionEnvironment; +import org.apache.flink.datastream.api.common.Collector; +import org.apache.flink.datastream.api.context.PartitionedContext; +import org.apache.flink.datastream.api.extension.eventtime.EventTimeExtension; +import org.apache.flink.datastream.api.extension.eventtime.function.OneInputEventTimeStreamProcessFunction; +import org.apache.flink.datastream.api.extension.eventtime.timer.EventTimeManager; +import org.apache.flink.datastream.api.stream.NonKeyedPartitionStream; +import org.apache.flink.streaming.api.functions.sink.PrintSink; +import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy; +import org.apache.flink.streaming.examples.dsv2.eventtime.util.StatisticNewsClickNumberData; +import org.apache.flink.util.ParameterTool; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * This example illustrates how to count the number of clicks on each news at 1 minute, 5 minutes, + * 10 minutes, and 1 hour after news publication. + * + * <p>The input consists of a series of {@link NewsEvent}s, which fall into two categories: news + * releases and news clicks. Each {@link NewsEvent} contains three components: the event type, the + * news ID and the timestamp. Notably, there is only one event of type {@link NewsEventType#RELEASE} + * for each news. + * + * <p>Usage: + * + * <ul> + * <li><code>--output <path></code>The output directory where the Job will write the + * results. If no output path is provided, the Job will print the results to <code>stdout + * </code>. + * </ul> + * + * <p>This example shows how to: + * + * <ul> + * <li>Usage of Event Time extension in DataStream API V2 + * </ul> + * + * <p>Please note that if you intend to run this example in an IDE, you must first add the following + * VM options: "--add-opens=java.base/java.util=ALL-UNNAMED". This is necessary because the module + * system in JDK 17+ restricts some reflection operations. + * + * <p>Please note that the DataStream API V2 is a new set of APIs, to gradually replace the original + * DataStream API. It is currently in the experimental stage and is not fully available for + * production. + */ +public class StatisticNewsClickNumber { + + // we will count the number of clicks within 1min/5min/10min/30min/1hour after the news release + private static final Duration[] TIMES_AFTER_NEWS_RELEASE = + new Duration[] { + Duration.ofMinutes(1), + Duration.ofMinutes(5), + Duration.ofMinutes(10), + Duration.ofMinutes(30), + Duration.ofHours(1) + }; + + /** + * The type of {@link NewsEvent}, note that only one event of type {@link NewsEventType#RELEASE} + * for each news. + */ + public enum NewsEventType { + RELEASE, + CLICK + } + + /** + * The {@link NewsEvent} represents an event on news, containing the event type, news id and the + * timestamp. + */ + public static class NewsEvent { + public NewsEventType type; + public long newsId; + public long timestamp; + + public NewsEvent(NewsEventType type, long newsId, long timestamp) { + this.type = type; + this.newsId = newsId; + this.timestamp = timestamp; + } + } + + /** + * The {@link NewsClickNumber} represents the number of clicks on news within a specified + * duration following its release. For example, NewsClickNumber{newsId="12345678", + * timeAfterRelease=60000, clickNumber=132} indicates that the news "12345678" has been clicked + * 132 times within 60,000 milliseconds after its release. + */ + public static class NewsClickNumber { + public long newsId; + public long timeAfterRelease; + public long clickNumber; + + public NewsClickNumber(long newsId, long timeAfterRelease, long clickCount) { + this.newsId = newsId; + this.timeAfterRelease = timeAfterRelease; + this.clickNumber = clickCount; + } + + public String toString() { + return String.format( + "(%d,%d,%d)", this.newsId, this.timeAfterRelease, this.clickNumber); + } + } + + public static void main(String[] args) throws Exception { + // parse the parameters + final ParameterTool params = ParameterTool.fromArgs(args); + final boolean fileOutput = params.has("output"); + + // obtain execution environment + ExecutionEnvironment env = ExecutionEnvironment.getInstance(); + + // the input consists of a series of {code NewsEvent}s, which include two types: news + // release event and news click event. + NonKeyedPartitionStream<NewsEvent> source = + env.fromSource( + DataStreamV2SourceUtils.fromData( + Arrays.asList(StatisticNewsClickNumberData.NEWS_EVENTS)), + "news event source"); + + NonKeyedPartitionStream<NewsClickNumber> clickNumberStream = + // extract event time and generate the event time watermark + source.process( + // the timestamp field of the input is considered to be the + // event time + EventTimeExtension.<NewsEvent>newWatermarkGeneratorBuilder( + event -> event.timestamp) + // generate event time watermarks every 200ms + .periodicWatermark(Duration.ofMillis(200)) + // if the input is idle for more than 30 seconds, it + // is ignored during the event time watermark + // combination process + .withIdleness(Duration.ofSeconds(30)) + // set the maximum out-of-order time of the event to + // 30 seconds, meaning that if an event is received + // at 12:00:00, then no further events should be + // received earlier than 11:59:30 + .withMaxOutOfOrderTime(Duration.ofSeconds(10)) + // build the event time watermark generator as + // ProcessFunction + .buildAsProcessFunction()) + // key by the news id + .keyBy(event -> event.newsId) + // count the click number of each news + .process( + EventTimeExtension.wrapProcessFunction( + new CountNewsClickNumberProcessFunction())); + + if (fileOutput) { + // write joined results to file Review Comment: joined? -- 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. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org