Flink本地安装教程

设置:下载并启动Flink

Flink可在Linux,Mac OS X和Windows上运行。为了能够运行Flink,唯一的要求是安装一个有效的Java 8.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)

下载并编译

$ git clone https://github.com/apache/flink.git
$ cd flink
$ mvn clean package -DskipTests # this will take up to 10 minutes
$ cd build-target               # this is where Flink is installed t

启动本地Flink群集

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

检查分派器的web前端在HTTP://localhost:8081,并确保一切都正常运行。Web前端应报告单个可用的TaskManager实例。
在这里插入图片描述
您还可以通过检查logs目录中的日志文件来验证系统是否正在运行:

$ tail log/flink-*-standalonesession-*.log
INFO ... - Rest endpoint listening at localhost:8081
INFO ... - http://localhost:8081 was granted leadership ...
INFO ... - Web frontend listening at http://localhost:8081.
INFO ... - Starting RPC endpoint for StandaloneResourceManager at akka://flink/user/resourcemanager .
INFO ... - Starting RPC endpoint for StandaloneDispatcher at akka://flink/user/dispatcher .
INFO ... - ResourceManager akka.tcp://flink@localhost:6123/user/resourcemanager was granted leadership ...
INFO ... - Starting the SlotManager.
INFO ... - Dispatcher akka.tcp://flink@localhost:6123/user/dispatcher was granted leadership ...
INFO ... - Recovering all persisted jobs.
INFO ... - Registering TaskManager ... under ... at the SlotManager.

阅读代码

您可以在Java上的GitHub上找到此SocketWindowWordCount示例的完整源代码。



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 <port>'");
            return;
        }

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

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

        // parse the data, group it, window it, and aggregate the counts
        DataStream<WordWithCount> windowCounts = text
            .flatMap(new FlatMapFunction<String, WordWithCount>() {
                @Override
                public void flatMap(String value, Collector<WordWithCount> 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<WordWithCount>() {
                @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
Starting execution of program

程序连接到套接字并等待输入。您可以检查Web界面以验证作业是否按预期运行:
在这里插入图片描述
在这里插入图片描述

  • 单词在5秒的时间窗口(处理时间,翻滚窗口)中计算并打印到stdout。监视TaskManager的输出文件并写入一些文本nc(输入在点击后逐行发送到Flink ):
$ nc -l 9000
lorem ipsum
ipsum ipsum ipsum
bye

该.out文件将在每个时间窗口结束时,只要打印算作字浮在,例如:

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

要停止Flink当你做类型:

$ ./bin/stop-cluster.sh

在Windows上运行Flink

如果要在Windows计算机上本地运行Flink,则需要下载并解压缩二进制Flink分发。之后,您可以使用Windows批处理文件(.bat),或使用Cygwin运行Flink JobManager。

从Windows批处理文件开始

要从Windows命令行启动Flink ,请打开命令窗口,导航到bin/Flink目录并运行start-cluster.bat。

注意:binJava Runtime Environment 的文件夹必须包含在Window的%PATH%变量中。按照本指南将Java添加到%PATH%变量中。

$ cd flink
$ cd bin
$ start-cluster.bat
Starting a local cluster with one JobManager process and one TaskManager process.
You can terminate the processes via CTRL-C in the spawned shell windows.
Web interface by default on http://localhost:8081/.

之后,您需要打开第二个终端来运行作业flink.bat。

从Cygwin和Unix Scripts开始

使用Cygwin,您需要启动Cygwin终端,导航到您的Flink目录并运行start-cluster.sh脚本:

$ cd flink
$ bin/start-cluster.sh
Starting cluster.

从Git安装Flink

如果您正在从git存储库安装Flink并且您正在使用Windows git shell,则Cygwin可能会产生类似于以下的故障:

c:/flink/bin/start-cluster.sh: line 30: $'\r': command not found

发生此错误是因为在Windows中运行时,git会自动将UNIX行结尾转换为Windows样式行结尾。问题是Cygwin只能处理UNIX样式的行结尾。解决方案是通过以下三个步骤调整Cygwin设置以处理正确的行结尾:

  1. 启动一个Cygwin shell。
  2. 输入确定您的主目录
cd; pwd
  1. 使用NotePad,写字板或其他文本编辑器打开.bash_profile主目录中的文件并附加以下内容:(如果文件不存在,则必须创建它)
export SHELLOPTS
set -o igncr

保存文件并打开一个新的bash shell。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值