flume学习日记

Flume
优点:

  1. 可以和任意存储进程集成。
  2. 输入的的数据速率大于写入目的存储的速率,flume会进行缓冲,减小hdfs的压力。
  3. flume中的事务基于channel,使用了两个事务模型(sender + receiver),确保消息被可靠发送。
    Flume使用两个独立的事务分别负责从soucrce到channel,以及从channel到sink的事件传递。一旦事务中所有的数据全部成功提交到channel,那么source才认为该数据读取完成。同理,只有成功被sink写出去的数据,才会从channel中移除。

参数说明:
–conf conf/ :表示配置文件存储在conf/目录
–name a1 :表示给agent起名为a1
–conf-file job/flume-netcat.conf :flume本次启动读取的配置文件是在job文件夹下的flume-telnet.conf文件。
-Dflume.root.logger==INFO,console :-D表示flume运行时动态修改flume.root.logger参数属性值,并将控制台日志打印级别设置为INFO级别。日志级别包括:log、info、warn、error。
a3.sources = s #定义source
a3.sinks = k #定义sink
a3.channels = c3 #定义channel

Describe/configure the source

a3.sources.r3.type = spooldir #定义source类型为目录
a3.sources.r3.spoolDir = /opt/module/flume/upload #定义监控目录
a3.sources.r3.fileSuffix = .COMPLETED #定义文件上传完后缀
a3.sources.r3.fileHeader = true #是否有文件头
a3.sources.r3.ignorePattern = ([^ ]*.tmp) #忽略所有以.tmp结尾的文件

Describe the sink

a3.sinks.k3.type = hdfs #sink类型为hdfs
a3.sinks.k3.hdfs.path = hdfs://hadoop102:9000/flume/upload/%Y%m%d/%H #文件上传的路径
a3.sinks.k3.hdfs.filePrefix = upload- #上传文件的到hdfs的前缀
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才刷新到hdfs一次
a3.sinks.k3.hdfs.fileType = DataStream #文件类型,可支持压缩
#多久生成一个新的文件
a2.sinks.k2.hdfs.rollInterval = 60
#设置每个文件的滚动大小
a2.sinks.k2.hdfs.rollSize = 134217700
#文件的滚动与Event数量无关
a2.sinks.k2.hdfs.rollCount = 0

Source是负责接收数据到Flume Agent的组件。Source组件可以处理各种类型、各种格式的日志数据,包括avro、thrift、exec、jms、spooling directory、netcat、sequence generator、syslog、http、legacy。官方提供的source类型已经很多,但是有时候并不能满足实际开发当中的需求,此时我们就需要根据实际需求自定义某些source。
如:实时监控MySQL,从MySQL中获取数据传输到HDFS或者其他存储框架,所以此时需要我们自己实现MySQLSource。
官方也提供了自定义source的接口:
官网说明:https://flume.apache.org/FlumeDeveloperGuide.html#source
根据官方说明自定义mysqlsource需要继承AbstractSource类并实现Configurable和PollableSource接口。
实现相应方法:
getBackOffSleepIncrement()//暂不用
getMaxBackOffSleepInterval()//暂不用
configure(Context context)//初始化context
process()//获取数据(从mysql获取数据,业务处理比较复杂,所以我们定义一个专门的类——SQLSourceHelper来处理跟mysql的交互),封装成event并写入channel,这个方法被循环调用
stop()//关闭相关的资源

package com.qf;

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;
}

}

Name the components on this agent

a1.sources = r1
a1.sinks = k1
a1.channels = c1

Describe/configure the source

a1.sources.r1.type = com.qf.MySource
a1.sources.r1.delay = 1000
#a1.sources.r1.field = qf

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

flume-ng agent -c conf/ -f job/mysource.conf -n a1 -Dflume.root.logger=INFO,console

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章