mac电脑端安装flink并运行example

本文介绍如何在mac电脑端安装flink、运行flink自带exmaple。

唯一的前置条件为电脑端安装Java 8.x 。

mac电脑端安装flink命令:

brew install apache-flink

查看flink安装位置,启动flink

brew info apache-flink
/usr/local/Cellar/apache-flink/1.8.1/libexec/bin
cd /usr/local/Cellar/apache-flink/1.8.1/libexec/bin;./start-cluster.sh
//输出:
//Starting cluster.
//Starting standalonesession daemon on host MacBook-Pro.local.
//Starting taskexecutor daemon on host MacBook-Pro.local.

查看flink控制台

http://localhost:8081/#/overview

创建一个flink的maven项目,这里使用flink-quickstart-scala模版生成项目,同时可以使用scala与java代码编写flink应用

 mvn archetype:generate -DarchetypeGroupId=org.apache.flink -DarchetypeArtifactId=flink-quickstart-scala -DarchetypeVersion=1.8.1 -DgroupId=com.galaxy.flink -DartifactId=galaxyFlink -Dversion=1.0-SNAPSHOT -Dpackage=com.galaxy.flink -DinteractiveMode=false

从github的flink项目获得代码,类名:org.apache.flink.streaming.examples.socket.SocketWindowWordCount

package com.galaxy.flink.examples.socket;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

/**
 * Implements a streaming windowed version of the "WordCount" program.
 *
 * <p>This program connects to a server socket and reads strings from the socket.
 * The easiest way to try this out is to open a text server (at port 12345)
 * using the <i>netcat</i> tool via
 * <pre>
 * nc -l 12345 on Linux or nc -l -p 12345 on Windows or Mac
 * </pre>
 * and run this example with the hostname and the port as arguments.
 */
@SuppressWarnings("serial")
public class SocketWindowWordCount {

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

        // the host and the port to connect to
        final String hostname;
        final int port;
        try {
            final ParameterTool params = ParameterTool.fromArgs(args);
            hostname = params.has("hostname") ? params.get("hostname") : "localhost";
            port = params.getInt("port");
        } catch (Exception e) {
            System.err.println("No port specified. Please run 'SocketWindowWordCount " +
                    "--hostname <hostname> --port <port>', where hostname (localhost by default) " +
                    "and port is the address of the text server");
            System.err.println("To start a simple text server, run 'netcat -l <port>' and " +
                    "type the input text into the command line");
            return;
        }

        // get the execution environment
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // get input data by connecting to the socket
        DataStream<String> text = env.socketTextStream(hostname, 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))

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

添加README.md文件与.gitignore文件,并将本地项目提交到github,方便预研代码托管。
图片

使用 netcat 启动一个本地server:[注意:在mac端,要使用p参数]

nc -l -p 9000
//随机敲入字符
a d
w e

运行example:

有两种方式启动代码:

  • 直接在IDEA中启动代码类,任务将在本地内嵌的Flink环境中运行
//从控制台日志中可以看到
15:01:38,218 INFO  org.apache.flink.streaming.api.environment.LocalStreamEnvironment  - Running job on local embedded Flink mini cluster
15:01:38,659 INFO  org.apache.flink.runtime.minicluster.MiniCluster              - Starting Flink Mini Cluster

控制台中会打印出统计信息。

  • 将代码打成jar包,提交到本地flink集群环境中运行
//提交到本地flink集群命令
/usr/local/Cellar/apache-flink/1.8.1/libexec/bin/flink run -c com.galaxy.flink.examples.socket.SocketWindowWordCount /Users/baozhiwang/local_dir/codes/galaxyFlink/target/galaxyFlink-1.0-SNAPSHOT.jar --hostname localhost --port 9000

查看日志:

tailf /usr/local/Cellar/apache-flink/1.8.1/libexec/log/flink-baozhiwang-taskexecutor-0-baozhideMacBook-Pro.local.out
/**
a : 1
d : 1
w : 1
e : 1
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值