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

相关推荐
派可数据BI可视化1 小时前
一文读懂系列:数据仓库为什么分层,分几层?数仓建模方法有哪些
大数据·数据仓库·信息可视化·spark·商业智能bi
Light601 小时前
不止于名:领码 SPARK 如何“链”动数据仓库、数据湖、中台与湖仓一体新纪元
大数据·数据仓库·数据湖·ipaas·湖仓一体·数据中台·领码 spark
郑小憨1 小时前
FlinkSQL窗口函数TUMBLE、SESSION 和 HOP的区别
大数据·数据仓库·sql·flink·database
码字的字节1 小时前
锚点模型:数据仓库中的高度可扩展建模技术详解
大数据·数据仓库·spark
Agentic AI人工智能与大数据1 小时前
大数据领域数据仓库的ETL任务优化
大数据·数据仓库·ai·etl
AI软件工程实践1 小时前
解读大数据领域数据仓库的事实表设计
大数据·数据仓库·ai
开开心心_Every3 小时前
CDR版本转换工具,支持多版本互转免升级软件
linux·运维·服务器·云原生·edge·pdf·serverless
生瓜硬劈..4 小时前
从写入到可查:Elasticsearch “近实时”查询原理详解
大数据·elasticsearch·搜索引擎
heimeiyingwang5 小时前
企业非结构化数据的 AI 处理与价值挖掘
大数据·数据库·人工智能·机器学习·架构