flume自定义 file sink 以本地时间,event数据为目录

参考:https://www.cnblogs.com/sunyaxue/p/6645753.html

数据收集为file时,不能以本地时间和数据内容分类接入文件,参考别人的文章实现

代码:

package flume;

import org.apache.avro.util.Utf8;
import org.apache.flume.*;
import org.apache.flume.conf.Configurable;
import org.apache.flume.sink.AbstractSink;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MySinks extends AbstractSink implements Configurable {
    private static final Logger logger = LoggerFactory.getLogger(MySinks.class);
    private static final String PROP_KEY_ROOTPATH = "sink.directory";
    private String fileName;
    private String filePath;
    private File path;
//    private static final SimpleDateFormat timeFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat timeFormater = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat timeFormater1 = new SimpleDateFormat("HH");

    @Override
    public void configure(Context context) {
        filePath = context.getString(PROP_KEY_ROOTPATH);
    }

    @Override
    public Status process() throws EventDeliveryException {
        Channel ch = getChannel();
        //get the transaction
        Transaction txn = ch.getTransaction();
        Event event = null;
        //begin the transaction
        txn.begin();
        while (true) {
            event = ch.take();
            if (event != null) {
                break;
            }
        }
        try {

            logger.debug("Get event.");

            String body = new String(event.getBody());
            String res = body + "\r\n";
            String logType = body.substring(body.lastIndexOf("|") + 1);
            String dayTime = timeFormater.format(new Date());
            String hourTime = timeFormater1.format(new Date());
            path = new File(filePath+"/" + dayTime + "/" + hourTime);
            if (!path.exists()) {
                path.mkdirs();
            }
            fileName = path +"/"+ logType;
            File file = new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fos = null;
            BufferedWriter pw=null;
            try {
                fos = new FileOutputStream(file, true);
                OutputStreamWriter osw = new OutputStreamWriter(fos,"Utf8");
                pw = new BufferedWriter(osw);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                pw.write(new String(res.getBytes("iso-8859-1"), "utf-8"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                pw.close();
                fos.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
            txn.commit();
            return Status.READY;
        } catch (Throwable th) {
            txn.rollback();

            if (th instanceof Error) {
                throw (Error) th;
            } else {
                throw new EventDeliveryException(th);
            }
        } finally {
            txn.close();
        }
    }


}





conf:

#flume flile
agent.sources=httpSrc
agent.channels=c1
agent.sinks=k1




agent.sources.httpSrc.type=http
agent.sources.httpSrc.bind=172.16.90.62
agent.sources.httpSrc.port=55555
agent.sources.httpSrc.channels=c1


agent.sources.httpSrc.interceptors = i2
agent.sources.httpSrc.interceptors.i2.type = flume.LogAnalysis$Builder
agent.sources.httpSrc.interceptors.i2.regex = ([^+]*)log_
agent.sources.httpSrc.interceptors.i2.serializers = s1 s2 
agent.sources.httpSrc.interceptors.i2.serializers.s1.name = data
agent.sources.httpSrc.interceptors.i2.serializers.s2.name = type



agent.channels.c1.type=memory
agent.channels.c1.capacity=100000
agent.channels.c1.transactionCapacity=100000




agent.sinks.k1.type = flume.MySinks
agent.sinks.k1.sink.directory=/data/flume_data/
agent.sinks.k1.channel=c1
agent.sinks.k1.sink.rollInterval=300

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一天两晒网

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值