flume大全,,,配置和相关实例

flume

agent 结构

flume运行的核心是agent。flume以agent为最小的独立运行单位,
flume有三个核心组件
- source
是数据的收集端,负责将数据捕获后进行特殊的格式化,将数据封装到事件(event)里,然后将事件推入Channel中
- source类型
- netcat source
监控某个端口将流经端口的每一个文本行数据作为event输入
- syslog sources
读取syslog数据,产生event,支持udp和tcp两种协议
- http source
基于http post或get方法的数据源,支持json、blob表示形式
- Channel
是连接source和sink的组件,大家可以将它看做一个数据的缓冲区,它可以将数据暂存到内存中也可以持久化到本地磁盘,知道sink处理完改事件
两个常用的Channel
- MemoryChannel
event数据存储在内存中
- FileChannel
event数据存储在磁盘文件中
- sink
sink从channel中取出事件,然后将数据发到别处,可以向文件系统、数据库、hadoop存数据也可以是其他agent的source,在日志文件较少时,可以将数据存储在文件系统中,并且设定一定的时间间隔保存数据
- sink类型
- hdfs sink
数据写入hdfs
- avro sink
数据将转换成avro event,然后发送到配置的rpc端口上
- logger sink
数据写入日志文件
- file roll sink
存储数据到本地文件系统
- hase sink
数据写入hbase数据库

使用flume监控某个端口,把端口写入的数据输出为logger

$ cp -a flume-conf.properties.template flume-telnet.conf
修改flume-telnet.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

测试:
# yum -y install telnet
$ bin/flume-ng agent --conf conf/ --name a1 --conf-file conf/flume-telnet.conf -Dflume.root.logger=INFO,console

$ netstat -an|grep 44444   --检查本机是否监听了44444端口
$ telnet localhost 44444   --telnet用于连接本机的44444端口
                --telnet是客户端去访问这个端口
flume的sources用来监听44444端口客户端发送的消息,
接收到消息后sources将字符串提交到chanel中,sink从chanel中将字符串
抽取输出到控制台

输入字符串..

使用flume去监控某个文件,将新添加进文件的内容抽取到其他地方[hdfs]

  • a1.sources.r1.type = exec
    表示可以使用linux命令行命令
  • a1.sources.r1.command = tail -F /文件
    对文件进行监控
  • a1.sinks.k1.hdfs.rollInterval = 10
    表示hdfs sink 间隔多长时间将临时文件滚动成最终文件
    如果设置为0,则表示不根据时间来滚动文件
    默认值为30
  • a1.sinks.k1.hdfs.rollSize = 20
    当临时文件达到大小20bytes时,滚动成自动文件
    如果设置为0,则表示不根据临时文件大小来滚动文件
    默认值是1024
  • a1.sinks.k1.hdfs.rollCount = 5
    当events数据达到该数量时,将临时文件滚动成目标文件
    如果设置为0,则表示不根据event数量来表示
    默认值是10

  • round
    表示是否启用时间上的舍弃

    • a1.sinks.k1.hdfs.round = true
    • a1.sinks.k1.hdfs.roundValue = 10
    • a1.sinks.k1.hdfs.rountUnit = minute
      上面三条配置信息表示
      当时间为2015-10-16 17:38:59 时候,hdfs.path 依然会被解析成
      /flume/events/20151016/17:30/00
      因为设置的时舍弃10分钟内的时间,因此,该目录没10分钟新生成一个
配置文件
修改flume-apache.conf             


a2.sources = r2
a2.channels = c2
a2.sinks = k2

# define sources
a2.sources.r2.type = exec
## 注意一定要执行flume命令的用户对该/var/log/httpd/access_log文件
## 具有可读的权限
a2.sources.r2.command = tail -F /opt/q.txt
a2.sources.r2.shell = /bin/bash -c

# define channels
a2.channels.c2.type = memory
a2.channels.c2.capacity = 1000
a2.channels.c2.transactionCapacity = 100

# define sinks
#启用设置多级目录,这里按年/月/日/时 2级目录,每个小时生成一个文件夹
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path=hdfs://node-1:8020/flume/%Y%m%d/%H
a2.sinks.k2.hdfs.filePrefix = accesslog
#启用按时间生成文件夹
a2.sinks.k2.hdfs.round=true
#设置round单位:小时  
a2.sinks.k2.hdfs.roundValue=1
a2.sinks.k2.hdfs.roundUnit=minute
#使用本地时间戳  
a2.sinks.k2.hdfs.useLocalTimeStamp=true

a2.sinks.k2.hdfs.batchSize=1000
a2.sinks.k2.hdfs.fileType=DataStream
a2.sinks.k2.hdfs.writeFormat=Text
a2.sources.r2.channels = c2
a2.sinks.k2.channel = c2
  • 向文件添加数据命令
    while true ; do echo 'access access' >>/opt/q.txt;sleep 0.5;done

使用flume监控某个目录

a3.sources = r3
a3.channels = c3
a3.sinks = k3

a3.sources.r3.type = spooldir

a3.sources.r3.spoolDir = /opt/a

a3.sources.r3.fileHeader = true



a3.channels.c3.type = memory
a3.channels.c3.capacity = 1000
a3.channels.c3.transactionCapacity = 100


a3.sinks.k3.type = hdfs

a3.sinks.k3.hdfs.path = hdfs://node-1:8020/flume1/%Y%m%d
a3.sinks.k3.hdfs.fileType = DataStream
a3.sinks.k3.hdfs.writeFormat = Text
a3.sinks.k3.hdfs.batchSize = 10
a3.sinks.k3.hdfs.useLocalTimeStamp = true
a3.sources.r3.channels = c3
a3.sinks.k3.channel = c3

命令:
bin/flume-ng agent --conf conf/ --name a3 --conf-file conf/flume-dir.conf -Dflume.root.logger=INFO,console

使用flume解决过多的小文件

利用flume监控某个目录[/var/log/httpd],把里面回滚好的文件
实时抽取到HDFS平台。
这里的日志回滚是apache这种应用服务器自己完成的
$ ls
access_log access_log.1 access_log.2

需求:抽取文件access_log.1和access_log.2

$ cp -a flume-apache.conf flume-dir.conf

a3.sources = r3
a3.channels = c3
a3.sinks = k3

# define sources
a3.sources.r3.type = spooldir
a3.sources.r3.spoolDir = /home/gao/logs
a3.sources.r3.ignorePattern = ^.*\_log$

# define channels      断点续传功能   ,记录抽取暂停时的位置
a3.channels.c3.type = file
a3.channels.c3.checkpointDir = /opt/modules/cdh/apache-flume-1.5.0-cdh5.3.6-bin/checkpoint
a3.channels.c3.dataDirs = /opt/modules/cdh/apache-flume-1.5.0-cdh5.3.6-bin/checkdata


# define sinks
#启用设置多级目录,这里按年/月/日/时 2级目录,每个小时生成一个文件夹
a3.sinks.k3.type = hdfs
a3.sinks.k3.hdfs.path=hdfs://node-1:8020/flume2/%Y%m%d/%H
a3.sinks.k3.hdfs.filePrefix = accesslog
#启用按时间生成文件夹
a3.sinks.k3.hdfs.round=true
#设置round单位:小时  
a3.sinks.k3.hdfs.roundValue=1
a3.sinks.k3.hdfs.roundUnit=hour
#使用本地时间戳  
a3.sinks.k3.hdfs.useLocalTimeStamp=true

a3.sinks.k3.hdfs.batchSize=1000
a3.sinks.k3.hdfs.fileType=DataStream
a3.sinks.k3.hdfs.writeFormat=Text

#设置解决文件过多过小问题
#每600秒生成一个文件
a3.sinks.k3.hdfs.rollInterval=600
#当达到128000000bytes时,创建新文件 127*1024*1024
#实际环境中如果按照128M回顾文件,那么这里设置一般设置成127M
a3.sinks.k3.hdfs.rollSize=128000000
#设置文件的生成不和events数相关
a3.sinks.k3.hdfs.rollCount=0
#设置成1,否则当有副本复制时就重新生成文件,上面三条则没有效果
a3.sinks.k3.hdfs.minBlockReplicas=1

# bind the sources and sinks to the channels
a3.sources.r3.channels = c3
a3.sinks.k3.channel = c3

执行以下命令
bin/flume-ng agent –conf conf/ –name a3 –conf-file conf/flume-dir.conf -Dflume.root.logger=INFO,console

测试:

浏览器访问网页
http://bigdata-hive/index.html
tail -f /var/log/httpd/access_log

同一个服务器启动三个agent

agent1:用于实时监控 /var/log/httpd/access_log

flume 的load-balance

负载均衡是用于解决一台机器无法解决所有请求而产生的一种算法
agent1是一个路由节点,负责将channel暂存的event均衡到对应的多个sink组件上,而每个sink组件分别连接到一个独立的agent上,
agent1的sink type 是下一级的source type

flume 的failover

failover sink processor 维护一个优先级sink组件列表,只要有一个sink组件可用。event就会被传递到下一个组件。故障转移机制的作用是将失败的sink降级到一个池。sink具有与之相关的优先级,数量越大,优先级越高

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值