[root@hadoop102 flume]$ sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@hadoop102 flume]$ sudo yum -y install httpd php
[root@hadoop102 flume]$ sudo yum -y install rrdtool perl-rrdtool rrdtool-devel
[root@hadoop102 flume]$ sudo yum -y install apr-devel
[root@hadoop102 flume]$ sudo yum -y install ganglia-gmetad
[root@hadoop102 flume]$ sudo yum -y install ganglia-web
[root@hadoop102 flume]$ sudo yum install -y ganglia-gmond
Ganglia由gmond、gmetad和gweb三部分组成。
gmond(Ganglia Monitoring Daemon)是一种轻量级服务,安装在每台需要收集指标数据的节点主机上。使用gmond,你可以很容易收集很多系统指标数据,如CPU、内存、磁盘、网络和活跃进程的数据等。
gmetad(Ganglia Meta Daemon)整合所有信息,并将其以RRD格式存储至磁盘的服务。
gweb(Ganglia Web)Ganglia可视化工具,gweb是一种利用浏览器显示gmetad所存储数据的PHP前端。在Web界面中以图表方式展现集群的运行状态下收集的多种不同指标数据。
4) 修改配置文件/etc/httpd/conf.d/ganglia.conf
[root@hadoop102 flume]$ sudo vim /etc/httpd/conf.d/ganglia.conf
5) 修改配置文件/etc/ganglia/gmetad.conf
[root@hadoop102 flume]$ sudo vim /etc/ganglia/gmetad.conf
修改为:
data_source “hadoop102” 192.168.1.102 (修改hadoop 地址)
6) 修改配置文件/etc/ganglia/gmond.conf
[root@hadoop102 flume]$ sudo vim /etc/ganglia/gmond.conf
修改为:
cluster { name = "hadoop102" 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 host = 192.168.1.102 port = 8649 ttl = 1 } udp_recv_channel { \# mcast_join = 239.2.11.71 port = 8649 bind = 192.168.1.102 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 }7) 修改配置文件/etc/selinux/config
[root@hadoop102 flume]$ 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本次生效关闭必须重启,如果此时不想重启,可以临时生效之:
[root@hadoop102 flume]$ sudo setenforce 0
5) 启动ganglia
[root@hadoop102 flume]$ sudo service httpd start
[root@hadoop102 flume]$ sudo service gmetad start
[root@hadoop102 flume]$ sudo service gmond start
6) 打开网页浏览ganglia****页面
http://hadoop/ganglia
尖叫提示:如果完成以上操作依然出现权限不足错误,请修改/var/lib/ganglia目录的权限:
[root@hadoop102 flume]$ sudo chmod -R 777 /var/lib/ganglia
[root@hadoop102 flume]$
bin/flume-ng agent \ --conf conf/ \ --name a1 \ --conf-file job/flume-netcat-logger.conf \ -Dflume.root.logger==INFO,console \ -Dflume.monitoring.type=ganglia \ -Dflume.monitoring.hosts=本机ip:8649[root@hadoop102 flume]$ nc localhost 44444
Source是负责接收数据到Flume Agent的组件。Source组件可以处理各种类型、各种格式的日志数据,包括avro、thrift、exec、jms、spooling directory、netcat、sequence generator、syslog、http、legacy。官方提供的source类型已经很多,但是有时候并不能满足实际开发当中的需求,此时我们就需要根据实际需求自定义某些source。
使用flume接收数据,并给每条数据添加前缀,输出到控制台。前缀可从flume配置文件中配置。
导入pom****依赖
<dependencies> <dependency> <groupId>org.apache.flume</groupId> <artifactId>flume-ng-core</artifactId> <version>1.7.0</version> </dependency> </dependencies> package com.atguigu; import org.apache.flume.Context; 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; public class MySource extends AbstractSource implements Configurable, PollableSource { //定义配置文件将来要读取的字段 private Long delay; private String field; //初始化配置信息 @Override public void configure(Context context) { delay = context.getLong("delay"); field = context.getString("field", "Hello!"); } @Override public Status process() throws EventDeliveryException { try { //创建事件头信息 HashMap<String, String> hearderMap = new HashMap<>(); //创建事件 SimpleEvent event = new SimpleEvent(); //循环封装事件 for (int i = 0; i < 5; i++) { //给事件设置头信息 event.setHeaders(hearderMap); //给事件设置内容 event.setBody((field + i).getBytes()); //将事件写入channel getChannelProcessor().processEvent(event); Thread.sleep(delay); } } catch (Exception e) { e.printStackTrace(); return Status.BACKOFF; } return Status.READY; } @Override public long getBackOffSleepIncrement() { return 0; } @Override public long getMaxBackOffSleepInterval() { return 0; } }将写好的代码打包,并放到flume的lib目录(/opt/module/flume)下。
[root@hadoop102 flume]$ pwd
/opt/module/flume
[root@hadoop102 flume]$ bin/flume-ng agent -c conf/ -f job/mysource.conf -n a1 -Dflume.root.logger=INFO,console
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。
使用flume接收数据,并在Sink端给每条数据添加前缀和后缀,输出到控制台。前后缀可在flume任务配置文件中配置。
将写好的代码打包,并放到flume的lib目录(/opt/module/flume)下。
[root@hadoop102 flume]$ pwd
/opt/module/flume
[root@hadoop102 flume]$ bin/flume-ng agent -c conf/ -f job/mysink.conf -n a1 -Dflume.root.logger=INFO,console
[root@hadoop102 ~]$ nc localhost 44444
hello
OK
atguigu
OK
实时监控MySQL,从MySQL中获取数据传输到HDFS或者其他存储框架,所以此时需要我们自己实现MySQLSource。
官方也提供了自定义source的接口: 官网说明:https://flume.apache.org/FlumeDeveloperGuide.html#source图6-1 自定义MySQLSource组成
根据官方说明自定义mysqlsource需要继承AbstractSource类并实现Configurable和PollableSource接口。
实现相应方法:
getBackOffSleepIncrement()//暂不用
getMaxBackOffSleepInterval()//暂不用
configure(Context context)//初始化context
process()//获取数据(从mysql获取数据,业务处理比较复杂,所以我们定义一个专门的类——SQLSourceHelper来处理跟mysql的交互),封装成event并写入channel,这个方法被循环调用
stop()//关闭相关的资源
PollableSource:从source中提取数据,将其发送到channel。
Configurable:实现了Configurable的任何类都含有一个context,使用context获取配置信息。
在classpath下添加jdbc.properties和log4j.properties