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

相关推荐
dixiuapp1 天前
校园后勤管理平台,如何选择与规划
大数据·人工智能·工单管理系统·院校工单管理系统·物业报修系统
hg01181 天前
埃塞俄比亚启动建设非洲最大机场
大数据
EmbedLinX1 天前
一文理解后端核心概念:同步/异步、阻塞/非阻塞、进程/线程/协程
linux·服务器·c语言·网络
小王努力学编程1 天前
LangChain——AI应用开发框架(核心组件2)
linux·服务器·c++·人工智能·python·langchain·信号
云服务器租用费用1 天前
京东云主机企业用户能参与的优惠活动汇总
服务器·网络·京东云
Hello.Reader1 天前
Flink Standalone 从 0 到可运维的 Session/HA 集群模板(附配置清单)
大数据·flink
talle20211 天前
Spark分布式计算框架介绍
大数据·分布式·spark·rdd
Leon Cheng1 天前
工作流引擎在AI Agent中的应用
大数据·人工智能
华南首席酱油官1 天前
精工筑净 标杆引领:净化板厂家赋能净化彩钢板行业新高度
大数据·人工智能
浅水壁虎1 天前
任务调度——XXLJOB3(执行器)
java·服务器·前端·spring boot