flink 安装

1.准备工作

##安装包
https://downloads.lightbend.com/scala/2.12.12/scala-2.12.12.zip
https://mirror.bit.edu.cn/apache/flink/flink-1.11.1/flink-1.11.1-bin-scala_2.12.tgz

2.安装步骤

##解压安装包
unzip scala-2.12.12.zip
##配置环境变量
export SCALA_HOME=/opt/scala-2.12.12
export PATH=${SCALA_HOME}/bin:$PATH
##查看版本号
[root@jxq-100-73-13-37 bin]# scala -version
Scala code runner version 2.12.12 -- Copyright 2002-2020, LAMP/EPFL and Lightbend, Inc.
##进入scala
[root@jxq-100-73-13-37 bin]# scala
Welcome to Scala 2.12.12 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_141).
Type in expressions for evaluation. Or try :help.

scala> 
##解压安装包
tar -zxvf flink-1.11.1-bin-scala_2.12.tgz
##配置环境变量
export FLINK_HOME=/data/lilin/flink-1.11.1
export PATH=${FLINK_HOME}/bin:$PATH
##启动服务
[root@jxq-100-73-13-37 bin]# pwd
/data/lilin/flink-1.11.1/bin
[root@jxq-100-73-13-37 bin]# ./start-cluster.sh 
[root@jxq-100-73-13-37 bin]# jps
29832 TaskManagerRunner
29529 StandaloneSessionClusterEntrypoint

3.本地测试

  • session1–指定一个端口启动,可使用netstat -alnp|grep 9002验证在这里插入图片描述
  • session2–使用官方自带的jar及端口启动在这里插入图片描述
  • session3–查看测试结果在这里插入图片描述
通过WEB查看结果-
http://100.73.13.37:8081

在这里插入图片描述
IDEA测试

package com;

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

/**
 * Author: Mr.Deng
 * Date: 2018/10/15
 * Desc: 使用flink对指定窗口内的数据进行实时统计,最终把结果打印出来
 *       先在node21机器上执行nc -l 9000
 */
public class demo {
    public static void main(String[] args) throws Exception {
        //定义socket的端口号
        int port = 9002;
        try{
            ParameterTool parameterTool = ParameterTool.fromArgs(args);
            port = parameterTool.getInt("port");
        }catch (Exception e){
            System.err.println("没有指定port参数,使用默认值9000");
            port = 9002;
        }
        //获取运行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //连接socket获取输入的数据
        DataStreamSource<String> text = env.socketTextStream("jxq-100-73-13-37", port, "\n");
        //计算数据
        DataStream<WordWithCount> windowCount = text.flatMap(new FlatMapFunction<String, WordWithCount>() {
            public void flatMap(String value, Collector<WordWithCount> out) throws Exception {
                String[] splits = value.split("\\s");
                for (String word:splits) {
                    out.collect(new WordWithCount(word,1L));
                }
            }
        })//打平操作,把每行的单词转为<word,count>类型的数据
                //针对相同的word数据进行分组
                .keyBy("word")
                //指定计算数据的窗口大小和滑动窗口大小
                .timeWindow(Time.seconds(2),Time.seconds(1))
                .sum("count");
        //把数据打印到控制台,使用一个并行度
        windowCount.print().setParallelism(1);
        //注意:因为flink是懒加载的,所以必须调用execute方法,上面的代码才会执行
        env.execute("streaming word 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 "WordWithCount{" +
                    "word='" + word + '\'' +
                    ", count=" + count +
                    '}';
        }
    }

}
  • session1-以9002端口启动服务
    在这里插入图片描述
  • 控制台输出:
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flink安装和配置可以按照以下步骤进行: 1. 首先,你需要下载Flink安装包。你可以从Flink的官方网站(https://flink.apache.org/downloads.html)上找到最新的稳定版本,并选择适合你操作系统的版本。 2. 下载完成后,解压缩安装包到你选择的目录。你可以使用以下命令进行解压缩: ``` tar -xzf flink-<version>.tgz ``` 其中,`<version>`是你下载的Flink版本号。 3. 进入解压缩后的目录: ``` cd flink-<version> ``` 4. 配置Flink的环境变量。在终端中执行以下命令: ``` export FLINK_HOME=/path/to/flink-<version> export PATH=$PATH:$FLINK_HOME/bin ``` 将上述命令中的`/path/to/flink-<version>`替换为你的Flink安装路径。 5. 配置Flink的配置文件。进入Flink安装目录下的`conf`文件夹,并复制一份`flink-conf.yaml.template`文件并重命名为`flink-conf.yaml`。 ``` cd conf cp flink-conf.yaml.template flink-conf.yaml ``` 6. 打开`flink-conf.yaml`文件,并根据需要进行配置。在这个文件中,你可以设置Flink的各种参数,如任务管理器的内存大小、并行度等。根据需求进行修改后保存。 7. 配置完成后,你可以启动Flink集群。在终端中执行以下命令: ``` ./bin/start-cluster.sh ``` 这将启动Flink的任务管理器和资源管理器。 8. 要验证Flink是否正常运行,你可以在浏览器中访问`http://localhost:8081`,这是Flink的Web界面。如果能够成功访问并显示Flink的状态信息,则表示安装和配置成功。 以上就是Flink安装与配置过程。希望能对你有所帮助!如果还有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值