环境:kafka_2.10-0.10.2.1.tgz,Hadoop-2.7.3集群,zookeeper-3.4.10,kafka_2.10-0.10.2.1
安装Flume之前先安装kakaf集群。点此查看kafka安装
一、Flume安装
我这里一共有一个Master,三个Slave。我只将Flume安装在其中的一个Slave(主机名Server3)上
1.解压到指定目录
tar -zxvf apache-flume-1.8.0-bin.tar.gz -C /usr/local
2.配置环境变量
export FLUME_HOME=/usr/local/apache-flume-1.8.0-bin
export PATH=${FLUME_HOME}/bin:$PATH
source /etc/profile
3.配置flume
进入conf目录中。
cp flume-env.sh.template flume-env.sh
然后修改flume-env.sh中的java_home。
export JAVA_HOME=/usr/local/jdk1.8.0_111
4.验证安装
执行命令:flume-ng version
二、Flume使用
a.非实时收集方式
1.单节点的agent
该配置方式不能用于实时收集日志,只能往监控目录中扔文件
在conf目录下增加配置文件single_agent.conf
vim single_agent.conf
将以下内容粘贴进去:
# agent的名称为a1,多个源用逗号或者空格分隔
a1.sources = source1
a1.channels = channel1
a1.sinks = sink1
# set source
a1.sources.source1.type = spooldir
a1.sources.source1.spoolDir=/usr/local/apache-flume-1.8.0-bin/data/spoolDirLog
a1sources.source1.fileHeader = flase
# set sink
a1.sinks.sink1.type = org.apache.flume.sink.kafka.KafkaSink
#a1.sinks.sink1.kafka.bootstrap.servers = Desktop:9092,Server1:9092,Server2:9092,Server3:9092
a1.sinks.sink1.brokerList= Desktop:9092,Server1:9092,Server2:9092,Server3:9092
a1.sinks.sink1.topic= spoolDirLog
a1.sinks.sink1.kafka.flumeBatchSize = 20
a1.sinks.sink1.kafka.producer.acks = 1
a1.sinks.sink1.kafka.producer.linger.ms = 1
a1.sinks.sink1.kafka.producer.compression.type = snappy
# set channel
a1.channels.channel1.type = file
a1.channels.channel1.checkpointDir = /usr/local/apache-flume-1.8.0-bin/data/flume_data/checkpoint
a1.channels.channel1.dataDirs= /usr/local/apache-flume-1.8.0-bin/data/flume_data/data
# bind 一个源可以进入多个通道,一个sink只能连一个通道
a1.sources.source1.channels = channel1
a1.sinks.sink1.channel = channel1
可以看到上面配置信息中
#a1.sinks.sink1.kafka.bootstrap.servers = Desktop:9092,Server1:9092,Server2:9092,Server3:9092
被注释掉,改换成
a1.sinks.sink1.brokerList= Desktop:9092,Server1:9092,Server2:9092,Server3:9092
这里根据官网配置a1.sinks.sink1.brokerList这个属性已经被弃用,但是使用a1.sinks.sink1.kafka.bootstrap.servers 属性会报错。
2.创建所需文件夹
zhang@Server3:~$ mkdir -p /usr/local/apache-flume-1.8.0-bin/data/spoolDirLog
zhang@Server3:~$ mkdir -p /usr/local/apache-flume-1.8.0-bin/data/flume_data/checkpoint
zhang@Server3:~$ mkdir -p /usr/local/apache-flume-1.8.0-bin/data/flume_data/data
3.在kafka上创建名为spoolDirLog的topic
在Server3上创建的。
zhang@Server3:~$ kafka-topics.sh --zookeeper Desktop:2181,Server1:2181,Server2:2181,Server3:2181 --create --topic spoolDirLog --replication-factor 1 --partitions 3
如果topic已经存在的话,会提示Error while executing topic command : Topic 'spoolDirLog' already exists.
4.启动flume
在Server3上新开一个终端并启动flume
zhang@Server3:~$ flume-ng agent --conf-file /usr/local/apache-flume-1.8.0-bin/conf/single_agent.conf --name a1 -Dflume.root.logger=INFO,console

可以看到已经启动成功。
5.创建一个kafka的consumer
在其他slave(我这里是在Server1)上创建一个spoolDirLog的topic。这里创建的topic要与第3的名字一样,否则consumer会接收不到消息。
kafka-console-consumer.sh --zookeeper Desktop:2181,Server1:2181,Server2:2181,Server3:2181 --topic spoolDirLog --from-beginning
6.产生消息
在第3步中Server3在的终端,非新开的Server3终端中制造消息:
zhang@Server3:~$ echo "Hello flume,welcome to csdn" > ~/flumetest5.log
zhang@Server3:~$ mv flumetest5.log /usr/local/apache-flume-1.8.0-bin/data/spoolDirLog
zhang@Server3:~$
然后在第5步新开的Server3终端上可以看见
接着在消费者终端,也就是第5步的Slave(Server1)上可以看到消息接收成功:

可以看到已经成功接收到“Hello flume,welcome to csdn”。
测试成功。
7.停止flume agent
在第4步新开的终端中同时按住ctrl+C。
b.实时收集方式
1.创建配置文档
在conf文件目录中创建single_agent.conf配置文件,
编辑文件
vim single_agent.conf
# agent
a1.sources = source1
a1.channels = channel1
a1.sinks = sink1
# set source
#a1.sources.source1.type = spooldir
#a1.sources.source1.spoolDir=/usr/local/apache-flume-1.8.0-bin/data/spoolDirLog
#a1sources.source1.fileHeader = flase
a1.sources.source1.type = exec
a1.sources.source1.command = for i in /usr/local/apache-flume-1.8.0-bin/data/spoolDirLog/*.log; do cat $i; done
a1.sources.source1.restart = true
a1.sources.source1.shell = /bin/bash -c
# set sink
a1.sinks.sink1.type = org.apache.flume.sink.kafka.KafkaSink
#a1.sinks.sink1.kafka.bootstrap.servers = Desktop:9092,Server1:9092,Server2:9092,Server3:9092
a1.sinks.sink1.brokerList= Desktop:9092,Server1:9092,Server2:9092,Server3:9092
a1.sinks.sink1.topic= spoolDirLog
a1.sinks.sink1.kafka.flumeBatchSize = 20
a1.sinks.sink1.kafka.producer.acks = 1
a1.sinks.sink1.kafka.producer.linger.ms = 1
a1.sinks.sink1.kafka.producer.compression.type = snappy
# set channel
a1.channels.channel1.type = file
a1.channels.channel1.checkpointDir = /usr/local/apache-flume-1.8.0-bin/data/flume_data/checkpoint
a1.channels.channel1.dataDirs= /usr/local/apache-flume-1.8.0-bin/data/flume_data/data
# bind
a1.sources.source1.channels = channel1
a1.sinks.sink1.channel = channel1
2.创建所需文件夹
zhang@Server3:~$ mkdir -p /usr/local/apache-flume-1.8.0-bin/data/spoolDirLog
zhang@Server3:~$ mkdir -p /usr/local/apache-flume-1.8.0-bin/data/flume_data/checkpoint
zhang@Server3:~$ mkdir -p /usr/local/apache-flume-1.8.0-bin/data/flume_data/data
3.在kafka上创建名为spoolDirLog的topic
在Server3上创建的。
zhang@Server3:~$ kafka-topics.sh --zookeeper Desktop:2181,Server1:2181,Server2:2181,Server3:2181 --create --topic spoolDirLog --replication-factor 1 --partitions 3
如果topic已经存在的话,会提示Error while executing topic command : Topic 'spoolDirLog' already exists.
4.启动flume
在Server3上新开一个终端并启动flume
zhang@Server3:~$ flume-ng agent --conf-file /usr/local/apache-flume-1.8.0-bin/conf/single_agent.conf --name a1 -Dflume.root.logger=INFO,console

可以看到已经启动成功。
5.创建一个kafka的consumer
在其他slave(我这里是在Server1)上创建一个spoolDirLog的topic。这里创建的topic要与第3的名字一样,否则consumer会接收不到消息。
kafka-console-consumer.sh --zookeeper Desktop:2181,Server1:2181,Server2:2181,Server3:2181 --topic spoolDirLog --from-beginning
6.产生消息
在Server3的终端(不是新开的那个启动flume的终端)上执行以下命令。
zhang@Server3:/usr/local/apache-flume-1.8.0-bin/data/spoolDirLog$ ls
1.log
zhang@Server3:/usr/local/apache-flume-1.8.0-bin/data/spoolDirLog$ cat 1.log
what?you are a pig
what?you are a dog
zhang@Server3:/usr/local/apache-flume-1.8.0-bin/data/spoolDirLog$ echo what?you are a big pig >> 1.log
zhang@Server3:/usr/local/apache-flume-1.8.0-bin/data/spoolDirLog$
可以看到在Server1上显示如下信息:
不过由于在配置文件中的command命令监控的是整个文件的所有log日志,并将每个日志文件的内容输出到channel,所以信息是重复的。