Flume笔记
1.Flume 概述
1.Flume 定义
Flume 是 Cloudera 提供的一个高可用的,高可靠的,分布式的**海量日志采集、聚合和传输的系统。**Flume 基于流式架构,灵活简单。
2.Flume 基础架构
-
Agent
Agent 是一个 JVM 进程,它以事件的形式将数据从源头送至目的。
Agent 主要有 3 个部分组成,Source、Channel、Sink。
-
Source
Source 是负责接收数据到 Flume Agent 的组件。Source 组件可以处理各种类型、各种格式的日志数据,包括 avro、thrift、exec、jms、spooling directory、netcat、sequence generator、syslog、http、legacy。
-
Sink
Sink 不断地轮询 Channel 中的事件且批量地移除它们,并将这些事件批量写入到存储或索引系统、或者被发送到另一个 Flume Agent。
Sink 组件目的地包括 hdfs、logger、avro、thrift、ipc、file、HBase、solr、自定义。
-
Channel
Channel 是位于 Source 和 Sink 之间的缓冲区。因此,Channel 允许 Source 和 Sink 运作在不同的速率上。Channel 是线程安全的,可以同时处理几个 Source 的写入操作和几个Sink 的读取操作。
Flume 自带两种 Channel:Memory Channel 和 File Channel 以及 Kafka Channel。
-
Event
传输单元,Flume 数据传输的基本单元,以 Event 的形式将数据从源头送至目的地。Event 由 Header 和 Body 两部分组成,Header 用来存放该 event 的一些属性,为 K-V 结构,Body 用来存放该条数据,形式为字节数组。
2.Flume 快速入门
1.Flume 安装部署
-
安装地址
-
安装部署
-
解压
-
修改 apache-flume-1.7.0-bin 的名称为 flume
-
将 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 入门案例
-
监控端口数据官方案例
-
案例需求:使用 Flume 监听一个端口,收集该端口数据,并打印到控制台。
-
需求分析:
-
实现步骤:
-
安装 netcat 工具
[root@master conf]# sudo yum install -y nc
-
判断 44444 端口是否被占用
[root@master conf]# sudo netstat -tunlp | grep 44444
-
创建 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
配置文件解析
-
先开启 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。
-
使用 netcat 工具向本机的 44444 端口发送内容
[root@master flume]# nc localhost 44444 hello OK word OK
-
在 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 }
-
-
-
flume写入到hdfs
-
案例需求:使用 Flume 监听一个端口,收集该端口数据,并上传到 HDFS 中
-
创建 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
-
查看 HDFS 上的数据
-
-
flume写入到kafka
-
案例需求:使用 Flume 监听一个端口,收集该端口数据,并上传到 kafka中
-
创建 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
-
查看 kafka 上的数据
[root@master kafka]# bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic mytopic hello word hello kafka
-