Flume笔记

Flume笔记

1.Flume 概述

1.Flume 定义

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

在这里插入图片描述

2.Flume 基础架构

在这里插入图片描述

  1. Agent

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

    Agent 主要有 3 个部分组成,SourceChannelSink

  2. Source

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

  3. Sink

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

    Sink 组件目的地包括 hdfsloggeravro、thrift、ipc、fileHBase、solr、自定义。

  4. Channel

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

    Flume 自带两种 Channel:Memory ChannelFile Channel 以及 Kafka Channel

  5. Event

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

    在这里插入图片描述

2.Flume 快速入门

1.Flume 安装部署

  1. 安装地址

    1. Flume 官网地址:Welcome to Apache Flume — Apache Flume
    2. 文档查看地址:Flume 1.9.0 User Guide — Apache Flume
    3. 下载地址:Index of /dist/flume (apache.org)
  2. 安装部署

    1. 解压

    2. 修改 apache-flume-1.7.0-bin 的名称为 flume

    3. 将 flume/conf 下的 flume-env.sh.template 文件修改为 flume-env.sh,并配置 flume-env.sh 文件

      [root@master conf]# mv flume-env.sh.template flume-env.sh
      [root@master conf]# vi flume-env.sh
      export JAVA_HOME=/opt/module/jdk1.8.0_162
      

2.Flume 入门案例

  1. 监控端口数据官方案例

    1. 案例需求:使用 Flume 监听一个端口,收集该端口数据,并打印到控制台。

    2. 需求分析:

      在这里插入图片描述

    3. 实现步骤:

      1. 安装 netcat 工具

        [root@master conf]# sudo yum install -y nc
        
      2. 判断 44444 端口是否被占用

        [root@master conf]# sudo netstat -tunlp | grep 44444
        
      3. 创建 Flume Agent 配置文件 netcat-flume-logger.conf

        # 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
        

        配置文件解析

        在这里插入图片描述

      4. 先开启 flume 监听端口

        [root@master flume]# ./bin/flume-ng agent -n a1 -c ./conf/ -f ./job/netcat-flume-logger.conf -Dflume.root.logger=INFO,console
        

        参数说明:

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

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

        **–conf-file/-f:**flume 本次启动读取的配置文件是在 job 文件夹下的 flume-telnet.conf文件。

        -Dflume.root.logger=INFO,console :-D 表示 flume 运行时动态修改 flume.root.logger参数属性值,并将控制台日志打印级别设置为 INFO 级别。日志级别包括:log、info、warn、error。

      5. 使用 netcat 工具向本机的 44444 端口发送内容

        [root@master flume]# nc localhost 44444
        hello
        OK
        word
        OK
        
      6. 在 Flume 监听页面观察接收数据情况

        2022-05-21 14:37:48,646 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:95)] Event: { headers:{} body: 68 65 6C 6C 6F                                  hello }
        2022-05-21 14:37:54,040 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:95)] Event: { headers:{} body: 77 6F 72 64                                     word }
        
  2. flume写入到hdfs

    1. 案例需求:使用 Flume 监听一个端口,收集该端口数据,并上传到 HDFS 中

    2. 创建 Flume Agent 配置文件 netcat-flume-logger.conf

      官网案例地址:Flume 1.9.0 User Guide — Apache Flume

      # 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 = hdfs
      a1.sinks.k1.channel = c1
      a1.sinks.k1.hdfs.path = hdfs://master:9000/flume/events/%y-%m-%d/%H%M/%S
      a1.sinks.k1.hdfs.filePrefix = logs-
      a1.sinks.k1.hdfs.round = true
      a1.sinks.k1.hdfs.roundValue = 10
      a1.sinks.k1.hdfs.roundUnit = minute
      a1.sinks.k1.hdfs.useLocalTimeStamp = true
      
      # 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
      
    3. 查看 HDFS 上的数据

      在这里插入图片描述

  3. flume写入到kafka

    1. 案例需求:使用 Flume 监听一个端口,收集该端口数据,并上传到 kafka中

    2. 创建 Flume Agent 配置文件 netcat-flume-kafka.conf

      官网案例地址:Flume 1.9.0 User Guide — Apache Flume

      # 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.channel = c1
      a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
      a1.sinks.k1.kafka.topic = mytopic
      a1.sinks.k1.kafka.bootstrap.servers = localhost:9092
      a1.sinks.k1.kafka.flumeBatchSize = 20
      a1.sinks.k1.kafka.producer.acks = 1
      a1.sinks.k1.kafka.producer.linger.ms = 1
      a1.sinks.k1.kafka.producer.compression.type = snappy
      
      # 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
      
    3. 查看 kafka 上的数据

      [root@master kafka]# bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic mytopic
      hello
      word
      hello
      kafka
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值