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

相关推荐
Vic10101几秒前
PostgreSQL 中序列(bigserial 和手动序列)的使用与注意事项
java·大数据·数据库·postgresql
武汉唯众智创1 分钟前
应用型大数据实训室实验教学方案:一份基于开源技术的完整实训指南
大数据·开源·大数据实训室·开源技术·大数据实验室
武子康2 分钟前
大数据-203 scikit-learn 决策树剪枝参数:max_depth/min_samples_leaf 到 min_impurity_decrease
大数据·后端·机器学习
ywyy679811 分钟前
品牌GEO优化系统开发:区域流量、用户点击、到店转化的数据分析技巧
大数据·geo系统开发·geo系统·geo优化系统开发·geo优化系统·品牌geo
躲在云朵里`13 分钟前
Linux环境下部署SpringBoot前后端分离项目
linux·服务器
llilian_1614 分钟前
时间同步校时服务器配件清单及挑选攻略 校时时间服务器 网络时间同步装置
运维·服务器·网络
oMcLin14 分钟前
如何在Ubuntu 20.04系统的香港服务器上使用Docker搭建高效的CI/CD流水线并集成Kubernetes?
服务器·ubuntu·docker
nvd1120 分钟前
通过 Gmail API 发送邮件的完整指南
服务器·网络
深圳市恒讯科技22 分钟前
防止服务器被黑:终极防范网络攻击指南
运维·服务器·网络安全