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

相关推荐
加勒比之杰克23 分钟前
【操作系统原理】Linux 进程控制
linux·运维·服务器·进程控制
TDengine (老段)24 分钟前
TDengine 字符串函数 TO_BASE64 用户手册
android·大数据·服务器·物联网·时序数据库·tdengine·涛思数据
啊吧怪不啊吧24 分钟前
算法王冠上的明珠——动态规划之斐波那契数列问题
大数据·算法·动态规划
wanhengidc7 小时前
云手机的软件核心是什么
运维·服务器·web安全·游戏·智能手机
芬加达8 小时前
jvm八股
运维·服务器·jvm
源码之家8 小时前
基于Python房价预测系统 数据分析 Flask框架 爬虫 随机森林回归预测模型、链家二手房 可视化大屏 大数据毕业设计(附源码)✅
大数据·爬虫·python·随机森林·数据分析·spark·flask
小兔薯了8 小时前
11. Linux firewall 防火墙管理
linux·运维·服务器
TDengine (老段)9 小时前
什么是 TDengine IDMP?
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
Apache Flink10 小时前
Flink Forward Asia 2025 城市巡回 · 深圳站
大数据·flink
Hello.Reader10 小时前
Flink DataStream API 打包使用 MySQL CDC 连接器
大数据·mysql·flink