Quickstart

原文链接


在几个简单的步骤中启动和运行Flink示例程序。

设置:下载和启动Flink

Flink运行在Linux, Mac OS X, and Windows。为了能够运行Flink,唯一的要求就是工作在Java 7.x(或更高)上。Windows用户,请查看Windows上运行Flink指南,它描述了在本地Windows上如何运行Flink。

你可以通过以下命令检查Java安装的正确性:

java -version

如果您有Java 8,那么输出将是这样的:

java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

Download and Unpack

  1. 从下载页下载二进制包。您可以选择您喜欢的任何Hadoop/Scala组合。你可以选择任何你喜欢的Hadoop/Scala组合包。如果你计划使用文件系统,那么可以使用任何Hadoop版本。
  2. 进入下载目录。
  3. 解压下载的压缩包。
$ cd ~/Downloads        # Go to download directory
$ tar xzf flink-*.tgz   # Unpack the downloaded archive
$ cd flink-1.4.1

MacOS X

对于MacOS X用户, 可以通过Homebrew安装Flink。

$ brew install apache-flink
...
$ flink --version
Version: 1.2.0, Commit ID: 1c659cf

启动一个本地Flink集群

$ ./bin/start-local.sh  # Start Flink

通过访问http://localhost:8081检查JobManager网页,确保所有组件都已运行。网页会显示一个有效的TaskManager实例。

Quickstart_第1张图片
1195625042e58743ce16e6722da1fc2f.png

您还可以通过检查日志目录中的日志文件来验证系统是否正在运行:

$ tail log/flink-*-jobmanager-*.log
INFO ... - Starting JobManager
INFO ... - Starting JobManager web frontend
INFO ... - Web frontend listening at 127.0.0.1:8081
INFO ... - Registered TaskManager at 127.0.0.1 (akka://flink/user/taskmanager)

阅读代码

你可以在GitHub上找到这个SocketWindowWordCount例子的完整的scala和java的完整源代码。

public class SocketWindowWordCount {

    public static void main(String[] args) throws Exception {

        // the port to connect to
        final int port;
        try {
            final ParameterTool params = ParameterTool.fromArgs(args);
            port = params.getInt("port");
        } catch (Exception e) {
            System.err.println("No port specified. Please run 'SocketWindowWordCount --port '");
            return;
        }

        // get the execution environment
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // get input data by connecting to the socket
        DataStream text = env.socketTextStream("localhost", port, "\n");

        // parse the data, group it, window it, and aggregate the counts
        DataStream windowCounts = text
            .flatMap(new FlatMapFunction() {
                @Override
                public void flatMap(String value, Collector out) {
                    for (String word : value.split("\\s")) {
                        out.collect(new WordWithCount(word, 1L));
                    }
                }
            })
            .keyBy("word")
            .timeWindow(Time.seconds(5), Time.seconds(1))
            .reduce(new ReduceFunction() {
                @Override
                public WordWithCount reduce(WordWithCount a, WordWithCount b) {
                    return new WordWithCount(a.word, a.count + b.count);
                }
            });

        // print the results with a single thread, rather than in parallel
        windowCounts.print().setParallelism(1);

        env.execute("Socket Window WordCount");
    }

    // Data type for words with count
    public static class WordWithCount {

        public String word;
        public long count;

        public WordWithCount() {}

        public WordWithCount(String word, long count) {
            this.word = word;
            this.count = count;
        }

        @Override
        public String toString() {
            return word + " : " + count;
        }
    }
}

运行示例

现在,我们将运行这个Flink应用程序。它将从一个套接字读取文本,每5秒打印出在前5秒内每一个不同单词的出现次数,即处理时间的滚动窗口,只要单词是浮动的。

  • 首先,我们使用netcat启动本地服务器。

    $ nc -l 9000
    
    
  • 提交Flink程序:

    $ ./bin/flink run examples/streaming/SocketWindowWordCount.jar --port 9000
    
    Cluster configuration: Standalone cluster with JobManager at /127.0.0.1:6123
    Using address 127.0.0.1:6123 to connect to JobManager.
    JobManager web interface address http://127.0.0.1:8081
    Starting execution of program
    Submitting job with JobID: 574a10c8debda3dccd0c78a3bde55e1b. Waiting for job completion.
    Connected to JobManager at Actor[akka.tcp://[email protected]:6123/user/jobmanager#297388688]
    11/04/2016 14:04:50     Job execution switched to status RUNNING.
    11/04/2016 14:04:50     Source: Socket Stream -> Flat Map(1/1) switched to SCHEDULED
    11/04/2016 14:04:50     Source: Socket Stream -> Flat Map(1/1) switched to DEPLOYING
    11/04/2016 14:04:50     Fast TumblingProcessingTimeWindows(5000) of WindowedStream.main(SocketWindowWordCount.java:79) -> Sink: Unnamed(1/1) switched to SCHEDULED
    11/04/2016 14:04:51     Fast TumblingProcessingTimeWindows(5000) of WindowedStream.main(SocketWindowWordCount.java:79) -> Sink: Unnamed(1/1) switched to DEPLOYING
    11/04/2016 14:04:51     Fast TumblingProcessingTimeWindows(5000) of WindowedStream.main(SocketWindowWordCount.java:79) -> Sink: Unnamed(1/1) switched to RUNNING
    11/04/2016 14:04:51     Source: Socket Stream -> Flat Map(1/1) switched to RUNNING
    
    

程序连接到套接字并等待输入。您可以检查web界面以验证作业是否按预期运行:

Quickstart_第2张图片
jobmanager-2.png
Quickstart_第3张图片
jobmanager-3.png

单词在5秒的时间窗口(处理时间,滚动的窗口)被计算,并打印到stdout。监视任务管理器的输出文件,并在nc中写入一些文本(点击后按行发送到Flink行):

$ nc -l 9000
lorem ipsum
ipsum ipsum ipsum
bye

The .out file will print the counts at the end of each time window as long as words are floating in, e.g.:

$ tail -f log/flink-*-taskmanager-*.out
lorem : 1
bye : 1
ipsum : 4

通过下述命令停止Flink:

$ ./bin/stop-local.sh

你可能感兴趣的:(Quickstart)