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

你可能感兴趣的:(flume)