目录
1、Flume插件
1.1 简介
Apache Flume 是一种分布式的、高可靠的、高可用的日志收集聚合系统,将不同来源海量的日志数据传输到集中的数据存储。
Flume agent 负责把外部事件流(数据流)传输到指定下一跳,agent包括source(数据源)、channel(传输通道)、sink(接收端)。Flume agent可以多跳级联,组成复杂的数据流。 Flume 支持多种类型的source:Avro数据源(序列化数据格式)、Thrift数据源(通讯协议格式)、Kafka数据源(消息队列)、NetCat数据源(网络访问)、Syslog数据源(系统日志)、文件数据源(普通文本)、自定义数据源等,可灵活地与应用系统集成,需要较少的开发代价。 Flume 能够与常见的大数据工具结合,支持多种sink:HDFS、Hive、HBase、Kafka等,将数据传输到这些系统,进行进一步分析处理。
1.2 安装
#下载Apache Flume压缩包(需要JDK环境)
wget https://mirror.bit.edu.cn/apache/flume/1.9.0/apache-flume-1.9.0-bin.tar.gz
tar zxvf apache-flume-1.9.0-bin.tar.gz
mv apache-flume-1.9.0-bin flume-1.9.0
1.3 配置
#拷贝配置文件,从监听端口获取数据,保存到本地文件
cd flume-1.9.0
cp conf/flume-conf.properties.template conf/flume-conf.properties
vim conf/flume-conf.properties
#编辑配置如下
---------------------------------------------------------------------------------
# The configuration file needs to define the sources,
# the channels and the sinks.
# Sources, channels and sinks are defined per agent,
# in this case called 'agent'
agent.sources = r1
agent.channels = c1
agent.sinks = s1
# For each one of the sources, the type is defined
agent.sources.r1.type = netcat
agent.sources.r1.bind = 192.168.223.128 #PS:这个地方配成localhost默认会转127.0.0.1
agent.sources.r1.port = 8888
# The channel can be defined as follows.
agent.sources.r1.channels = c1
# Each sink's type must be defined
agent.sinks.s1.type = file_roll
agent.sinks.s1.sink.directory = /usr/local/flume-1.9.0/logs
#Specify the channel the sink should use
agent.sinks.s1.channel = c1
agent.sinks.s1.hdfs.roundUnit = minute
agent.sinks.s1.hdfs.rollInterval = 2
# Each channel's type is defined.
agent.channels.c1.type = memory
# Other config values specific to each type of channel(sink or source)
# can be defined as well
# In this case, it specifies the capacity of the memory channel
agent.channels.c1.capacity = 100
1.4 测试
1.4.1 编写Flume启动/停止/重启脚本
flume的启动,停止,重启脚本比较麻烦,我们这里使用shell脚本编写一个一键启动:
vim bin/flume.sh
---------------------------输入如下配置----------------------------------------
#!/bin/bash
#echo "begin start flume..."
#flume的安装根目录(根据自己情况,修#!/bin/bash
#echo "begin start flume..."
#flume的安装根目录(根据自己情况,修改为自己的安装目录)
path=/usr/local/flume-1.9.0
echo "flume home is :$path"
#flume的进程名称,固定值(不用修改)
JAR="flume"
#flume的配置文件名称(根据自己的情况,修改为自己的flume配置文件名称)
Flumeconf="flume-conf.properties"
#定义的soure名称
agentname="agent"
function start(){
echo "begin start flume process ...."
#查找flume运行的进程数
num=$(ps -ef|grep flume-conf|grep -v grep|wc -l)
echo $num
#判断是否有flume进程运行,如果没有则运行执行启动命令
if [ $num -lt 1 ] ;then
$path/bin/flume-ng agent --conf conf -f $path/conf/$Flumeconf --name $agentname -Dflume.root.logger=INFO,console &
echo "start success...."
echo "日志路径: $path/logs/flume.log"
else
echo "进程已经存在,启动失败,请检查....."
exit 0
fi
}
function stop(){
echo "begin stop flume process.."
num=$(ps -ef|grep flume|wc -l)
echo $num
#echo "$num...."
if [ $num -gt 0 ];then
#停止flume
ps -ef|grep flume-conf|awk '{print $2;}'|xargs kill
echo "进程已经关闭..."
else
echo "服务未启动,无须停止..."
fi
}
function restart(){
echo "begin stop flume process .."
#判断程序是否彻底停止
num=$(ps -ef|grep flume|wc -l)
#stop完成之后,查找flume的进程数,判断进程数是否为0,如果不为0,则休眠5秒,再次查看,直到进程数为0
if [ $num -gt 0 ];then
stop
echo "flume process stoped,and starting..."
fi
#执行start
start
echo "started...."
}
#case 命令获取输入的参数,如果参数为start,执行start函数,如果参数为stop执行stop函数,如果参数为restart,执行restart函数
case "$1" in
"start")
start $@
exit 0
;;
"stop")
stop
exit 0
;;
"restart")
restart $@
exit 0
;;
*)
echo "用法: $0 {start|stop|restart}"
exit 1
;;
esac
命令介绍
参数 | 作用 | 举例 |
---|---|---|
–conf 或 -c | 指定配置文件夹,包含flume-env.sh和log4j的配置文件 | –conf conf |
–conf-file 或 -f | 配置文件地址 | –conf-file conf/flume-conf.properties |
–name 或 -n | agent名称 | –name agent |
启动命令:./bin/flume.sh start
1.4.2 启动客户端测试
[root@ydt1 logs]# telnet 127.0.0.1 8888
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
hello laohu
OK
[root@ydt1 logs]# ll -s
总用量 12
4 -rw-r--r--. 1 root root 25 10月 10 20:47 1602333960297-3
4 -rw-r--r--. 1 root root 23 10月 10 20:47 1602333960297-4
0 -rw-r--r--. 1 root root 0 10月 10 20:48 1602333960297-5
0 -rw-r--r--. 1 root root 0 10月 10 20:48 1602333960297-6
0 -rw-r--r--. 1 root root 0 10月 10 20:49 1602333960297-7
0 -rw-r--r--. 1 root root 0 3月 3 14:09 1614751795117-1
4 -rw-r--r--. 1 root root 13 3月 3 14:10 1614751795117-2
0 -rw-r--r--. 1 root root 0 3月 3 14:10 1614751795117-3
[root@ydt1 logs]# pwd
/usr/local/flume-1.9.0/logs
#查看生成的