Flink hello world

下载并且解压Flink

Downloads | Apache Flink

启动Flink.

$ ./bin/start-cluster.sh 
Starting cluster.
Starting standalonesession daemon on host harrydeMacBook-Pro.local.
Starting taskexecutor daemon on host harrydeMacBook-Pro.local.

访问localhost:8081

Flink 的版本附带了许多示例作业。您可以快速将这些应用程序之一部署到正在运行的集群。

$ ./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

$ ./bin/stop-cluster.sh

自己编写java 代码运行第一个flink hello world.

pom.xml

     <properties>
       <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <flink.version>1.17.1</flink.version>
        
    </properties>

      <dependencies>
        <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</artifactId>
            <version>${flink.version}</version>
        </dependency>

</dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>org.example.HelloWorld</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

java 代码

package org.example;

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
        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));
            }
        }
    }
}

maven打包成jar

mvn package

利用flink CLI 运行jar

./bin/flink run -c your.package.FlinkJob /path/to/your/jar

执行后你能在localhost:8081上发现你的job相关信息。


参考

Local Installation | Apache Flink

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-10 02:26:09       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-10 02:26:09       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-10 02:26:09       87 阅读
  4. Python语言-面向对象

    2024-03-10 02:26:09       96 阅读

热门阅读

  1. 新手怎么使用github?

    2024-03-10 02:26:09       45 阅读
  2. 手撕算法系列----Dijkstra单源最短路径

    2024-03-10 02:26:09       44 阅读
  3. 生活里的英语应该【怎么说】

    2024-03-10 02:26:09       61 阅读
  4. 探索1688 API接口:实现商品数据自动化处理

    2024-03-10 02:26:09       43 阅读
  5. OpenFeign的学习总结

    2024-03-10 02:26:09       35 阅读
  6. QWebEngineView的load和seturl函数

    2024-03-10 02:26:09       48 阅读
  7. 朴素贝叶斯基本原理&sklearn实现

    2024-03-10 02:26:09       36 阅读
  8. oracle归档日志清理

    2024-03-10 02:26:09       42 阅读
  9. 数据库基础知识记录

    2024-03-10 02:26:09       41 阅读