1、端口监听
1.1、编写配置文件
编写配置文件,并保存为netcatToLog.conf
# 给agent起一个名字为a
a.sources = r1
a.channels = c1
a.sinks = k1
# 配置source
a.sources.r1.type = netcat
a.sources.r1.bind = 0.0.0.0
a.sources.r1.port = 8888
# 配置channel
a.channels.c1.type = memory
# 配置sink
a.sinks.k1.type = logger
# 绑定source与sink
a.sources.r1.channels = c1
a.sinks.k1.channel = c1
1.2、执行任务
使用如下命令执行任务 flume-ng agent -n a -f netcatToLog.conf flume已经处于监听状态  使用telnet像对应端口发送消息 telnet localhost 8888   如上图监听成功
2、监控指定目录
目的:使用flume监控指定目录,将目录下新建的文件上传至HDFS
2.1、编写配置文件
将配置文件保存为,spoolDir.conf
# 给agent起一个名字
a.sources = r1
a.channels = c1
a.sinks = k1
# 配置source
a.sources.r1.type = spooldir
a.sources.r1.spoolDir = /root/flumeDir
# 配置channel
a.channels.c1.type = memory
# 配置sink
a.sinks.k1.type = hdfs
a.sinks.k1.hdfs.path = /flume/data1
# 指定文件类型为 流 来什么输出什么
a.sinks.k1.hdfs.fileType = DataStream
# 指定文件输出格式
a.sinks.k1.hdfs.writeFormat = text
# 指定文件前缀名
a.sinks.k1.hdfs.filePrefix = testFile
# 指定文件名后缀
a.sinks.k1.hdfs.fileSuffix = .txt
# 指定文件达到多大时写入
a.sinks.k1.hdfs.rollSize = 10240
# 指定多少条数据写入
a.sinks.k1.hdfs.rollCount = 1000
# 组装
a.sources.r1.channels = c1
a.sinks.k1.channel = c1
2.2、执行任务
执行如下命令,启动flume flume-ng agent -n a -f spoolDir.conf
去所监控的文件夹/root/flumeDir 下,创建一个文件并保存  在HDFS上查看结果 
3、监控指定文件并过滤内容
目的:监控指定的文件,并根据规则过滤出符合要求的内容
3.1、编写配置文件
保存文件为webFilter.conf
# 给agent起一个名字
a.sources = r1
a.channels = c1
a.sinks = k1
# 配置source
a.sources.r1.type = spooldir
a.sources.r1.spoolDir = /root/flumeDir
# 给拦截器起一个名字
a.sources.r1.interceptors = i1
# 配置拦截器
a.sources.r1.interceptors.i1.type = regex_filter
a.sources.r1.interceptors.i1.regex = https.*com
# 配置channel
a.channels.c1.type = memory
a.channels.c1.capacity = 1000
# 表示sink每次会从channel里取多少数据
a.channels.c1.transactionCapacity = 200
# 配置sink
a.sinks.k1.type = logger
# 虽然设置了大小,但是body还是只能显示16个字节大小!!!,就很郁闷。
# 貌似是bug
a.sinks.k1.maxBytesToLog = 1024
# 组装
a.sources.r1.channels = c1
a.sinks.k1.channel = c1
flumeDir目录下website.txt文件内容 
3.2、执行任务
flume-ng agent -n a -f webFilter.conf
结果如下,已经过滤出来。和上述文件内容对比,已经将所符合的内容过滤出来。 虽然指定了body大小,但是没有生效。 

|