Flink 1.18.1的基本使用

系统示例应用
shell 复制代码
/usr/local/flink-1.18.1/bin/flink run /usr/local/flies/streaming/SocketWindowWordCount.jar --port 9010
shell 复制代码
nc -l 9010
asd asd sdfsf sdf sdfsdagd sdf

单次统计示例工程
shell 复制代码
cd C:\Dev\IdeaProjects


mvn archetype:generate -DarchetypeGroupId=org.apache.flink -DarchetypeArtifactId=flink-quickstart-java -DarchetypeVersion=1.18.1
shell 复制代码
 Define value for property 'groupId':
 Define value for property 'artifactId':
 Define value for property 'version' 1.0-SNAPSHOT: :
 Define value for property 'package' : :

 com.edu
 flink-example
 1.0.0
 com.edu.flink
java 复制代码
package com.edu.flink;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.time.Duration;


public class WindowWordCount {

    public static void main(String[] args) throws Exception {
        //设置运行时环境
        StreamExecutionEnvironment env =
                StreamExecutionEnvironment.getExecutionEnvironment();

        //设置输入流,并执行数据流的处理和转换
        env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime);
        DataStream<Tuple2<String, Integer>> dataStream = env
                .socketTextStream("192.168.18.128", 9000)
                .flatMap(new Splitter())
                .keyBy(0)
                .timeWindow(Time.seconds(5))
                .sum(1);
        dataStream.assignTimestampsAndWatermarks(
                WatermarkStrategy.forBoundedOutOfOrderness(Duration.ofSeconds(3))
        );

        //设置输出流
        dataStream.print();
        //执行程序
        env.execute("Window WordCount");
        System.out.print("finished...");
    }

    public static class Splitter implements FlatMapFunction<String, Tuple2<String,
            Integer>> {
        @Override
        public void flatMap(String sentence, Collector<Tuple2<String, Integer>> out)
                throws Exception {
            for (String word : sentence.split(" ")) {
                out.collect(new Tuple2<String, Integer>(word, 1));
            }
        }
    }

}
相关推荐
青云交11 分钟前
Java 大视界 -- Java 大数据在智能医疗电子病历数据分析与临床决策支持中的应用
java·flink·数据清洗·电子病历·智能医疗·医疗信息化·临床决策
物流可信数据空间21 分钟前
可信数据空间+数据入表:华储数据助力数据交易所破解困局,迈向规模流通
大数据
半夏知半秋1 小时前
Elasticsearch 分词器
大数据·学习·elasticsearch·搜索引擎·全文检索
Hello.Reader1 小时前
Flink SQL 中的 OVER 聚合——为每一行算“窗口统计
数据库·sql·flink
Sui_Network2 小时前
BitGo 通过 LayerZero 将原生 WBTC 引入 Sui
大数据·人工智能·科技·去中心化·区块链
2501_941982052 小时前
赋能销售与客户服务:企业微信外部群 RPA 自动化应用实战
大数据
汽车仪器仪表相关领域2 小时前
SCG-1 增压 + 空燃比二合一仪表:涡轮改装的 “空间杀手” 与 “安全保镖”
大数据·服务器·人工智能·功能测试·安全·汽车·可用性测试
诗旸的技术记录与分享2 小时前
Flink-1.19.0源码详解10-Flink计算资源的申请与调度
大数据·flink
Lenyiin2 小时前
makefile
java·大数据·前端
资深web全栈开发3 小时前
一文讲透 MySQL 崩溃恢复方案设计
大数据·人工智能