Flume监控端口数据小案例
案例目的 使用 Flume 监听一个端口,收集该端口数据,并打印到控制台。
实现步骤
安装netcat工具,netcat所做的就是在两台电脑之间建立链接并返回两个数据流,具体用法可见:Linux Netcat 命令——网络工具中的瑞士军刀
sudo yum install -y nc
判断44444端口是否被占用
sudo netstat -tunlp | grep 44444
创建 Flume Agent 的配置文件 flume-netcat-logger.conf
cd /usr/local/flume
mkdir job
cd job
vi flume-netcat-logger.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
运行
cd /usr/local/flume
#启动flume监听
bin/flume-ng agent -c conf/ -n a1 -f job/flume-netcat-logger.conf -Dflume.root.logger=INFO,console
重开一个终端,使用 netcat 工具向本机的 44444 端口发送内容
nc localhost 44444
在 Flume 可以监听到发送的内容
Flume实时监控单个追加文件
案例目的 实时监控单个文件的数据,并上传到 HDFS 中(不要忘记启动hadoop)
实现步骤
Flume 要想将数据输出到 HDFS,须持有 Hadoop 相关 jar 包 ,需要将以下jar包
commons-configuration-1.6.jar
hadoop-auth-2.7.7.jar
hadoop-common-2.7.7.jar
hadoop-hdfs-2.7.7.jar
commons-io-2.4.jar
htrace-core-3.1.0.jar
拷贝到/usr/local/flume/lib文件夹下
创建 Flume Agent 配置文件 flume-file-hdfs.conf
cd /usr/local/flume/job
vi flume-file-hdfs.conf
# Name the components on this agent
a2.sources = r2
a2.sinks = k2
a2.channels = c2
# Describe/configure the source
a2.sources.r2.type = exec
a2.sources.r2.command = tail -F ~/flumetest1.txt
a2.sources.r2.shell = /bin/bash -c
# Describe the sink
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://Master:9000/flume/%Y%m%d/%H
a2.sinks.k2.hdfs.filePrefix = logs-
a2.sinks.k2.hdfs.round = true
a2.sinks.k2.hdfs.roundValue = 1
a2.sinks.k2.hdfs.roundUnit = hour
a2.sinks.k2.hdfs.useLocalTimeStamp = true
a2.sinks.k2.hdfs.batchSize = 1000
a2.sinks.k2.hdfs.fileType = DataStream
a2.sinks.k2.hdfs.rollInterval = 30
a2.sinks.k2.hdfs.rollSize = 134217700
a2.sinks.k2.hdfs.rollCount = 0
# Use a channel which buffers events in memory
a2.channels.c2.type = memory
a2.channels.c2.capacity = 1000
a2.channels.c2.transactionCapacity = 100
# Bind the source and sink to the channel
a2.sources.r2.channels = c2
a2.sinks.k2.channel = c2
运行
cd /usr/local/flume
bin/flume-ng agent --conf conf/ --name a2 --conf-file job/flume-file-hdfs.conf
打开另一个终端,添加数据内容到flumetest1.txt中
打开hadoop的web 监控界面中可以查看到相应数据在hdfs文件系统中的更新
这里演示了一个使用flume监控单个追加文件的小案例,我们可以利用这种机理来监控一些日志文件,比如hive的日志文件。
Flume实时监控目录下多个新文件
案例目的 使用 Flume 监听整个目录的文件,并上传至 HDFS (不要忘记启动hadoop)
实现步骤
创建 flume-dir-hdfs.conf 配置文件(配置项已附上说明)
cd /usr/local/flume/job
vim flume-dir-hdfs.conf
#定义source、channels和sinks
a3.sources = r3
a3.sinks = k3
a3.channels = c3
#描述和配置source
a3.sources.r3.type = spooldir #定义source类型为目录
a3.sources.r3.spoolDir = ~/test1 #定义监控目录
a3.sources.r3.fileSuffix = .COMPLETED #定义文件上传后的后缀名
a3.sources.r3.ignorePattern = ([^ ]*\.tmp) #忽略所有以.tmp 结尾的文件,不上传
#描述sink
a3.sinks.k3.type = hdfs #sink类型为hdfs
a3.sinks.k3.hdfs.path = hdfs://Master:9000/flume/upload/%Y%m%d/%H #输出到何处
a3.sinks.k3.hdfs.filePrefix = upload- #上传文件的前缀
a3.sinks.k3.hdfs.round = true #是否按照时间滚动文件夹
a3.sinks.k3.hdfs.roundValue = 1 #多少时间单位创建一个新的文件夹
a3.sinks.k3.hdfs.roundUnit = hour #重新定义时间单位
a3.sinks.k3.hdfs.useLocalTimeStamp = true #是否使用本地时间戳
a3.sinks.k3.hdfs.batchSize = 100 #积攒多少个 Event 才 flush 到 HDFS 一次
a3.sinks.k3.hdfs.fileType = DataStream #设置文件类型,可支持压缩
a3.sinks.k3.hdfs.rollInterval = 60 #多久生成新文件
a3.sinks.k3.hdfs.rollSize = 134217700 #多大生成新文件
a3.sinks.k3.hdfs.rollCount = 0 #生成新文件和event无关
#描述channels
a3.channels.c3.type = memory
a3.channels.c3.capacity = 1000
a3.channels.c3.transactionCapacity = 100
#绑定source和sink到channel
a3.sources.r3.channels = c3
a3.sinks.k3.channel = c3
启动监控命令
cd /usr/local/flume
bin/flume-ng agent --conf conf/ --name a3 --conf-file job/flume-dir-hdfs.conf
向监控的test1文件夹添加文件
touch atguigu.txt
touch atguigu.tmp
touch atguigu.log
ll一下test1文件夹,查看其文件的后缀名是否发生变化
cd test1
ll
查看 HDFS 上相应目录上的数据变化