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

}
相关推荐
科技圈快讯20 分钟前
破解企业低碳转型难题,港华商会携手碳启元出击
大数据·人工智能
TYFHVB1243 分钟前
11款CRM数字化方案横评:获客-履约-复购全链路能力对决
大数据·人工智能·架构·自动化·流程图
Elastic 中国社区官方博客2 小时前
易捷问数(NewmindExAI)平台解决 ES 升级后 AI 助手与 Attack Discovery 不正常问题
大数据·运维·数据库·人工智能·elasticsearch·搜索引擎·ai
大模型玩家七七5 小时前
技术抉择:微调还是 RAG?——以春节祝福生成为例
android·java·大数据·开发语言·人工智能·算法·安全
GEO-optimize6 小时前
2026北京GEO服务商评审指南:核心实力与适配指南
大数据·人工智能·机器学习·geo
跨境小技6 小时前
如何从eBay抓取商品价格数据?2026 eBay数据采集实用方案
大数据·运维
JosieBook7 小时前
【数据库】时序数据库选型指南:从大数据角度解析IoTDB的优势
大数据·数据库·时序数据库
小叮当⇔8 小时前
电动工具品牌简介
大数据·人工智能
Aloudata8 小时前
数据治理新解法:基于算子级血缘的主动元数据如何破解数仓重构难题?
大数据·数据库·数据治理·元数据·数据血缘
weixin199701080168 小时前
海外淘宝商品详情页前端性能优化实战
大数据·前端·python