【Flume-工具篇】Flume的安装及使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenshi_2753/article/details/79702257

官网地址:http://flume.apache.org/

第一部分:Flume简介
这里写图片描述
Flume 是分布式的日志收集系统,可以处理各种类型各种格式的日志数据,包括avro、thrift、exec、jms、spooling directory、netcat、sequence generator、syslog、http、legacy、自定义等
分析:整个Agent(代理)组件,有source、channle、sink三部分组成。
由webser提供数据源,经过souce,存放数据源。经过channel管道再到sink最后输出到HDFS或者打印在控制台上等等。

第二部分:Flume搭建
搭建:

1、上传
2、解压
3、修改conf/flume-env.sh 文件中的JDK目录
注意:JAVA_OPTS 配置 如果我们传输文件过大 报内存溢出时 需要修改这个配置项
4、验证安装是否成功 ./flume-ng version
5、配置环境变量
export FLUME_HOME=/home/apache-flume-1.6.0-bin

第三部分:Flume案例及解析

Source、Channel、Sink有哪些类型
Flume Source

Source类型 说明
Avro Source 支持Avro协议(实际上是Avro RPC),内置支持
Thrift Source 支持Thrift协议,内置支持
Exec Source 基于Unix的command在标准输出上生产数据
JMS Source 从JMS系统(消息、主题)中读取数据
Spooling Directory Source 监控指定目录内数据变更
Twitter 1% firehose Source 通过API持续下载Twitter数据,试验性质
Netcat Source 监控某个端口,将流经端口的每一个文本行数据作为Event输入
Sequence Generator Source 序列生成器数据源,生产序列数据
Syslog Sources 读取syslog数据,产生Event,支持UDP和TCP两种协议
HTTP Source 基于HTTP POST或GET方式的数据源,支持JSON、BLOB表示形式
Legacy Sources 兼容老的Flume OG中Source(0.9.x版本)

Flume Channel

Channel类型 说明
Memory Channel Event数据存储在内存中
JDBC Channel Event数据存储在持久化存储中,当前Flume Channel内置支持Derby
File Channel Event数据存储在磁盘文件中
Spillable Memory Channel Event数据存储在内存中和磁盘上,当内存队列满了,会持久化到磁盘文件
Pseudo Transaction Channel 测试用途
Custom Channel 自定义Channel实现

Flume Sink
Sink类型 说明

HDFS Sink 数据写入HDFS
Logger Sink 数据写入日志文件
Avro Sink 数据被转换成Avro Event,然后发送到配置的RPC端口上
Thrift Sink 数据被转换成Thrift Event,然后发送到配置的RPC端口上
IRC Sink 数据在IRC上进行回放
File Roll Sink 存储数据到本地文件系统
Null Sink 丢弃到所有数据
HBase Sink 数据写入HBase数据库
Morphline Solr Sink 数据发送到Solr搜索服务器(集群)
ElasticSearch Sink 数据发送到Elastic Search搜索服务器(集群)
Kite Dataset Sink 写数据到Kite Dataset,试验性质的
Custom Sink 自定义Sink实现

案例1、 A simple example
案例1为单机模式,具体操作如下说明:
http://flume.apache.org/FlumeUserGuide.html#a-simple-example
配置文件

    ############################################################
    # Name the components on this agent
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1

    # Describe/configure the source
    a1.sources.r1.type = netcat
    //netcat是一个用于TCP/UDP连接和监听的linux工具, 主要用于网络传输及调试领域。netcat 可以打开TCP连接发送UDP报文,监听在TCP和UDP端口,以及TCP端口扫描,并将错误消息输出到屏幕上。
    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
    ############################################################

上述的例子放置的位置:可以家目录下新建文件,eg:新建option1 后人存入上述配置信息。
启动:

长选项:
$ bin/flume-ng agent –conf conf –conf-file example.conf –name a1 -Dflume.root.logger=INFO,console
说明:此为长选项,实际操作如下即可:
flume-ng agent -f option1 -n a1 -Dflume.root.logger=INFO,console

用telnet客户端测试是否通。安装telnet
yum install telnet
退出 ctrl+] quit
telnet 127.0.0.1 44444
然后写入数据,查看服务端是否接收到发送的信息。

Flume Sources 源对不同类型请求进行监听:

Avro Source:远程协议端口监听
Thrift Source:RPC通信监听
Exec Source:命令监听
JMS Source:消息队列监听
Spooling Directory Source:监听目录// 增减文件
备注:
Thrift是Google开发的用于跨语言RPC通信,它拥有功能强大的软件堆栈和代码生成引擎,允许定义一个简单的IDL文件来生成不同语言的代码,服务器端和客户端通过共享这个IDL文件来构建来完成通信。
Flume的Thrift Source是其实现的众多Source中的一个,Flume已经实现了服务器端,因此我们可以用任意自己熟悉的语言编写自己的Thrift Source客户端来采集数据,然后发送给Thrift Source服务器端。

案例2、两个flume做集群

这里写图片描述

node01服务器中,配置文件:

    ############################################################
    # 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 = node1 //前端的节点
    a1.sources.r1.port = 44444

    # Describe the sink
    # a1.sinks.k1.type = logger
    a1.sinks.k1.type = avro
    a1.sinks.k1.hostname = node2 // 后端的节点
    a1.sinks.k1.port = 60000

    # 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
    ############################################################

node02服务器中,安装Flume(步骤略)
后端配置文件:

    ############################################################
    # Name the components on this agent
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1

    # Describe/configure the source
    a1.sources.r1.type = avro
    a1.sources.r1.bind = node2
    a1.sources.r1.port = 60000

    # 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
    ############################################################

测试:

先启动node02的Flume
flume-ng agent -n a1 -c conf -f avro.conf -Dflume.root.logger=INFO,console
再启动node01的Flume
flume-ng agent -n a1 -c conf -f simple.conf2 -Dflume.root.logger=INFO,console
打开telnet 测试 node02控制台输出结果

对命令进行监听,案例如下:
案例3、Exec Source
http://flume.apache.org/FlumeUserGuide.html#exec-source
配置文件:

############################################################
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1

    # Describe/configure the source
    a1.sources.r1.type = exec // 定义对命令进行监听
    a1.sources.r1.command = tail -F /home/flume.exec.log // 命令的位置

    # 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// 达到1000条为上限
    a1.channels.c1.transactionCapacity = 100 // source放和sink去以100条为单位

    # Bind the source and sink to the channel // 关联
    a1.sources.r1.channels = c1
    a1.sinks.k1.channel = c1
    ############################################################

测试:

启动Flume
flume-ng agent -n a1 -c conf -f exec.conf -Dflume.root.logger=INFO,console
创建空文件演示 touch flume.exec.log
循环添加数据
for i in {1..50}; do echo “$i hi flume” >> flume.exec.log ; sleep 0.1; done

对文件夹监控
案例4、Spooling Directory Source
http://flume.apache.org/FlumeUserGuide.html#spooling-directory-source
配置文件:

  ############################################################
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1

    # Describe/configure the source
    a1.sources.r1.type = spooldir
    a1.sources.r1.spoolDir = /home/logs
    a1.sources.r1.fileHeader = true

    # 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
flume-ng agent -n a1 -c conf -f -Dflume.root.logger=INFO,console
拷贝文件演示
mkdir logs
cp flume.exec.log logs/

sink到HDFS的过程配置详解:
案例5、hdfs sink
http://flume.apache.org/FlumeUserGuide.html#hdfs-sink
配置文件:

############################################################
    a1.sources = r1
    a1.sinks = k1
    a1.channels = c1

    # Describe/configure the source
    a1.sources.r1.type = spooldir
    a1.sources.r1.spoolDir = /home/logs
    a1.sources.r1.fileHeader = true

    # Describe the sink
    ***只修改上一个spool sink的配置代码块 a1.sinks.k1.type = logger
    a1.sinks.k1.type=hdfs
    a1.sinks.k1.hdfs.path=hdfs://mycluster/flume/%Y-%m-%d/%H%M// 此处指定HDFS的逻辑入口

    ##每隔60s或者文件大小超过10M的时候产生新文件
    # hdfs有多少条消息时新建文件,0不基于消息个数
    a1.sinks.k1.hdfs.rollCount=0
    # hdfs创建多长时间新建文件,0不基于时间
    a1.sinks.k1.hdfs.rollInterval=60
    # hdfs多大时新建文件,0不基于文件大小
    a1.sinks.k1.hdfs.rollSize=10240
    # 当目前被打开的临时文件在该参数指定的时间(秒)内,没有任何数据写入,则将该临时文件关闭并重命名成目标文件
    a1.sinks.k1.hdfs.idleTimeout=3

    a1.sinks.k1.hdfs.fileType=DataStream
    a1.sinks.k1.hdfs.useLocalTimeStamp=true

    ## 每五分钟生成一个目录:
    # 是否启用时间上的”舍弃”,这里的”舍弃”,类似于”四舍五入”,后面再介绍。如果启用,则会影响除了%t的其他所有时间表达式
    a1.sinks.k1.hdfs.round=true
    # 时间上进行“舍弃”的值;
    a1.sinks.k1.hdfs.roundValue=5
    # 时间上进行”舍弃”的单位,包含:second,minute,hour
    a1.sinks.k1.hdfs.roundUnit=minute

    # 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
############################################################

创建HDFS目录
hadoop fs -mkdir /flume (可自动新建)

启动Flume
flume-ng agent -n a1 -c conf -f hdfs.conf -Dflume.root.logger=INFO,console
段选项:flume-ng agent -f option5 -n a1 -Dflume.root.logger=INFO,console
然后只要往home/logs里放文件,服务端就自动执行写入HDFS的操作。可在hdfs的UI页面查看,会发现,先生成临时文件,最后会生成正式文件。如果超过一定时间,再次放入文件,则从新生成文件夹放入写入的文件。亦可通过查看hdfs文件命令:
hdfs dfs -ls /flume
hdfs dfs -cat /flume/2018-03-26/1835/FlumeData.1522060770488
或:
hadoop fs -ls /flume/…
hadoop fs -get /flume/…

猜你喜欢

转载自blog.csdn.net/chenshi_2753/article/details/79702257