entry : System.getenv().entrySet()) {
+ map.put(entry.getKey(), entry.getValue());
+ }
+ return map;
+ }
+
+
+ /**
+ * 设置默认参数
+ *
+ * @param parameterTool the parameter tool
+ * @return the stream execution environment
+ * @throws Exception the exception
+ */
+ public static StreamExecutionEnvironment prepare(ParameterTool parameterTool) throws Exception {
+ StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
+ // 设置作业的默认并行度
+ env.setParallelism(parameterTool.getInt(AppConstants.STREAM_PARALLELISM, 5));
+ // 关闭系统日志
+ env.getConfig().disableSysoutLogging();
+ // 失败之后,重拾4次,每次间隔10秒
+ env.getConfig().setRestartStrategy(RestartStrategies.fixedDelayRestart(4, 10000));
+ if (parameterTool.getBoolean(AppConstants.STREAM_CHECKPOINT_ENABLE, true)) {
+ // create a checkpoint every 5 seconds
+ env.enableCheckpointing(parameterTool.getInt(AppConstants.STREAM_CHECKPOINT_INTERVAL, 1000));
+ }
+ // make parameters available in the web interface
+ env.getConfig().setGlobalJobParameters(parameterTool);
+ // 默认是事件时间
+ env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
+ return env;
+ }
+}
+
diff --git a/maven/src/main/java/com/duanledexianxianxian/maven/flink/sink/kafka/Main.java b/maven/src/main/java/com/duanledexianxianxian/maven/flink/sink/kafka/Main.java
new file mode 100644
index 0000000000000000000000000000000000000000..d8c5dd6531023b02e0974fb53676b3c2206860c1
--- /dev/null
+++ b/maven/src/main/java/com/duanledexianxianxian/maven/flink/sink/kafka/Main.java
@@ -0,0 +1,131 @@
+/*
+ * 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 com.duanledexianxianxian.maven.flink.sink.kafka;
+
+import com.alibaba.fastjson.JSONObject;
+import com.duanledexianxianxian.maven.flink.common.util.ExecutionEnvUtils;
+import com.duanledexianxianxian.maven.flink.model.Student;
+import com.google.common.collect.Lists;
+import org.apache.flink.api.common.functions.AggregateFunction;
+import org.apache.flink.api.common.functions.MapFunction;
+import org.apache.flink.api.common.serialization.SimpleStringSchema;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.api.java.utils.ParameterTool;
+import org.apache.flink.streaming.api.TimeCharacteristic;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
+import org.apache.flink.streaming.api.windowing.time.Time;
+import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
+import org.apache.kafka.clients.producer.KafkaProducer;
+
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * Skeleton for a Flink Streaming Job.
+ *
+ * For a tutorial how to write a Flink streaming application, check the
+ * tutorials and examples on the Flink Website.
+ * ……
+ *
To package your application into a JAR file for execution, run
+ * 'mvn clean package' on the command line.
+ *
+ *
If you change the name of the main class (with the public static void main(String[] args))
+ * method, change the respective entry in the POM.xml file (simply search for 'mainClass').
+ *
+ * @author Administrator
+ */
+public class Main {
+
+ public static void main(String[] args) throws Exception {
+ final ParameterTool parameterTool = ExecutionEnvUtils.createParameterTool(args);
+
+ // 创建执行环境上下文
+ final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
+ //设置窗口的时间单位为process time
+ env.setStreamTimeCharacteristic(TimeCharacteristic.IngestionTime);
+ Properties properties = new Properties();
+ properties.setProperty("bootstrap.servers", "113.105.144.9:9104");
+
+ FlinkKafkaConsumer consumer = new FlinkKafkaConsumer<>("topic_first", new SimpleStringSchema(), properties);
+
+ //从最早开始消费
+ DataStream stream = env
+ .addSource(consumer);
+
+ // map 函数 flatMap是map的特殊形式
+ SingleOutputStreamOperator student = stream.map((MapFunction) value -> JSONObject.parseObject(value, Student.class));
+
+
+ student.keyBy(Student::getSex).window(TumblingEventTimeWindows.of(Time.seconds(20))).aggregate(new Agg()).addSink(new KafkaProducer<>).print();
+
+ // execute program
+ env.execute("Flink Streaming Java API Skeleton");
+ }
+
+ /**
+ * 自定义聚合函数
+ */
+ public static class Agg implements AggregateFunction, Long>, Tuple2, Long>> {
+ //创建一个数据统计的容器,提供给后续操作使用。
+ @Override
+ public Tuple2, Long> createAccumulator() {
+ return new Tuple2, Long>();
+ }
+
+ //每个元素被添加进窗口的时候调用。
+ //第一个参数是添加进窗口的元素,第二个参数是统计的容器(上面创建的那个)。
+ @Override
+ public Tuple2, Long> add(Student in, Tuple2, Long> acc) {
+ List list = acc.f0;
+ Long count = acc.f1;
+ if (list == null) {
+ list = Lists.newArrayList();
+ }
+ list.add(in);
+ if (count == null) {
+ count = 1L;
+ } else {
+ count++;
+ }
+ return new Tuple2<>(list, count);
+ }
+
+ //窗口统计事件触发时调用来返回出统计的结果。
+ @Override
+ public Tuple2, Long> getResult(Tuple2, Long> acc) {
+ return acc;
+ }
+
+ //只有在当窗口合并的时候调用,合并2个容器
+ @Override
+ public Tuple2, Long> merge(Tuple2, Long> acc1, Tuple2, Long> acc2) {
+ List list1 = acc1.f0;
+ List list2 = acc2.f0;
+ Long count1 = acc1.f1;
+ Long count2 = acc2.f1;
+ list1.addAll(list2);
+ return new Tuple2<>(list1, count1 + count2);
+ }
+ }
+}
+
+
diff --git a/maven/src/main/resources/application.properties b/maven/src/main/resources/application.properties
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391