2、根据数据采集的需求 配置采集方案,描述在配置文件中(文件名可任意自定义)
3、 指定采集方案配置文件,在相应的节点上启动flume agent
4、Flume没有高可用,失败后只能重启---只有自己写个脚本监听,失败后自动重启
实践一:NetCat方式
# 定义这个agent中各组件的名字
#类型, 从网络端口接收数据,在本机启动, 所以localhost, type=spoolDir采集目录源,目录里有就采 netcat在数据太长会截断
//类型:从网络端口获取数据 如linux的socket程序nc命令 其他机器可以通过 new Socket(127.0.0.1,44444)发送数据 把接收到的一行数据注册成一个event 如果不是netcat而是spoolDir,则是目录下的所有数据
agent.sources.r1.type = netcat
agent.sources.r1.bind = 127.0.0.1
# 描述和配置sink组件:k1 把数据下沉到logger服务器,然后logger打印到屏幕上
#下沉的时候是一批一批的, 下沉的时候是一个个eventChannel参数解释:
#capacity:默认该通道中最大的可以存储的event数量
#trasactionCapacity:每次最大可以从source中拿到或者送到sink中的event数量
# 描述和配置source channel sink之间的连接关系 --将source、channel、sink链接起来

#告诉flum启动一个agent,指定配置文件的路径, --name:agent的名字,--name 后面的a1必须和配置文件的名字一模一样
$ bin/flume-ng agent --conf conf --conf-file conf/netcat-logger.conf --name a1 -Dflume.root.logger=INFO,console
例子:
bin/flume-ng agent -c conf -f conf/netcat-logger.conf -n a1 -Dflume.root.logger=INFO,console
-c conf 指定flume自身的配置文件所在目录
-f conf/netcat-logger.con 指定我们所描述的采集方案
-n a1 指定我们这个agent的名字
随便在一个能跟agent节点联网的机器上
telnet anget-hostname port
监视文件夹
启动命令:bin/flume-ng agent -c ./conf -f ./conf/spool-logger.conf -n a1 -Dflume.root.logger=INFO,console
测试: 往/home/hadoop/flumeSpool放文件(mv ././xxxFile /home/hadoop/flumeSpool),但是不要在里面生成文件
#######配置文件spooldir-hdfs#######
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
#监听目录,spoolDir指定目录, fileHeader要不要给文件夹前坠名
a1.sources.r1.type = spooldir//spooldir也会有自己的参数
a1.sources.r1.spoolDir = /home/hadoop/flumespool //要创建这个路径 不能不存在
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
此时只要有文件在此路径发生创建、移动、改变均能被采集到日志
实践二:Exec方式


实践三:HDFS
• ./bin/flume-ng avro-client --conf conf -H master -p 41414 -F
/home/badou/flume_test/monitor_source/3.txt -Dflume.root.logger=DEBUG,console
实践四:模拟使用Flume监听日志变化,并且把增量的日志文件写入到hdfs中
• ]# vim flume.conf
• 运行flume-ng
• ./bin/flume-ng agent --conf conf -- conf-file ./conf/flume.conf -name a1 - Dflume.root.logger=DEBUG,console
• 验证:
• echo "11113" >> 1.log
用tail命令获取数据,下沉到hdfs
vim tail-hdfs.conf
配置参数详情查看官网mkdir /home/hadoop/log
while true
do
echo 111111 >> /home/hadoop/log/test.log
sleep 0.5
done
tail -F test.log
采集到hdfs中, 文件中的目录不用自己建的
启动hdfs:start-dfs.sh
检查下hdfs式否是salf模式:(刚刚启动时很容易还在safe模式)hdfs dfsadmin -report
bin/flume-ng agent -c conf -f conf/tail-hdfs.conf -n a1
前端页面查看下, master:50070, 文件目录: /flum/events/16-04-20/
启动命令:
bin/flume-ng agent -c conf -f conf/tail-hdfs.conf -n a1
################################################################
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#exec 指的是命令
# Describe/configure the source
a1.sources.r1.type = exec
#F根据文件名追中, f根据文件的nodeid追中--采集的文件
a1.sources.r1.command = tail -F /home/hadoop/log/test.log
a1.sources.r1.channels = c1
# Describe the sink
#下沉目标
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
#指定目录, flum帮做目的替换
a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M/ 路径为年月日时分秒
#文件的命名, 前缀 后缀为时间戳
a1.sinks.k1.hdfs.filePrefix = events-
#10 分钟就改目录
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute
#文件滚动之前的等待时间(秒)
a1.sinks.k1.hdfs.rollInterval = 3
#文件滚动的大小限制(bytes)
a1.sinks.k1.hdfs.rollSize = 500
#写入多少个event数据后滚动文件(事件个数)
a1.sinks.k1.hdfs.rollCount = 20
#5个事件就往里面写入
a1.sinks.k1.hdfs.batchSize = 5
#用本地时间格式化目录 a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M/
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#下沉后, 生成的文件类型,默认是Sequencefile,可用DataStream,则为普通文本
a1.sinks.k1.hdfs.fileType = DataStream
# 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
从avro端口接收数据,下沉到logger
bin/flume-ng agent -c conf -f conf/avro-logger.conf -n al -Dflume.root.logger=INFO,console#########
采集配置文件,avro-hdfs.conf
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
#source中的avro组件是接收者服务, 绑定本机
a1.sources.r1.type = avro
a1.sources.r1.channels = c1
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port = 4141
# 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
发送数据:
$ bin/flume-ng avro-client -H localhost -p 4141 -F /usr/logs/log.10
flume串联---多个flume的联合使用
从tail命令获取数据发送到avro端口(tail到avro,avro到logger)--要和上一个配置文件avro-hdfs.conf中的IP地址和端口号对应,本agent会发送event到上面avro-hdfs.conf这个的agent上,然后再由avro-hdfs.conf的agent写到logger中
另一个节点可配置一个avro源来中继数据,发送外部存储
################### Name the components on this agent
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/hadoop/log/test.log
a1.sources.r1.channels = c1
# Describe the sink
#绑定的不是本机, 是另外一台机器的服务地址, sink端的avro是一个发送端, avro的客户端, 往master这个机器上发
a1.sinks = k1
a1.sinks.k1.type = avro ---这次不是hdfs 要指定一个服务器主机和端口号
a1.sinks.k1.channel = c1
a1.sinks.k1.hostname = master
a1.sinks.k1.port = 4141
a1.sinks.k1.batch-size = 2 //一批次两个event
# 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