Flink hello world

下载并且解压Flink

Downloads | Apache Flink

启动Flink.

bash 复制代码
$ ./bin/start-cluster.sh
Starting cluster.
Starting standalonesession daemon on host DESKTOP-T4TU7JE.
Starting taskexecutor daemon on host DESKTOP-T4TU7JE.

Flink 的版本附带了许多示例作业。您可以快速将这些应用程序之一部署到正在运行的集群。

XML 复制代码
$ ./bin/flink run examples/streaming/WordCount.jar
$ tail log/flink-*-taskexecutor-*.out
  (nymph,1)
  (in,3)
  (thy,1)
  (orisons,1)
  (be,4)
  (all,2)
  (my,1)
  (sins,1)
  (remember,1)
  (d,4)

Stop Flink

bash 复制代码
$ ./bin/stop-cluster.sh

利用java 代码运行第一个flink hello world.

pom.xml

XML 复制代码
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-java</artifactId>
            <version>${flink.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-streaming-java_2.12</artifactId>
            <version>${flink.version}</version>
        </dependency>

java 代码

java 复制代码
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class HelloWorld {

    public static void main(String[] args) throws Exception {

        // Set up the execution environment
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // Create a stream of data
        DataStream<String> dataStream = env.fromElements("Hello", "World", "Flink");

        // Apply transformation: split each word by space
        DataStream<Tuple2<String, Integer>> wordCounts = dataStream
                .flatMap(new Splitter())
                .keyBy(0)
                .sum(1);

        // Print the result
        wordCounts.print();

        // Execute the Flink job
        env.execute("Hello World Example");
    }

    // Custom FlatMapFunction to split each sentence into words
    public static final class Splitter implements FlatMapFunction<String, Tuple2<String, Integer>> {
        @Override
        public void flatMap(String sentence, Collector<Tuple2<String, Integer>> out) {
            // Split the sentence into words
            for (String word : sentence.split(" ")) {
                // Emit the word with a count of 1
                out.collect(new Tuple2<>(word, 1));
            }
        }
    }
}

参考

Local Installation | Apache Flink

相关推荐
曦夜日长3 小时前
Git系统篇,Git本地使用(一):从“究极进化版“到三个区域的底层原理
大数据·linux·git·elasticsearch
阿明副业观察4 小时前
2026年免费AI视频生成工具功能深度解析:有无水印、中文语音及商业用途考量
大数据·人工智能·ai作画·aigc·音视频·ai写作
mengchanmian4 小时前
VSFTP 服务器搭建(Centos)
运维·服务器·centos
别动我齐刘海6 小时前
Linux 系统调试学习1
linux·运维·服务器·c++·学习·机器学习·机器人
2601_962780916 小时前
2027校招准备:统计学专业如何用数据能力应聘企业经营管理管培生
大数据
阿明副业观察6 小时前
AI视频生成技术全解:生成方法、操作流程与选型指南
大数据·人工智能·ai作画·aigc·ai写作
不像程序员的程序媛7 小时前
linux常见文本操作命令
linux·运维·服务器
Leo.yuan7 小时前
FineBI AI原生版:Agent主动规划分析路径,让数据成为企业经营决策的顾问
大数据
VibeCoding工程之道8 小时前
第八章 最终格局:不是 AI 淘汰人,是「依赖 AI 的人」淘汰自己
大数据·人工智能·ai焦虑·ai思考·ai反噬
蜗牛互联网8 小时前
Python Agent工具调用的白名单、参数校验与审批门禁
java·服务器·前端·人工智能·后端·python