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

相关推荐
森G3 分钟前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt
张飞飞飞飞飞9 分钟前
Tmux命令使用教程
linux·服务器·ubuntu
QZ1665609515936 分钟前
动态感知·全覆盖管控·符合司法要求:通用行业知形数据库风险监测合规落地方案
大数据·人工智能
森G1 小时前
77、线程池原理和实现------服务器源码解析----云视频服务项目
服务器·c++·qt
GEO优化小助手1 小时前
2026临沂GEO优化公司实测解析:3家本土机构适配性参考
大数据·人工智能·python
qeen871 小时前
【Linux】Linux简单介绍与基本指令(上)
linux·运维·服务器·学习
OceanBase数据库官方博客1 小时前
OceanBase + Flink 数据集成(第二部分):通过 JDBC 协议实现实时数据同步
大数据·flink·oceanbase
難釋懷2 小时前
Nginx获取客户端真实IP
服务器·前端·nginx
跨境摸鱼2 小时前
年中政策切换窗口临近跨境卖家如何安排新品测试与库存回收
大数据·人工智能·跨境电商·跨境·营销策略
IsJunJianXin3 小时前
pdd小程序 cdp 保存响应体
linux·服务器·小程序·pdd小程序·拼多多响应体解密·小程序cdp·拼多多rpc取响应体