Flume学习历程


date: 2023-04-10 19:38:04
tags:大数据 Flume 自学


第1章 Flume概述

1.1 Flume定义

Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集、聚合和传输的系统。Flume基于流式架构,灵活简单。

image-20230412182635560

1.2 Flume基础架构

Flume组成架构如下图所示。

image-20230412182703543

1.2.1 Agent

Agent是一个JVM进程,它以事件的形式将数据从源头送至目的地。

Agent主要有3个部分组成,Source、Channel、Sink。

1.2.2 Source

Source是负责接收数据到Flume Agent的组件。Source组件可以处理各种类型、各种格式的日志数据

1.2.3 Sink

Sink不断地轮询Channel中的事件且批量地移除它们,并将这些事件批量写入到存储或索引系统、或者被发送到另一个Flume Agent。

1.2.4 Channel

Channel是位于Source和Sink之间的缓冲区。因此,Channel允许Source和Sink运作在不同的速率上。Channel是线程安全的,可以同时处理几个Source的写入操作和几个Sink的读取操作。

Flume自带两种Channel:Memory Channel和File Channel。

Memory Channel是内存中的队列。Memory Channel在不需要关心数据丢失的情景下适用。如果需要关心数据丢失,那么Memory Channel就不应该使用,因为程序死亡、机器宕机或者重启都会导致数据丢失。

File Channel将所有事件写到磁盘。因此在程序关闭或机器宕机的情况下不会丢失数据。

1.2.5 Event

传输单元,Flume数据传输的基本单元,以Event的形式将数据从源头送至目的地。Event由HeaderBody两部分组成,Header用来存放该event的一些属性,为K-V结构,Body用来存放该条数据,形式为字节数组。

image-20230412182749045

第2章 Flume入门

2.1 Flume安装部署

2.1.1 安装地址

(1)Flume官网地址:http://flume.apache.org/

(2)文档查看地址:http://flume.apache.org/FlumeUserGuide.html

(3)下载地址:http://archive.apache.org/dist/flume/

2.1.2 安装部署

(1)将apache-flume-1.9.0-bin.tar.gz上传到linux的/opt/software目录下

(2)解压apache-flume-1.9.0-bin.tar.gz到/opt/module/目录下

[hadoop@node02 software]$ tar -zxvf /opt/software/apache-flume-1.9.0-bin.tar.gz -C /opt/module/

(3)修改apache-flume-1.9.0-bin的名称为flume

[hadoop@node02 module]$ mv /opt/module/apache-flume-1.9.0-bin /opt/module/flume

(4)将lib文件夹下的guava-11.0.2.jar删除以兼容Hadoop 3.1.3

[hadoop@node02 lib]$ rm /opt/module/flume/lib/guava-11.0.2.jar

(5)修改conf下的log4j.properties确定日志打印的位置

#console表示同时将日志输出到控制台

flume.root.logger=INFO,LOGFILE,console

#固定日志输出的位置

flume.log.dir=/opt/module/flume/logs

#日志文件的名称

flume.log.file=flume.log

2.2 Flume入门案例

2.2.1 监控端口数据官方案例

1)案例需求:

使用Flume监听一个端口,收集该端口数据,并打印到控制台。

2)实现步骤:

(1)安装netcat工具

[hadoop@node02 software]$ sudo yum install -y nc

(2)判断44444端口是否被占用

[hadoop@node02 flume]$ sudo netstat -nlp | grep 44444

(3)在conf文件夹下创建Flume Agent配置文件nc-flume-log.conf。

[hadoop@node02 conf]$ vim nc-flume-log.conf

(4)在nc-flume-log.conf文件中添加如下内容。

添加内容如下:

# Name the components on this agent

#配置agent

a1.sources = r1

a1.sinks = k1

a1.channels = c1

 

# Describe/configure the source

#配置source

a1.sources.r1.type = netcat

a1.sources.r1.bind = localhost

a1.sources.r1.port = 44444

 

# Describe the sink

配置sink

a1.sinks.k1.type = logger

 

# Use a channel which buffers events in memory

#配置channel容量

a1.channels.c1.type = memory

a1.channels.c1.capacity = 1000

a1.channels.c1.transactionCapacity = 100

 

# Bind the source and sink to the channel

#通过channel绑定source和sink

a1.sources.r1.channels = c1

a1.sinks.k1.channel = c1

注:配置文件来源于官方手册http://flume.apache.org/FlumeUserGuide.html

PS:一个source可以分出多个channel,一个sink只能对应一个channel,但一个channel能对应多个sink

image-20230411204034400

(5)先开启flume监听端口

第一种写法:

[hadoop@node02 flume]$ bin/flume-ng agent --conf conf/ --name a1 --conf-file  conf/nc-flume-log.conf -Dflume.root.logger=INFO,console

第二种写法:

[hadoop@node02 flume]$ bin/flume-ng agent -c conf/ -n a1 -f conf/nc-flume-log.conf -Dflume.root.logger=INFO,console

参数说明:

​ --conf/-c:表示配置文件存储在conf/目录

​ --name/-n:表示给agent起名为a1

​ --conf-file/-f:flume本次启动读取的配置文件是在conf文件夹下的nc-flume-log.conf文件。

​ -Dflume.root.logger=INFO,console :-D表示flume运行时动态修改flume.root.logger参数属性值,并将控制台日志打印级别设置为INFO级别。日志级别包括:log、info、warn、error。日志参数已经在配置文件中修改了,不再需要重复输入。

(6)使用netcat工具向本机的44444端口发送内容

[hadoop@node02 ~]$ nc localhost 44444

hello 

jason

10

(7)在Flume监听页面观察接收数据情况

2021-07-15 13:51:00,236 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:95)] Event: { headers:{} body: 31 30                      10 }

(8)event打印的源码介绍

LoggerSink的process方法:

if (event != null) {

  if (logger.isInfoEnabled()) {

    logger.info("Event: " + EventHelper.dumpEvent(event, maxBytesToLog));

  }

}

dumpEvent方法返回值:buffer是固定长度的字符串,前端是16进制表示的字符的阿斯卡码值。

return "{ headers:" + event.getHeaders() + " body:" + buffer + " }";

2.2.2 实时监控目录下的多个追加文件

Taildir Source适合用于监听多个实时追加的文件,并且能够实现断点续传。

1)案例需求:使用Flume监听整个目录的实时追加文件,并上传至HDFS

2)需求分析

实时读取目录文件到HDFS

image-20230415194622152

3)实现步骤

(1)在conf目录下创建配置文件taildir-flume-hdfs.conf

①创建一个文件

[hadoop@node02 conf]$ vim taildir-flume-hdfs.conf

②添加如下内容

#定义组件

a1.sources = r1

a1.sinks = k1

a1.channels = c1

 

# Describe/configure the source

a1.sources.r1.type = TAILDIR

a1.sources.r1.filegroups = f1 f2

# 必须精确到文件,可以写匹配表达式匹配多个文件

a1.sources.r1.filegroups.f1 = /opt/module/flume/files1/.*file.*

a1.sources.r1.filegroups.f2 = /opt/module/flume/files2/.*log.*

# 实现断点续传的文件存放位置 不改有默认位置也能实现断点续传

a1.sources.r1.positionFile = /opt/module/flume/taildir_position.json

 

 

# Describe the sink

a1.sinks.k1.type = hdfs

# 地址值可以填写hdfs://node02:8020也可以省略,flume会自动读取hadoop配置文件信息获取地址

a1.sinks.k1.hdfs.path = hdfs://node02:8020/flume/%Y%m%d/%H

#上传文件的前缀

a1.sinks.k1.hdfs.filePrefix = log-

 

#是否使用本地时间戳

a1.sinks.k1.hdfs.useLocalTimeStamp = true

 

#设置文件类型 分为二进制文件SequenceFile和文本文件DataStream(不能压缩) 和CompressedStream(可以压缩)

a1.sinks.k1.hdfs.fileType = DataStream

 

#多久生成一个新的文件

a1.sinks.k1.hdfs.rollInterval = 30

#设置每个文件的滚动大小大概是128M

a1.sinks.k1.hdfs.rollSize = 134217700

#文件的滚动与Event数量无关

a1.sinks.k1.hdfs.rollCount = 0 

 

# Use a channel which buffers events in memory

a1.channels.c1.type = memory

a1.channels.c1.capacity = 1000

a1.channels.c1.transactionCapacity = 100

 

# Bind the source and sink to the channel

a1.sources.r1.channels = c1

a1.sinks.k1.channel = c1

(2)向files文件夹中追加内容

在/opt/module/flume目录下创建files1文件夹

[hadoop@node02 flume]$ mkdir files1

[hadoop@node02 flume]$ mkdir files2

(3)启动监控文件夹命令

[hadoop@node02 flume]$ bin/flume-ng agent --conf conf/ --name a1 --conf-file conf/taildir-flume-hdfs.conf

#也可以这样写
[hadoop@node02 flume]$ bin/flume-ng agent -n a1 -c conf/ -f conf/taildir-flume-hdfs.conf

向upload文件夹中添加文件

[hadoop@node02 files1]$ echo 111 >> file1.txt

[hadoop@node02 files1]$ echo 111 >> file2.txt

(4)查看HDFS上的数据

image-20230412023452073 image-20230412023519831

PS:log.1681237600047是一个时间戳,可以使用时间戳转换器来查看具体时间(百度搜索)

Taildir说明:

Taildir Source维护了一个json格式的position File,其会定期的往position File中更新每个文件读取到的最新的位置,因此能够实现断点续传。Position File的格式如下:

{“inode”:2496272,“pos”:12,“file”:“/opt/module/flume/files1/file1.txt”}

{“inode”:2496275,“pos”:12,“file”:“/opt/module/flume/files1/file2.txt”}

注:Linux中储存文件元数据的区域就叫做inode,每个inode都有一个号码,操作系统用inode号码来识别不同的文件,Unix/Linux系统内部不使用文件名,而使用inode号码来识别文件。TailDir source使用inode和文件的全路径一起识别同一个文件,所以修改文件名之后如果表达式也能够匹配上,会再重新读取一份文件的数据。

第3章 Flume进阶

3.1 Flume事务

image-20230412183102997

3.2 Flume Agent内部原理

image-20230412183159660

组件介绍:

1)ChannelSelector

ChannelSelector的作用就是选出Event将要被发往哪个Channel。其共有两种类型,分别是Replicating(复制)和Multiplexing(多路复用)。

ReplicatingSelector会将同一个Event发往所有的Channel,Multiplexing会根据相应的原则,将不同的Event发往不同的Channel。

2)SinkProcessor

SinkProcessor共有三种类型,分别是DefaultSinkProcessor(默认1对1)、LoadBalancingSinkProcessor(负载均衡)和FailoverSinkProcessor(故障转移)

DefaultSinkProcessor对应的是单个的Sink,LoadBalancingSinkProcessor和FailoverSinkProcessor对应的是Sink Group,LoadBalancingSinkProcessor可以实现负载均衡的功能,FailoverSinkProcessor可以错误恢复的功能。

3.3 Flume企业开发案例

3.3.1 复制案例

1)案例需求

使用Flume-1监控文件变动,Flume-1将变动内容传递给Flume-2,Flume-2负责存储到HDFS。同时Flume-1将变动内容传递给Flume-3,Flume-3负责输出到Local FileSystem。

2)需求分析

单数据源多口输出案例

image-20230412183342650

3)实现步骤

(1)准备工作

在/opt/module/flume/conf目录下创建group1文件夹。

[hadoop@node02 conf]$ mkdir group1/

在/opt/module/flume/目录下创建flume3datas文件夹。

[hadoop@node02 flume]$ mkdir flume3datas

(2)创建flume1.conf

配置1个接收日志文件的source和两个channel、两个sink,分别输送给flume2和flume3。

①编辑配置文件

[hadoop@node02 group1]$ vim flume1.conf

②添加如下内容

# 定义组件
a1.sources = r1
a1.channels = c1 c2
a1.sinks = k1 k2

# 配置sources
# 查看官方文档,不同的sources的配置要求不一样
#必须配置
a1.sources.r1.type = TAILDIR 
a1.sources.r1.filegroups = f1 f2
a1.sources.r1.filegroups.f1 = /opt/module/flume/files1/.*file.*
a1.sources.r1.filegroups.f2 = /opt/module/flume/files2/.*log.*
#断点续存,节点断开后,方便继续传输文件
a1.sources.r1.positionFile = /opt/module/flume/taildir_position.json 

# 配置channels
# 查看官方文档,不同的channels的配置要求不一样
a1.channels.c1.type = memory
a1.channels.c1.capacity = 100
a1.channels.c1.transactionCapacity = 100

a1.channels.c2.type = memory
a1.channels.c2.capacity = 100
a1.channels.c2.transactionCapacity = 100

# 配置sinks
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = node02
a1.sinks.k1.port = 4141

a1.sinks.k2.type = avro
a1.sinks.k2.hostname = node02
a1.sinks.k2.port = 4142


# 组装
a1.sources.r1.channels = c1 c2
a1.sinks.k1.channel = c1
a1.sinks.k2.channel = c2


(3)创建flume2.conf

配置上级Flume输出的Source,输出是到HDFS的Sink。

①编辑配置文件

[hadoop@node02 group1]$ vim flume2.conf

②添加如下内容

# 定义组件
a1.sources = r1
a1.channels = c1 
a1.sinks = k1 

# 配置sources
a1.sources.r1.type = avro
a1.sources.r1.bind = node02
a1.sources.r1.port = 4141

# 配置channels
# 查看官方文档,不同的channels的配置要求不一样
a1.channels.c1.type = memory
a1.channels.c1.capacity = 100
a1.channels.c1.transactionCapacity = 100

# 配置sinks
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = /flume/%Y-%m-%d/%H
a1.sinks.k1.hdfs.filePrefix = log
a1.sinks.k1.hdfs.fileType = DataStream
#生成新文件的时间间隔
a1.sinks.k1.hdfs.rollInterval = 10
#每个文件的滚动大小
a1.sinks.k1.hdfs.rollSize = 134217700
#文件的滚动与Event数量无关
a1.sinks.k1.hdfs.useLocalTimeStamp = true


# 组装
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

(4)创建flume3.conf

配置上级Flume输出的Source,输出是到本地目录的Sink。

①编辑配置文件

[hadoop@node02 group1]$ vim flume3.conf

②添加如下内容

# 定义组件
a1.sources = r1
a1.channels = c1 
a1.sinks = k1 

# 配置sources
a1.sources.r1.type = avro
a1.sources.r1.bind = node02
a1.sources.r1.port = 4142

# 配置channels
# 查看官方文档,不同的channels的配置要求不一样
a1.channels.c1.type = memory
a1.channels.c1.capacity = 100
a1.channels.c1.transactionCapacity = 100

# 配置sinks
a1.sinks.k1.type = file_roll
a1.sinks.k1.sink.directory = /opt/module/flume/flume3datas

# 组装
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

提示:输出的本地目录必须是已经存在的目录,如果该目录不存在,并不会创建新的目录。

(5)启动Hadoop

#脚本文件启动hadoop
[hadoop@node02 hadoop-3.1.3]$ all.sh start

(6)执行配置文件

分别启动对应的flume进程:flume1,flume2,flume3。

注意:这里要先启动flume2、flume3再启动flume1,因为flume是监听4141、4142两个端口,flume2、flume3需要启动4141、4142这两个端口才能被flume1监听

[hadoop@node02 flume]$ bin/flume-ng agent -n a1 -c conf/ -f conf/group1/flume3.conf

[hadoop@node02 flume]$ bin/flume-ng agent -n a1 -c conf/ -f conf/group1/flume2.conf
 
[hadoop@node02 flume]$ bin/flume-ng agent -n a1 -c conf/ -f conf/group1/flume1.conf

(7)向监控的文件传入数据

[hadoop@node02 files1]$ echo hello >> file1.txt

(8)检查HDFS上数据

image-20230413190341705

(8)检查/opt/module/datas/flume3目录中数据

[hadoop@node02 flume3]$ ll

总用量 8

-rw-rw-r–. 1 hadoop hadoop 5942 5月 22 00:09 1526918887550-3

3.3.2 多路复用及拦截器的使用

1)案例需求

使用Flume采集服务器本地日志,需要按照日志类型的不同,将不同种类的日志发往不同的分析系统。

2)需求分析

在实际的开发中,一台服务器产生的日志类型可能有很多种,不同类型的日志可能需要发送到不同的分析系统。此时会用到Flume的channel selecter中的Multiplexing结构,Multiplexing的原理是,根据event中Header的某个key的值,将不同的event发送到不同的Channel中,所以我们需要自定义一个Interceptor,为不同类型的event的Header中的key赋予不同的值。

在该案例中,我们以端口数据模拟日志,以数字(单个)和字母(单个)模拟不同类型的日志,我们需要自定义interceptor区分数字和字母,将其分别发往不同的分析系统(Channel)。

Interceptor和Multiplexing Channel Selector

image-20230413191654259

3)实现步骤

(1)创建一个maven项目,并引入以下依赖。

<dependencies>

  <dependency>

   <groupId>org.apache.flume</groupId>

   <artifactId>flume-ng-core</artifactId>

   <version>1.9.0</version>

  </dependency>

</dependencies>

(2)定义CustomInterceptor类并实现Interceptor接口。

package com.jason.flume;

import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;
import org.apache.velocity.runtime.directive.Foreach;

import java.util.List;
import java.util.Map;

/**
 * @author Jason
 * @date 2023/4/13
 * @package_name com.jason.flume
 */
public class MyInterceptor implements Interceptor {
    //这可以不管
    public void initialize() {

    }

    public Event intercept(Event event) {
        //配合channel选择器在header中添加一个(key,value)分别对应 (state,number/letter)
        //
        byte[] body = event.getBody();
        byte b = body[0];
        Map<String, String> headers = event.getHeaders();
        if (b >= '0' && b <= '9'){
            //是数字
            headers.put("state","number");
        }else if ((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')){
            //是字母
            headers.put("state","letter");

        }
        return event;
    }

    public List<Event> intercept(List<Event> list) {
        for (Event event : list) {
            intercept(event);
        }
        return list;
    }
    //这可以不管
    public void close() {

    }

    //还需要实现一个builder
    public static class MyBuilder implements Interceptor.Builder{

        //创造拦截器
        public Interceptor build() {
            return new MyInterceptor();
        }
        //这可以不管
        public void configure(Context context) {

        }
    }
}

打包过程:

  1. 点击clean

    image-20230415152137688
  2. 点击package

    image-20230415152428778

  3. 在target目录下点击jar包的右键,找到jar包所在的本地目录

    image-20230415152947536
  4. 上传jar包到node02上,flume的lib目录下

    image-20230415153240873

    image-20230415153334121

(3)编辑flume配置文件

创建新文件conf/group2/flume1,创建新文件conf/group2/flume2,创建新文件conf/group2/flume3。

为Flume1配置1个netcat source,1个sink group(2个avro sink),并配置相应的ChannelSelector和interceptor。

#Name the components on this agent

a1.sources = r1

a1.sinks = k1 k2

a1.channels = c1 c2

 

#Describe/configure the source

a1.sources.r1.type = netcat

a1.sources.r1.bind = localhost

a1.sources.r1.port = 44444





a1.sources.r1.selector.type = multiplexing

#使用headers中的哪些参数

a1.sources.r1.selector.header = type

a1.sources.r1.selector.mapping.number = c2

a1.sources.r1.selector.mapping.letter = c1

a1.sources.r1.selector.default = c1

 

#拦截器配置

a1.sources.r1.interceptors = i1

a1.sources.r1.interceptors.i1.type = com.jason.flume.MyInterceptor$MyBuilder

 

#Describe the sink

a1.sinks.k1.type = avro

a1.sinks.k1.hostname = node02

a1.sinks.k1.port = 4141

 

a1.sinks.k2.type = avro

a1.sinks.k2.hostname = node02

a1.sinks.k2.port = 4142

 

#Use a channel which buffers events in memory

a1.channels.c1.type = memory

a1.channels.c1.capacity = 1000

a1.channels.c1.transactionCapacity = 100

 

a1.channels.c2.type = memory

a1.channels.c2.capacity = 1000

a1.channels.c2.transactionCapacity = 100

 

#Bind the source and sink to the channel

a1.sources.r1.channels = c1 c2

a1.sinks.k1.channel = c1

a1.sinks.k2.channel = c2

为Flume2配置一个avro source和一个logger sink。

a1.sources = r1

a1.sinks = k1

a1.channels = c1

 

a1.sources.r1.type = avro

a1.sources.r1.bind = node02

a1.sources.r1.port = 4141

 

a1.sinks.k1.type = logger

 

a1.channels.c1.type = memory

a1.channels.c1.capacity = 1000

a1.channels.c1.transactionCapacity = 100

 

a1.sinks.k1.channel = c1

a1.sources.r1.channels = c1

为Flume3配置一个avro source和一个logger sink。

a1.sources = r1

a1.sinks = k1

a1.channels = c1

 

a1.sources.r1.type = avro

a1.sources.r1.bind = node02

a1.sources.r1.port = 4142

 

a1.sinks.k1.type = logger

 

 

a1.channels.c1.type = memory

a1.channels.c1.capacity = 1000

a1.channels.c1.transactionCapacity = 100

 

a1.sinks.k1.channel = c1

a1.sources.r1.channels = c1

(4)先分别启动flume2和flume3,最后启动flume1。

(5)在node02使用netcat向localhost:44444发送字母和数字。

[hadoop@node02 ~]$ nc localhost 44444

(6)观察flume2和flume3打印的日志。

3.3.3 聚合案例

1)案例需求:

node02上的Flume-1监控文件/opt/module/flume/files1/.file.

node03上的Flume-2监控某一个端口的数据流,

Flume-1与Flume-2将数据发送给node04上的Flume-3,Flume-3将最终数据打印到控制台。

2)需求分析

多数据源汇总

image-20230412185056186

3)实现步骤:

(1)准备工作

①分发Flume

[hadoop@node02 module]$ xsync flume

②在node02、node03以及node04的/opt/module/flume/conf目录下创建一个group3文件夹。

[hadoop@node02 conf]$ mkdir group3

[hadoop@node03 conf]$ mkdir group3

[hadoop@node04 conf]$ mkdir group3

(2)创建flume1.conf

配置Source用于监控本地文件,配置Sink输出数据到下一级Flume。

①在node02上编辑配置文件

[hadoop@node02 group3]$ vim flume1.conf 

②添加如下内容

# 定义组件
a1.sources = r1
a1.channels = c1
a1.sinks = k1

# 配置sources
# 查看官方文档,不同的sources的配置要求不一样
#必须配置
a1.sources.r1.type = TAILDIR 
a1.sources.r1.filegroups = f1 f2
a1.sources.r1.filegroups.f1 = /opt/module/flume/files1/.*file.*
a1.sources.r1.filegroups.f2 = /opt/module/flume/files2/.*log.*
#断点续存,节点断开后,方便继续传输文件
a1.sources.r1.positionFile = /opt/module/flume/taildir_position.json 

# 配置channels
# 查看官方文档,不同的channels的配置要求不一样
a1.channels.c1.type = memory  
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# 配置sinks
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = node04
a1.sinks.k1.port = 4141


# 组装
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

(3)创建flume2.conf

配置Source监控端口44444数据流,配置Sink数据到下一级Flume:

①在node03上编辑配置文件

[hadoop@node02 group3]$ vim flume2.conf

②添加如下内容

# 定义组件
a1.sources = r1
a1.channels = c1
a1.sinks = k1

# 配置sources
# 查看官方文档,不同的sources的配置要求不一样
#必须配置
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444

# 配置channels
# 查看官方文档,不同的channels的配置要求不一样
a1.channels.c1.type = memory  
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# 配置sinks
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = node04
a1.sinks.k1.port = 4141

# 组装
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

(4)创建flume3.conf

配置source用于接收flume1与flume2发送过来的数据流,最终合并后sink到控制台。

①在node04上编辑配置文件

[hadoop@node04 group3]$ touch flume3.conf

[hadoop@node04 group3]$ vim flume3.conf

②添加如下内容

# 定义组件
a1.sources = r1
a1.channels = c1
a1.sinks = k1

# 配置sources
# 查看官方文档,不同的sources的配置要求不一样
#必须配置
a1.sources.r1.type = avro
a1.sources.r1.bind = node04
a1.sources.r1.port = 4141

# 配置channels
# 查看官方文档,不同的channels的配置要求不一样
a1.channels.c1.type = memory  
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# 配置sinks
a1.sinks.k1.type = logger

# 组装
a1.sources.r1.channels = c1
a.sinks.k1.channel = c1

(5)执行配置文件

分别开启对应配置文件:flume1.conf,flume2.conf,flume3.conf。

[hadoop@node02 flume]$ bin/flume-ng agent -n a1 -c conf/ -f  conf/group3/flume1.conf

[hadoop@node03 flume]$ bin/flume-ng agent -n a1 -c conf/ -f  conf/group3/flume2.conf

[hadoop@node04 flume]$ bin/flume-ng agent -n a1 -c conf/ -f  conf/group3/flume3.conf

(6)在node02上向/opt/module/flume目录下的group.log追加内容

[hadoop@node02 files1]$ echo 'hello' > file1

(7)在node03上向44444端口发送数据

[hadoop@node03 flume]$ nc node03 44444

(8)检查node04上数据

第4章 Flume数据流监控

4.1 Ganglia的安装与部署

Ganglia由gmond、gmetad和gweb三部分组成。

gmond(Ganglia Monitoring Daemon)是一种轻量级服务,安装在每台需要收集指标数据的节点主机上。使用gmond,你可以很容易收集很多系统指标数据,如CPU、内存、磁盘、网络和活跃进程的数据等。

gmetad(Ganglia Meta Daemon)整合所有信息,并将其以RRD格式存储至磁盘的服务。

gweb(Ganglia Web)Ganglia可视化工具,gweb是一种利用浏览器显示gmetad所存储数据的PHP前端。在Web界面中以图表方式展现集群的运行状态下收集的多种不同指标数据。

1)安装ganglia

(1)规划

node02:   web gmetad gmod 

node03:   gmod

node04:   gmod

(2)在node02、node03、node04分别安装epel-release依赖

[hadoop@node02 flume]$ sudo yum -y install epel-release

(3)在102 安装

[hadoop@node02 flume]$ sudo yum -y install ganglia-gmetad 

[hadoop@node02 flume]$ sudo yum -y install ganglia-web

[hadoop@node02 flume]$ sudo yum -y install ganglia-gmond

(4)在103 和 104 安装

[hadoop@node02 flume]$ sudo yum -y install ganglia-gmond

2 )在102修改配置文件/etc/httpd/conf.d/ganglia.conf

[hadoop@node02 flume]$ sudo vim /etc/httpd/conf.d/ganglia.conf

修改为红颜色的配置:

#
#Ganglia monitoring system php web frontend
#
Alias /ganglia /usr/share/ganglia

<Location /ganglia>

 #Order deny,allow

 #Deny from all

 #Allow from 127.0.0.1

 #Allow from ::1

#Allow from .example.com

 Require all granted

</Location> 

3)在102修改配置文件/etc/ganglia/gmetad.conf

[hadoop@node02 flume]$ sudo vim /etc/ganglia/gmetad.conf

修改为:

data_source "node02" node02

4)在102 103 104修改配置文件/etc/ganglia/gmond.conf

[hadoop@node02 flume]$ sudo vim /etc/ganglia/gmond.conf 

#30行,修改为: 

cluster {

 name = "node02"

 owner = "unspecified"

 latlong = "unspecified"

 url = "unspecified"

}

udp_send_channel {

 #bind_hostname = yes # Highly recommended, soon to be default.

            # This option tells gmond to use a source address

            # that resolves to the machine's hostname. Without

            # this, the metrics may appear to come from any

            # interface and the DNS names associated with

            # those IPs will be used to create the RRDs.

# mcast_join = 239.2.11.71

#数据发送给node02

 host = node02

 port = 8649

 ttl = 1

}

udp_recv_channel {

# mcast_join = 239.2.11.71

 port = 8649

#接收来自任意连接的数据

 bind = 0.0.0.0

 retry_bind = true

#Size of the UDP buffer. If you are handling lots of metrics you really should bump it up to e.g. 10MB or even higher.

buffer = 10485760

}

5)在102修改配置文件/etc/selinux/config

[hadoop@node02 flume]$ sudo vim /etc/selinux/config

修改为:

#This file controls the state of SELinux on the system.

#SELINUX= can take one of these three values:

#enforcing - SELinux security policy is enforced.

#permissive - SELinux prints warnings instead of enforcing.

#disabled - No SELinux policy is loaded.

SELINUX=disabled

#SELINUXTYPE= can take one of these two values:

#targeted - Targeted processes are protected,

#mls - Multi Level Security protection.

SELINUXTYPE=targeted

提示:selinux本次生效关闭必须重启,如果此时不想重启,可以临时生效之:

[hadoop@node02 flume]$ sudo setenforce 0

6)启动ganglia

(1)在102 103 104 启动

[hadoop@node02 flume]$ sudo systemctl start gmond

(2)在102 启动

[hadoop@node02 flume]$ sudo systemctl start httpd

[hadoop@node02 flume]$ sudo systemctl start gmetad

7)打开网页浏览ganglia页面

http://node02/ganglia

加载速度较慢。

提示:如果完成以上操作依然出现权限不足错误,请修改/var/lib/ganglia目录的权限:

[hadoop@node02 flume]$ sudo chmod -R 777 /var/lib/ganglia

image-20230415203306395

4.2 操作Flume测试监控

1)启动Flume任务

[hadoop@node02 flume]$ bin/flume-ng agent \

-c conf/ \

-n a1 \

-f conf/nc-flume-log.conf \

-Dflume.monitoring.type=ganglia \

-Dflume.monitoring.hosts=node02:8649
# 反斜杠'\'是多行命令

​ 启动Flume任务后,监控页面多了flume metrics模块

image-20230415203918376

主要关注以下四个监控图

EventPutAttemptCount : put 的次数 EventPutSuccessCount : put 成功的次数

EventTakeAttemptCount : take 的次数 EventTakeSuccessCount : take 成功的次数

通过判断Attempt和Success的次数是否相等,去判断flume的数据传输情况

image-20230415204353007

2)发送数据观察ganglia监测图

[hadoop@node02 flume]$ nc localhost 44444
image-20230415204936143

node02上监控到数据:

image-20230415205037442

样式如图:

通过监控图可以看到,Attempt和Success的次数相等,则flume的一个agent无异常

image-20230415205116175

图例说明:

字段(图表名称)字段含义
EventPutAttemptCountsource尝试写入channel的事件总数量
EventPutSuccessCount成功写入channel且提交的事件总数量
EventTakeAttemptCountsink尝试从channel拉取事件的总数量。
EventTakeSuccessCountsink成功读取的事件的总数量
StartTimechannel启动的时间(毫秒)
StopTimechannel停止的时间(毫秒)
ChannelSize目前channel中事件的总数量
ChannelFillPercentagechannel占用百分比
ChannelCapacitychannel的容量
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值