flume安装和简单使用

Flume****日志收集

(1)Apache Flume简介
--Flume用于将多种来源的日志以流的方式传输至Hadoop或者其它目的地
	一种可靠、可用的高效分布式数据收集服务
--Flume拥有基于数据流上的简单灵活架构,支持容错、故障转移与恢复
--由Cloudera 2009年捐赠给Apache,现为Apache顶级项目
(2)Flume架构
--Client:客户端,数据产生的地方,如Web服务器
--Event:事件,指通过Agent传输的单个数据包,如日志数据通常对应一行数据
--Agent:代理,一个独立的JVM进程
	Flume以一个或多个Agent部署运行
	Agent包含三个组件
		Source
		Channel
		Sink

(3)Flume安装
--解压flume安装包
tar -zxf 安装包
mv 解压文件 /opt/soft/flume160

--复制配置文件,并且修改,conf目录中
mv flume-env.sh.template flume-env.sh
修改其中的jdk路径
vi flume-env.sh
export JAVA_HOME=/opt/soft/jdk180

--修改环境变量并且激活
vi /etc/profile
export FLUME_HOME=/opt/soft/flume160
export PATH=$PATH:$FLUME_HOME/bin
source /etc/profile

--验证flume是否安装成功
flume-ng version
(4)Flume简单样例一
--编辑配置文件
/opt/flumeconf/conf_0804_simple.properties

文件内容如下:
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = 192.168.190.151
a1.sources.r1.port = 6666

# 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 #channels的容量
a1.channels.c1.transactionCapacity = 100 #channels事务交易的限制

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

--安装nc
yum install nmap-ncat.x86_64

--启动flume,注意:-n后面跟着agent的名字,-c后面跟着是读取配置文件(flume-env.sh),-f后面是演示样例的路径,最后的是在控制台输出INFO以上级别的日志信息
flume-ng agent -n a1 -c conf -f /opt/flumeconf/conf_0804_simple.properties
-Dflume.root.logger=INFO,console

--启动nc,注意端口号和配置文件中一致:(a1.sources.r1.port = 6666)
nc 192.168.190.151 6666
(5)Source(exec source)简单样例二,加粗的属性是必写项
描述:执行Linux指令,并消费指令返回的结果,如“tail -f”
属性缺省值描述
type-exec
command-如"tail -f xxx.log"
shell-选择系统shell程序,如"/bin/sh"
batchSize20发送给channel的最大行数
#参考配置文件
a1.sources = s1
a1.channels = c1
a1.sinks = sk1

# Describe/configure the source
#设置source类型为exec
a1.sources.s1.type = exec
#设置命令,查看文件内容
a1.sources.s1.command = tail -f /root/data/exectest.txt

#source和channel连接
a1.sources.s1.channels = c1

# 指定sink
a1.sinks.sk1.type = logger
#sink和channel 连接
a1.sinks.sk1.channel = c1

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100 
文件内容:
hello world
hello world
zhangsan
lisi

运行命令:
[root@zhangqi flumeconf]# flume-ng agent -n a1 -c conf -f exec.conf

执行结果:
20/08/17 18:43:39 INFO sink.LoggerSink: Event: { headers:{} body: 68 65 6C 6C 6F 20 77 6F 72 6C 64                hello world }
20/08/17 18:43:39 INFO sink.LoggerSink: Event: { headers:{} body: 68 65 6C 6C 6F 20 77 6F 72 6C 64                hello world }
20/08/17 18:43:39 INFO sink.LoggerSink: Event: { headers:{} body: 7A 68 61 6E 67 73 61 6E                         zhangsan }
20/08/17 18:43:39 INFO sink.LoggerSink: Event: { headers:{} body: 6C 69 73 69                                     lisi }
(6)Source(spooling directory source)简单样例三,加粗的属性是必写项
描述:从磁盘文件夹中获取文件数据,可避免重启或者发送失败后数据丢失,还可用于监控文件夹新文件
属性缺省值描述
type-spooldir
spoolDir-需读取的文件夹
fileSuffix.COMPLETED文件读取完成后添加的后缀
deletePolicynever文件完成后删除策略:never和immediate
#参考配置文件
a1.sources = s1
a1.channels = c1
a1.sinks = sk1

# Describe/configure the source
#设置source类型为spooldir
a1.sources.s1.type = spooldir
a1.sources.s1.spoolDir = /root/data/spooldirTest

#source和channel连接
a1.sources.s1.channels = c1

# 指定sink
a1.sinks.sk1.type = logger
#sink和channel 连接
a1.sinks.sk1.channel = c1

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
说明:
spooldirTest文件夹下的文件会被监控,读取完成后默认加上.COMPLETED的后缀

执行命令:
flume-ng agent -n a1 -c conf -f spooldir.conf

执行前:
[root@zhangqi spooldirTest]# ls
name.txt  test.txt

执行后:
[root@zhangqi spooldirTest]# ls
name.txt.COMPLETED  test.txt.COMPLETED
(7)Source(http source)简单样例四,加粗的属性是必写项
用于接收HTTP的Get和Post请求
属性缺省值描述
type-http
port-监听端口
bind0.0.0.0绑定IP
handlerorg.apache.flume.source.http.JSONHandler数据处理程序类全名
#参考配置
a1.sources = s1
a1.channels = c1
a1.sinks = sk1

# Describe/configure the source
#设置source为http
a1.sources.s1.type = http
a1.sources.s1.port = 5140

#source和channel连接
a1.sources.s1.channels = c1

# 指定sink
a1.sinks.sk1.type = logger
#sink和channel 连接
a1.sinks.sk1.channel = c1

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
执行命令:
(1)
[root@zhangqi flumeconf]# flume-ng agent -n a1 -c conf -f http.conf
(2)
curl -XPOST localhost:5140 -d'[{"headers":{"h1":"v1","h2":"v2"},"body":"hello body"}]'

结果:
20/08/17 19:03:46 INFO sink.LoggerSink: Event: { headers:{h1=v1, h2=v2} body: 68 65 6C 6C 6F 20 62 6F 64 79                   hello body }
(8-1)Source(avro source)简单样例五,加粗的属性是必写项
描述:监听Avro端口,并从外部Avro客户端接收events
属性缺省值描述
type-avro
bind-绑定IP地址
port-端口
threads-最大工作线程数量
参考配置文件1:(avro_source.conf)
a2.sources = s1
a2.channels = c1
a2.sinks = sk1

# Describe/configure the source
#设置source类型为avro
a2.sources.s1.type = avro
a2.sources.s1.bind = 0.0.0.0
a2.sources.s1.port = 44444

#source和channel连接
a2.sources.s1.channels = c1

# 指定sink
a2.sinks.sk1.type =logger
#sink和channel 连接
a2.sinks.sk1.channel = c1

# Use a channel which buffers events in memory
a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000

(8-2)Sink(avro sink)加粗的属性是必写项
描述:作为avro客户端向avro服务端发送avro事件
属性缺省值描述
type-avro
hostname-服务端IP地址
post-端口
batch-size100批量发送事件数量
参考配置文件2:(avro_sink.conf)
a1.sources = s1
a1.channels = c1
a1.sinks = sk1

# Describe/configure the source
#设置source类型为exec
a1.sources.s1.type = exec
#设置命令,查看文件内容
a1.sources.s1.command = tail -f /root/data/customer.csv

#source和channel连接
a1.sources.s1.channels = c1

# 指定sink
a1.sinks.sk1.type = avro
a1.sinks.sk1.hostname = localhost
a1.sinks.sk1.port = 44444
#sink和channel 连接
a1.sinks.sk1.channel = c1

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
功能:查看customer.csv文件的内容,然后传送给avro端口,再监听该端口,获取文件信息
执行命令:
(1)
flume-ng agent -n a2 -c conf -f avro_source.conf
(2)
flume-ng agent -n a1 -c conf -f avro_sink.conf
结果:
source端,会读取出avro监听到的文件信息
(9)Source(taildir source)简单样例七,加粗的属性是必写项
断点续传:监控指定的一些文件,并在检测到新的一行数据产生的时候几乎实时地读取它们,如果新的一行数据还没写完,Taildir Source会等到这行写完后再读取。

属性默认值描述
type-组件类型,这个是: TAILDIR.
filegroups-被监控的文件夹目录集合,这些文件夹下的文件都会被监控,多个用空格分隔
filegroups.-被监控文件夹的绝对路径。正则表达式(注意不会匹配文件系统的目录)只是用来匹配文件名
positionFile~/.flume/taildir_position.json用来设定一个记录每个文件的绝对路径和最近一次读取位置inode的文件,这个文件是JSON格式。
headers..-给某个文件组下的Event添加一个固定的键值对到header中,值就是value。一个文件组可以配置多个键值对。
参考配置:
a1.sources = s1
a1.channels = c1
a1.sinks = sk1

# Describe/configure the source
#设置source类型为taildir,断点续传,1.8之后有
a1.sources.s1.type = TAILDIR
a1.sources.s1.filegroups = f1 f2

#配置filegorups的f1和f2
a1.sources.s1.filegroups.f1 = /root/data/tail_1/example.log
a1.sources.s1.filegroups.f2 = /root/data/tail_2/.*log.*

#指定position的位置
a1.sources.s1.positionFile = /root/data/tail_position/taildir_position.json

#指定headers
a1.sources.s1.headers.f1.headerKey1 = value1
a1.sources.s1.headers.f2.headerKey1 = value2
a1.sources.s1.headers.f2.headerKey2 = value3

a1.sources.s1.fileHeader = true

#source和channel连接
a1.sources.s1.channels = c1

# 指定sink
a1.sinks.sk1.type = logger
#sink和channel 连接
a1.sinks.sk1.channel = c1

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100 
执行命令:
flume-ng agent -n a1 -c conf -f taildir.conf
(10)Sink(hdfs sink)简单样例八,加粗的属性是必写项
将事件写入Hadoop分布式文件系统(HDFS)
属性缺省值描述
type-hdfs
hdfs.path-hdfs目录
hdfs.filPrefixFlumeData文件前缀
hdfs.fileSuffix-文件后缀
参考配置:
a1.sources = s1
a1.channels = c1
a1.sinks = sk1

# Describe/configure the source
#设置source类型为exec
a1.sources.s1.type = exec
#设置命令,查看文件内容
a1.sources.s1.command = tail -f /root/data/customer.csv

#source和channel连接
a1.sources.s1.channels = c1

# 指定sink
a1.sinks.sk1.type = hdfs
a1.sinks.sk1.hdfs.path = hdfs://192.168.190.151:9000/flume/event/%y-%m-%d/%H%M/%S
#hdfs.useLocalTimeStamp:使用日期时间转义符时是否使用本地时间戳(而不是使用 Event header 中自带的时间戳)
a1.sinks.sk1.hdfs.useLocalTimeStamp=true
#sink和channel 连接
a1.sinks.sk1.channel = c1

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100 
执行命令:
flume-ng agent -n a1 -c conf -f hdfs.conf
结果:
在hdfs上的/flume/event/文件夹下产生了层级目录和文件
(11)Channel分类
--Memory Channel
	event保存在Java Heap中。如果允许数据小量丢失,推荐使用
--File Channel
	event保存在本地文件中,可靠性高,但吞吐量低于Memory Channel
--Kafka Channel
--JDBC Channel
	event保存在关系数据中,一般不推荐使用
	
	flume-ng agent -n a1 -c conf -f avro_sink.conf

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值