fredia commented on code in PR #26117:
URL: https://github.com/apache/flink/pull/26117#discussion_r1945921637


##########
flink-examples/flink-examples-streaming/src/main/java/org/apache/flink/streaming/examples/asyncstate/AsyncStateWordCount.java:
##########
@@ -0,0 +1,161 @@
+/*
+ * 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.asyncstate;
+
+import org.apache.flink.api.common.eventtime.WatermarkStrategy;
+import org.apache.flink.api.common.functions.FlatMapFunction;
+import org.apache.flink.api.common.functions.OpenContext;
+import org.apache.flink.api.common.functions.RichFlatMapFunction;
+import org.apache.flink.api.common.state.StateTtlConfig;
+import org.apache.flink.api.common.state.v2.ValueState;
+import org.apache.flink.api.common.state.v2.ValueStateDescriptor;
+import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
+import org.apache.flink.api.common.typeinfo.Types;
+import 
org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.connector.datagen.source.DataGeneratorSource;
+import org.apache.flink.connector.datagen.source.GeneratorFunction;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.datastream.KeyedStream;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.operators.StreamingRuntimeContext;
+import org.apache.flink.util.Collector;
+import org.apache.flink.util.MultipleParameterTool;
+
+import java.time.Duration;
+import java.util.Random;
+
+/**
+ * Implements the "WordCount" program that computes a simple word occurrence 
histogram generated by
+ * datagen. This Job can only be executed with async state enabled.
+ *
+ * <p>This example shows how to:
+ *
+ * <ul>
+ *   <li>Create a state via state V2 interface.
+ *   <li>Use async state in a user-defined function.
+ * </ul>
+ */
+public class AsyncStateWordCount {
+    static final String[] INPUTS = {
+        "This",
+        "is",
+        "an",
+        "example",
+        "for",
+        "state",
+        "V2",
+        "usage",
+        "This",
+        "Job",
+        "can",
+        "only",
+        "be",
+        "executed",
+        "with",
+        "async",
+        "state",
+        "enabled"
+    };
+
+    public static void main(String[] args) throws Exception {
+        MultipleParameterTool params = MultipleParameterTool.fromArgs(args);
+        int ttl = params.getInt("ttl", 0);
+        final StreamExecutionEnvironment env = 
StreamExecutionEnvironment.getExecutionEnvironment();
+
+        // Use DataGenerator to generate data
+        final Random rnd = new Random();
+        GeneratorFunction<Long, String> generatorFunction =
+                index -> INPUTS[rnd.nextInt(INPUTS.length)];
+        DataGeneratorSource<String> source =
+                new DataGeneratorSource<>(
+                        generatorFunction,
+                        Long.MAX_VALUE,
+                        RateLimiterStrategy.perSecond(100),
+                        Types.STRING);
+
+        KeyedStream<String, String> streamSource =
+                env.fromSource(source, WatermarkStrategy.noWatermarks(), "Data 
Generator")
+                        .keyBy(e -> e);
+
+        FlatMapFunction<String, Tuple2<String, Long>> flatMapFunction = new 
WordCounter(ttl);
+
+        DataStream<Tuple2<String, Long>> mapper =
+                streamSource
+                        .enableAsyncState() // enable async state
+                        .flatMap(flatMapFunction)
+                        .setParallelism(3);
+
+        mapper.print().setParallelism(1);
+        env.execute("Async state Word Count example");
+    }
+
+    /** A simple word counter that uses state V2. */
+    public static class WordCounter extends RichFlatMapFunction<String, 
Tuple2<String, Long>> {
+
+        private transient ValueState<Integer> wordCounter;
+
+        private final long ttl;
+
+        public WordCounter(long ttl) {
+            this.ttl = ttl;
+        }
+
+        @Override
+        public void open(OpenContext context) {
+            ValueStateDescriptor<Integer> descriptor =
+                    new ValueStateDescriptor<>("Counter", 
BasicTypeInfo.INT_TYPE_INFO);
+            if (ttl > 0) {
+                StateTtlConfig ttlConfig =
+                        StateTtlConfig.newBuilder(Duration.ofMillis(ttl))
+                                
.setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite)
+                                .setStateVisibility(
+                                        
StateTtlConfig.StateVisibility.ReturnExpiredIfNotCleanedUp)
+                                .build();
+                descriptor.enableTimeToLive(ttlConfig);
+            }
+            wordCounter = ((StreamingRuntimeContext) 
getRuntimeContext()).getValueState(descriptor);
+        }
+
+        @Override
+        public void flatMap(String value, Collector<Tuple2<String, Long>> out) 
throws Exception {
+            wordCounter
+                    .asyncValue()
+                    .thenAccept(

Review Comment:
   Since `StateMachineExample` has `RichFlatMapFunction`, I removed this 
example and rewrite `StateMachineExample#StateMachineMapper` into state-v2 
style.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to