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

相关推荐
Elastic 中国社区官方博客2 分钟前
快 12 倍的 Elasticsearch 向量索引:使用 GPU 和 CPU 分层部署 NVIDIA cuVS
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索·nvidia
鹧鸪云光伏4 分钟前
光伏设计软件:多屋脊房型如何设计?
大数据·信息可视化·光伏·光伏设计·光伏图纸
前沿推行者8 分钟前
辽宁传媒学院教学特色解析:从实践导向到产教融合
大数据·人工智能
神奇椰子22 分钟前
[特殊字符] 服务器搭建网站完整教程
运维·服务器
数智化管理手记39 分钟前
精益生产3步实操,让现场从混乱变标杆
大数据·运维·网络·人工智能·精益工程
多年小白44 分钟前
2026年5月半导体板块深度分析
大数据·人工智能·科技·区块链
才兄说44 分钟前
机器人二次开发机器人动作定制?毫秒级同步精度
大数据·人工智能·机器人
2601_957786771 小时前
拆解矩阵系统的底层逻辑:从“人海战术“到“一套系统管所有“
大数据·人工智能·矩阵
Project_Observer1 小时前
使用Zoho Projects AI自动项目管理
大数据·数据库·人工智能·深度学习·机器学习·深度优先
SEO_juper1 小时前
CDN 地域节点优化:匹配 GEO 信号,提升加载速度
服务器·ai·php·seo·cdn·geo·谷歌优化