Flume学习笔记

视频地址:https://www.bilibili.com/video/BV1wf4y1G7EQ/

定义

Flume是一个高可用的、高可靠的、分布式的海量日志采集、聚合和传输的系统。
Flume高最要的作用就是实时读取服务器本地磁盘的数据,将数据写入HDFS。
官网:https://flume.apache.org/releases/content/1.9.0/FlumeUserGuide.html
源码包:https://gitee.com/apache/flume.git

架构图

在这里插入图片描述

1.Agent

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

2.Source

Source是负责接收数据到Flume Agent的组件。Source 组件可以处理各种类型、各种格式的日志数据,包括 avro、thrift、exec、jms、spooling directory、netcat、taildir、sequence generator、syslog、http、legacy。

3.Sink

Sink 不断地轮询Channel中的事件且批量地移除它们,并将这些事件批量写入到存储或索引系统、或者被发送到另一个Flume Agent。
Sink 组件目的地包括hdfs、logger、avro、thrift、ipc、file、HBase、solr、自定义。

4.Channel

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

  • Memory Channel是内存中的队列。Memory Channel在不需要关心数据丢失的情景下适用。如果需要关心数据丢失,那么MemoryChanne1就不应该使用,因为程序死亡、机器宕机或者重启都会导致数据丢失。
  • FileChannel将所有事件写到磁盘。因此在程序关闭或机器宕机的情况下不会丢失数据。

5.Event

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

示例

官方文档
在这里插入图片描述

1.配置文件

# example.conf: A single-node Flume configuration# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444
# Describe the sink
a1.sinks.k1.type = logger
# 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
  1. 可以配置多个source、sink、channel
  2. 事务容量要比总容量小,事务容量指单次最大的事件数量
  3. 一个source可以绑定多个channel
  4. 一个sink只能绑定一个channel
  5. 一个channel可以绑定多个sink

2.启动命令

bin/flume-ng agent -n a1 -c conf -f conf/flume-conf
  1. -n --name : agent名称
  2. -c --conf: conf目录
  3. -f --conf-file: 配置文件
  4. -Dflume.root.logger=INFO,console 打印日志

3.Agent

3.1.内部原理

在这里插入图片描述

3.1.1.Channel Selectors
3.1.1.1.ReplicatingChannel Selector

(默认)将source过来的events发往所有channel
在这里插入图片描述
示例
在这里插入图片描述

3.1.1.2.MultiplexingChannel Selector

可以配置发往哪些Chanmel
在这里插入图片描述
示例
在这里插入图片描述

3.1.2.SinkProcessor

3.1.2.1.DefaultSinkProcessor

只接收一个请求,只能绑定一个Sink

3.1.2.2.LoadBalanceingSinkProcessor

负载均衡,分散到其他sink中
在这里插入图片描述示例
在这里插入图片描述

3.1.2.3.FailoverSinkProcessor

故障转移,按优先级排序

在这里插入图片描述
示例
在这里插入图片描述

4.Source

4.1.exec

在这里插入图片描述

4.1.1.示例

监控文件内容
在这里插入图片描述

4.1.2.缺点

不能断点续传

4.2.spooldir

在这里插入图片描述

4.2.1.示例

监控目录中新文件
在这里插入图片描述

4.2.2.缺点

不能动态监听变化文件

4.3.Taildir

在这里插入图片描述

4.3.1.示例

在这里插入图片描述

4.3.2.优点

1.监控不同目录
2.

4.4.Avro

在这里插入图片描述

4.4.1.示例

在这里插入图片描述

4.4.2.缺点

5.Sink

5.1.HDFS

在这里插入图片描述
在这里插入图片描述

5.1.1.示例

在这里插入图片描述

5.2.Avro

在这里插入图片描述

5.2.1.示例

在这里插入图片描述

5.3.FileRoll

在这里插入图片描述

5.3.1.示例

在这里插入图片描述

6.修改源码

下载源码:https://gitee.com/apache/flume.git

Flume拓扑结构

1.简单串联

1.1.结构图

在这里插入图片描述

1.2.示例

2.复制和多路利用

2.1.结构图

在这里插入图片描述

2.2.示例

2.2.1.单数据源多出口案例
2.2.1.1.需求

在这里插入图片描述

2.2.1.2.flume-file-flume.conf
# Name the comnentson this agent
a1.sources=r1
a1.channels=c1
a1.sinks = k1 k2

#Describe/configure the source
a1.sources.r1.type =exec
a1.sources.r1.command=tail -F /opt/module/hive/logs/hive.log
a1.sources.r1.shell= /bin/bash -c

#Describe the sink
a1.sinks.kl.type =avro
a1.sinks.k1.hostname =hadoop102
a1.sinks.k1.port =4141

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

#Describe the channe
a1.channels.c1.type=memory
al.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100

#将数据流复制给所有channele
a1.sources.r1.selector.type=relicating

# Bind the source and sink to the channel
a1.sources.r1.channels=c1 c2
al.sinks.k1.channel=c1
a1.sinks.k2.channel=c2
2.2.1.2.1.启动命令
/bin/flume-gn agent -n a1 -c conf/ -f /job/group1/flume-file-flume.conf 
2.2.1.3.flume-flume-hdfs.conf
# Name the comnentson this agent
a2.sources=r1
a2.channels=c1
a2.sinks = k1

#Describe/configure the source
a2.sources.r1.type =avro
a2.sources.r1.bind=hadoop102
a2.sources.r1.port=4141

#Describe the sink
a2.sinks.k1.type =hdfs
a2.sinks.k1.hdfs.path =hdfs://hadoop102:9820/flume2/%Y%m%d/%H
# 上传文件的前缀
a2.sinks.k1.hdfs.filePrefix=flume-
# 是否按照时间滚动文件夹
a2.sinks.k1.hdfs.round=true
# 多少时间单位创建一个新的文件夹
a2.sinks.k1.hdfs.roundValue=1
# 重新定义时间单位
a2.sinks.k1.hdfs.roundUnit=hour
a2.sinks.k1.hdfs.useLocalTimeStamp=true
# 积赞多少个event才flush到Hdfs一次
a2.sinks.k1.hdfs.batchSize=100
# 设置文件类型,可支持压缩
a2.sinks.k1.hdfs.fileType=DataStream
# 设置多久生成一个新的文件
a2.sinks.k1.hdfs.rollInterval=600
# 设置每个文件的滚动大小大概是128M
a2.sinks.k1.hdfs.rollSize=134217700
# 文件的滚动与Event数量无关
a2.sinks.k1.hdfs.rollCount=0

#Describe the channe
a2.channels.c1.type=memory
a2.channels.c1.capacity=1000
a2.channels.c1.transactionCapacity=100

# Bind the source and sink to the channel
a2.sources.r1.channels=c1
a2.sinks.k1.channel=c1
2.2.1.3.1.启动命令
/bin/flume-gn agent -n a2 -c conf/ -f /job/group1/flume-flume-hdfs.conf 
2.2.1.4.flume-flume-dir.conf
# Name the comnentson this agent
a3.sources=r1
a3.channels=c1
a3.sinks = k1

#Describe/configure the source
a3.sources.r1.type =avro
a3.sources.r1.bind=hadoop102
a3.sources.r1.port=4142

#Describe the sink
a3.sinks.k1.type =file_roll
a3.sinks.k1.directory=/opt/module/data/flume3

#Describe the channe
a3.channels.c1.type=memory
a3.channels.c1.capacity=1000
a3.channels.c1.transactionCapacity=100

# Bind the source and sink to the channel
a3.sources.r1.channels=c1
a3.sinks.k1.channel=c1
2.2.1.4.1.提示

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

2.2.1.4.2.启动命令
/bin/flume-gn agent -n a3 -c conf/ -f /job/group1/flume-flume-dir.conf 

3.负载均衡和故障转移

3.1.结构图

在这里插入图片描述

3.2.负载均衡示例

3.2.1.需求
3.2.2.flume-netcat-flume.conf
# Name the comnentson this agent
a1.sources=r1
a1.channe1s=c1
a1.sinks= k1 k2
a1.sinkgroups =g1


#Describe/configure the source
a1.sources.rl.type =netcat
a1.sources.r1.bind=localhost
a1.sources.rl.port=44444

a1.sinkgroups.g1.processor.type=failover
a1.sinkgroups.g1.processor.priority.k1=5
a1.sinkgroups.g1.processor.priority.k2=10
a1.sinkgroups.g1.processor.maxpenalty=10000

#Describe the sink
a1.sinks.kl.type =avro
a1.sinks.k1.hostname =hadoop102
a1.sinks.k1.port =4141

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

#Describe the channe
a1.channels.c1.type=memory
al.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100

a1.sinkgroups.g1.sinks = k1 k2
a1.sources.r1.channels=c1
a1.sink.k1.channel=c1
a1.sink.k2.channel=c1
3.3.故障转移示例
3.3.1.需求

在这里插入图片描述

3.3.2. flume-netcat-flume.conf
# Name the comnentson this agent
a1.sources=r1
a1.channe1s=c1
al.sinkgroups =g1
a1.sinks = k1 k2

#Describe/configure the source
al.sources.rl.type =netcat
a1.sources.r1.bind=localhost
al.sources.rl.port=44444

al.sinkgroups.g1.processor.type=failover
a1.sinkgroups.g1.processor.priority.k1=5
al.sinkgroups.g1.processor.priority.k2=10
a1.sinkgroups.g1.processor.maxpenalty=10000

#Describe the sink
al.sinks.kl.type =avro
al.sinks.k1.hostname =hadoop102
al.sinks.k1.port =4141

al.sinks.k2.type =avro
al.sinks.k2.hostname =hadoop102
al.sinks.k2.port =4142

#Describe the channe
a1.channels.c1.type=memory
al.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
3.3.3.启动命令
/bin/flume-ng agent -n a1 -c /conf -f /group2/flume-netcat-flume.conf 
3.3.4.flume-flume-console1.conf
# Name the comnentson this agent
a2.sources=r1
a2.channe1s=c1
a2.sinks = k1

#Describe/configure the source
a2.sources.rl.type =avro
a2.sources.r1.bind=hadoop102
a2.sources.rl.port=4141

#Describe the sink
a2.sinks.kl.type =logger

#Describe the channe
a2.channels.c1.type=memory
a2.channels.c1.capacity=1000
a2.channels.c1.transactionCapacity=100

a2.sources.r1.channels=c1
a2.sinks.k1.channel=c1
3.3.5.启动命令
/bin/flume-gn agent -c conf/ -n a2 -f job/group2/flume-flume-console1.conf
3.3.6.flume-flume-console2.conf
# Name the comnentson this agent
a3.sources=r1
a3.channe1s=c1
a3.sinks = k1

#Describe/configure the source
a3.sources.rl.type =avro
a3.sources.r1.bind=hadoop102
a3.sources.rl.port=4142

#Describe the sink
a3.sinks.kl.type =logger

#Describe the channe
a3.channels.c1.type=memory
a3.channels.c1.capacity=1000
a3.channels.c1.transactionCapacity=100

a3.sources.r1.channels=c1
a3.sinks.k1.channel=c1
3.3.7.启动命令
/bin/flume-gn agent -c conf/ -n a3 -f job/group2/flume-flume-console2.conf

4.聚合

4.1.结构图

在这里插入图片描述

4.2.示例
4.2.1.需求

在这里插入图片描述

自定义源码

1.引入依赖

<dependency>
    <groupId>org.apache.flume</groupId>
    <artifactId>flume-ng-core</artifactId>
    <version>1.9.0</version>
</dependency>

2.自定义Interceptor

package com.flume;

import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;

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

/**
 * @author : Song
 * @description
 * @date : 2024/4/2
 */
public class TypeInterceptor implements Interceptor {
    @Override
    public void initialize() {

    }

    @Override
    public Event intercept(Event event) {

        // 1.获取head&body
        Map<String, String> headers = event.getHeaders();
        String body = new String(event.getBody());

        if (body.contains("type")) {
            headers.put("type", "first");
        }else {
            headers.put("type","second");
        }
        return event;
    }

    @Override
    public List<Event> intercept(List<Event> list) {
        return null;
    }

    @Override
    public void close() {

    }

    public static class Builder implements Interceptor.Builder{
        @Override
        public Interceptor build() {
            return new TypeInterceptor();
        }

        @Override
        public void configure(Context context) {

        }
    }
}

3.自定义Source

package com.flume;

import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.EventDeliveryException;
import org.apache.flume.PollableSource;
import org.apache.flume.conf.Configurable;
import org.apache.flume.event.SimpleEvent;
import org.apache.flume.source.AbstractSource;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : Song
 * @description
 * @date : 2024/4/2
 */
public class MySource extends AbstractSource implements Configurable, PollableSource {

    private String prefix;

    private String subfix;

    // 获取数据封闭成event并写入channel,这个方法将被循环调用
    @Override
    public Status process() throws EventDeliveryException {
        Event event = new SimpleEvent();

        Map<String, String> header = new HashMap<>();
        event.setHeaders(header);
        event.setBody("body".getBytes());
        return Status.READY;
    }

    // backoff步长
    @Override
    public long getBackOffSleepIncrement() {
        return 0;
    }

    //backoff最长时间
    @Override
    public long getMaxBackOffSleepInterval() {
        return 0;
    }

    // 初始化context
    @Override
    public void configure(Context context) {
        prefix = context.getString("pre", "per-");
        subfix = context.getString("sub");
    }
}

4.自定义Sink

package com.flume;

import org.apache.flume.*;
import org.apache.flume.conf.Configurable;
import org.apache.flume.sink.AbstractSink;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author : Song
 * @description
 * @date : 2024/4/2
 */
public class MySink extends AbstractSink implements Configurable {

    private Logger log = LoggerFactory.getLogger(MySink.class);

    @Override
    public Status process() throws EventDeliveryException {

        Status result = Status.READY;
        Channel channel = getChannel();
        Transaction transaction = channel.getTransaction();
        //开始事务
        transaction.begin();

        try {
            Event event;

            do {
                event = channel.take();
            } while (event == null);
            log.info("事件!!!");

            // 提交事务
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚事务
            transaction.rollback();
        } finally {
            // 关闭事务
            transaction.close();
        }
        return result;
    }

    @Override
    public void configure(Context context) {

    }
}

5.打包

6.上传jar到集群 lib目录下

7.修改配置

# 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.interceptors=i1
a1.sources.r1.interceptors.i1.type =com.flume.TypeInterceptor$Builder
a1.sources.r1.selector.type = multiplexing
a1.sources.r1.selector.header =type
al.sources.r1.selector.mapping.first=c1
a1.sources.r1.selector.mapping.second=c2

# Describe the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = hadoop103
a1.sinks.k1.port = 4141

a1.sinks.k2.type = avro
a1.sinks.k2.hostname = hadoop104
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

8.启动flume

/bin/flume-ng agent -n -c -f 

Ganglia
1.安装

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值