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

相关推荐
实验室管理云平台4 分钟前
实验室信息管理系统:提升科研效率与数据安全的关键工具
大数据·科技
CingSyuan4 分钟前
4U GPU服务器整机无法上电故障排查实录:通过交叉替换、逐项回装与跨槽位测试,定位RTX 4080显卡故障及硬盘槽位异常并完成临时恢复
运维·服务器
SelectDB9 分钟前
Apache Doris 多表 Join 优化实战:从 CBO 到 Colocate Join 配置(附 SQL)
大数据·数据库·数据分析
handler0112 分钟前
【Linux】ELF、链接与动态库,程序装载全解析
linux·运维·服务器·chrome·c·elf·文件
云动雨颤16 分钟前
云服务器运行WordPress需要什么配置?
服务器
dishugj20 分钟前
操作系统四大特征|软考架构师考点梳理
java·linux·服务器
ApacheSeaTunnel25 分钟前
195 次合并、13 个新连接器!Apache SeaTunnel 8 月有哪些新进展?
大数据·开源·数据集成·seatunnel·技术分享·数据同步
Huangjin007_27 分钟前
【Linux 系统篇(十九)】进程(七):深入理解虚拟地址空间:从进程地址空间到内核管理
linux·运维·服务器
YIN_尹32 分钟前
【Linux系统编程】模拟实现一个最简版C语言标准IO库
linux·服务器·c语言
FW-Linker32 分钟前
什么是5G直播背包?广电记者外场推流入门指南
服务器·网络·5g