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

相关推荐
Regentsoft丽晶软件8 分钟前
2026年ERP+分销一体化还是独立部署?两种架构的优劣对比与选型建议
大数据·架构·产品运营
逸Y 仙X15 分钟前
文章三:Elasticsearch 集群恢复和索引分布
java·大数据·linux·服务器·elasticsearch·搜索引擎·全文检索
CDYXY2 小时前
2026年4月成都卡布灯箱源头口碑深度调研与避坑指南
大数据·人工智能
ZFSS7 小时前
Localization Translate API 集成与使用指南
java·服务器·数据库·人工智能·mysql·ai编程
鹧鸪云光伏8 小时前
微电网容量规划软件:光储设备配置一站式解决方案
大数据·储能·光伏·储能设计方案
扫地的小何尚8 小时前
NVIDIA Vera Rubin 平台如何解决 Agentic AI 的 Scale-up 难题
大数据·人工智能·机器学习
kekekka8 小时前
重塑品牌增长逻辑:专业媒体发稿服务如何让每一分预算产生长效复利?
大数据·搜索引擎·媒体
索木木9 小时前
NCCL SHARP 和 TREE算法
java·服务器·算法
Gongxiangqishou10 小时前
县域即时配送订单规模同比增长35%,远超一线城市的22%
大数据·人工智能
落日屿星辰10 小时前
【无标题】
大数据