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));
            }
        }
    }

}
相关推荐
vx_biyesheji00018 小时前
豆瓣电影推荐系统 | Python Django 协同过滤 Echarts可视化 深度学习 大数据 毕业设计源码
大数据·爬虫·python·深度学习·django·毕业设计·echarts
2501_943695338 小时前
高职大数据与会计专业,考CDA证后能转纯数据分析岗吗?
大数据·数据挖掘·数据分析
实时数据8 小时前
通过大数据的深度分析与精准营销策略,企业能够有效实现精准引流
大数据
子榆.9 小时前
CANN 性能分析与调优实战:使用 msprof 定位瓶颈,榨干硬件每一分算力
大数据·网络·人工智能
新芒9 小时前
暖通行业两位数下滑,未来靠什么赢?
大数据·人工智能
忆~遂愿9 小时前
CANN ATVOSS 算子库深度解析:基于 Ascend C 模板的 Vector 算子子程序化建模与融合优化机制
大数据·人工智能
艾莉丝努力练剑11 小时前
【Linux:文件】Ext系列文件系统(初阶)
大数据·linux·运维·服务器·c++·人工智能·算法
lili-felicity11 小时前
CANN异步推理实战:从Stream管理到流水线优化
大数据·人工智能
2501_9336707912 小时前
2026 高职大数据专业考什么证书对就业有帮助?
大数据
xiaobaibai15312 小时前
营销自动化终极形态:AdAgent 自主闭环工作流全解析
大数据·人工智能·自动化