Flume自定义jar包
自定义source
Source 是负责接收数据到 Flume Agent 的组件。Source 组件可以处理各种类型、各种格式的日志数据,包括 avro、thrift、exec、jms、spooling directory、netcat、sequence ,generator、syslog、http、legacy。官方提供的 source 类型已经很多,但是有时候并不能满足实际开发当中的需求,此时我们就需要根据实际需求自定义某些 source
官方也提供了自定义 source 的接口:https://flume.apache.org/FlumeDeveloperGuide.html#source
根据官方说明自定义MySource 需要继承AbstractSource 类并实现 Configurable 和 PollableSource 接口
实现相应方法:
getBackOffSleepIncrement() //backoff 步长
getMaxBackOffSleepInterval()//backoff 最长时间
configure(Context context)//初始化 context(读取配置文件内容)
process()//获取数据封装成 event 并写入 channel,这个方法将被循环调用。
使用场景:读取 MySQL 数据或者其他文件系统
需求:使用flume接受数据,并给每条数据添加前缀,输出到控制台,前缀可以从flume配置文件中配置
自定义jar包
package com.hadoop.source;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.EventDeliveryException;
import org.apache.flume.PollableSource;
import org.apache.flume.conf.Configurable;
import org.apache.flume.event.SimpleEvent;
import org.apache.flume.source.AbstractSource;
import java.util.HashMap;
/**
* @author H
* @create 2022/11/21 9:55
*/
public class Mysource extends AbstractSource implements Configurable, PollableSource {
//声明数据的前后缀
private String perfix;//前缀
private String subfix;//后缀
private Long delay;
@Override
public void configure(Context context) {
//context会获取配置文件
perfix = context.getString("per","per-");
subfix = context.getString("sub");
delay = context.getLong("delay",2000L);
}
@Override
public Status process() throws EventDeliveryException {
//1.声明事件
Event event = new SimpleEvent();
HashMap<String, String> header = new HashMap<>();
//2.循环创建事件信息,传给channel
try {
for (int i = 0; i < 5; i++) {
event.setHeaders(header);
event.setBody((perfix + "hadoop:" + i + subfix).getBytes());
getChannelProcessor().processEvent(event);
}
Thread.sleep(delay);
return Status.READY;
} catch (Exception e) {
e.printStackTrace();
return Status.BACKOFF;
}
}
@Override
public long getBackOffSleepIncrement() {
return 0;
}
@Override
public long getMaxBackOffSleepInterval() {
return 0;
}
}
在集群配置flume配置文件
[master job]$ vim mysource.conf
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = com.hadoop.source.Mysource
#a1.sources.r1.pre =
#a1.sources.r1.sub =
#a1.sources.r1.delay = 3000
#a1.sources.r1.field = atguigu
# 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 agent -c conf -n a1 -f job/mysource.conf -Dflume.root.logger=INFO,console
自定义sink
Sink 不断地轮询 Channel 中的事件且批量地移除它们,并将这些事件批量写入到存储或索引系统、或者被发送到另一个 Flume Agent。
Sink 是完全事务性的。在从 Channel 批量删除数据之前,每个 Sink 用 Channel 启动一个事务。批量事件一旦成功写出到存储系统或下一个 Flume Agent,Sink 就利用 Channel 提交事务。事务一旦被提交,该 Channel 从自己的内部缓冲区删除事件。
Sink 组件目的地包括 hdfs、logger、avro、thrift、ipc、file、null、HBase、solr、自定义。官方提供的 Sink 类型已经很多,但是有时候并不能满足实际开发当中的需求,此时我们就需要根据实际需求自定义某些 Sink。
官方也提供了自定义 sink 的接口:
https://flume.apache.org/FlumeDeveloperGuide.html#sink 根据官方说明自定义
MySink 需要继承 AbstractSink 类并实现 Configurable 接口。
实现相应方法:
configure(Context context)//初始化 context(读取配置文件内容)
process()//从 Channel 读取获取数据(event),这个方法将被循环调用。
使用场景:读取 Channel 数据写入 MySQL 或者其他文件系统
需求:使用 flume 接收数据,并在 Sink 端给每条数据添加前缀和后缀,输出到控制台。前后缀可在 flume 任务配置文件中配置
jar包代码
package com.hadoop.sink;
import org.apache.flume.*;
import org.apache.flume.conf.Configurable;
import org.apache.flume.sink.AbstractSink;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author H
* @create 2022/11/21 15:23
*/
public class Mysink extends AbstractSink implements Configurable {
//声明数据的前后缀
private String perfix;//前缀
private String subfix;//后缀
//创建Logger对象
private Logger logger = LoggerFactory.getLogger(Mysink.class);
@Override
public void configure(Context context) {
//context会获取配置文件
perfix = context.getString("per","per-");
subfix = context.getString("sub");
}
@Override
public Status process() throws EventDeliveryException {
//1.获取channel并开启事务
Channel channel = getChannel();
Transaction transaction = channel.getTransaction();
transaction.begin();
//2.从channel中抓取数据打印到控制台
try {
//2.1抓取数据
Event event;
while (true){
event = channel.take();
if (event != null){
break;
}
}
//2.2处理数据
logger.info(perfix + new String(event.getBody()) + subfix);
//2.3提交事务
transaction.commit();
return Status.READY;
} catch (ChannelException e) {
transaction.rollback();
return Status.BACKOFF;
} finally {
transaction.close();
}
}
}
创建配置文件
[master job]$ vim mysink.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 = com.hadoop.sink.Mysink
#a1.sinks.k1.per = hadoop-
a1.sinks.k1.sub = -bs
# 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 agent -c conf -n a1 -f job/mysink.conf -Dflume.root.logger=INFO,console
nc localhost 44444
jps -l
kill -9 杀死进程
Flume数据流监控
Ganglia安装与部署
Ganglia 由 gmond、gmetad 和 gweb 三部分组成。
gmond(Ganglia Monitoring Daemon)是一种轻量级服务,安装在每台需要收集指标数据的节点主机上。使用 gmond,你可以很容易收集很多系统指标数据,如 CPU、内存、磁盘、网络和活跃进程的数据等。
gmetad(Ganglia Meta Daemon)整合所有信息,并将其以 RRD 格式存储至磁盘的服务。
gweb(Ganglia Web)Ganglia 可视化工具,gweb 是一种利用浏览器显示 gmetad 所存储数据的 PHP 前端。在 Web 界面中以图表方式展现集群的运行状态下收集的多种不同指标数据
安装gnaglia
部署:
master : web gmetad gmod
slave1 : gmod
slave2 : gmod
在master slave1 slave2 分别安装epel-release
sudo yum -y install epel-release
在master安装
sudo yum -y install ganglia-gmetad
sudo yum -y install ganglia-web
sudo yum -y install ganglia-gmond
slave1 slave2 安装
sudo yum -y install ganglia-gmond
在 master 修改配置文件/etc/httpd/conf.d/ganglia.conf
vim /etc/httpd/conf.d/ganglia.conf
# Ganglia monitoring system php web frontend
#
Alias /ganglia /usr/share/ganglia
<Location /ganglia>
Order deny,allow
Require all granted
#Deny from all
#Allow from 127.0.0.1
#Allow from ::1
# Allow from .example.com
</Location>
在 master 修改配置文件/etc/ganglia/gmetad.conf
sudo vim /etc/ganglia/gmetad.conf
data_source "my cluster" master
在master slave1 slave2 修改配置文件/etc/ganglia/gmond.conf
sudo vim /etc/ganglia/gmond.conf
修改为:
cluster {
name = "my cluster"
owner = "unspecified"
latlong = "unspecified"
url = "unspecified"
}
udp_send_channel {
#bind_hostname = yes # Highly recommended, soon to be default.
# This option tells gmond to use a source
address
# that resolves to the machine's hostname.
Without
# this, the metrics may appear to come from
any
# interface and the DNS names associated with
# those IPs will be used to create the RRDs.
# mcast_join = 239.2.11.71
# 数据发送给 master
host = master
port = 8649
ttl = 1
}
udp_recv_channel {
# mcast_join = 239.2.11.71
port = 8649
# 接收来自任意连接的数据
bind = 0.0.0.0
retry_bind = true
# Size of the UDP buffer. If you are handling lots of metrics
you really
# should bump it up to e.g. 10MB or even higher.
# buffer = 10485760
}
在 master 修改配置文件/etc/selinux/config
sudo vim /etc/selinux/config
修改为:
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
selinux 生效需要重启,如果此时不想重启,可以临时生效之:
[master flume]$ sudo setenforce 0
启动ganglia
在master slave1 slave2启动
sudo systemctl start gmond
在master启动
sudo systemctl start httpd
sudo systemctl start gmetad
sudo systemctl stop gmond
sudo systemctl stop httpd
sudo systemctl stop gmetad
打开网页浏览ganglia页面
如果完成以上操作依然出现权限不足错误,请修改/var/lib/ganglia 目录的权限
[master flume]$ sudo chmod -R 777 /var/lib/ganglia
操作 Flume 测试监控
启动Flume任务
bin/flume-ng agent \
-c conf/ \
-n a1 \
-f job/flume-netcat-logger.conf \
-Dflume.root.logger=INFO,console \
-Dflume.monitoring.type=ganglia \
-Dflume.monitoring.hosts=hadoop102:8649
发送数据观察ganglia检测图
nc localhost 44444
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律