package com.ihooyah.flume.sink;

import org.apache.flume.*;
import org.apache.flume.conf.Configurable;
import org.apache.flume.sink.AbstractSink;
import org.mortbay.util.ajax.JSON;

/**
 * 描述 检测到flume channls中的文件信息并处理文件内容
 *
 * @author fatiaojie
 * @date 2020/4/2 14:06
 */
public class DirHandleSink extends AbstractSink implements Configurable {


    @Override
    public Status process() throws EventDeliveryException {
        Status status = Status.READY;
        Channel channel = this.getChannel();
        Transaction transaction = channel.getTransaction();
        try {
            transaction.begin();
            Event event = channel.take();
            if (event != null) {
                String body = new String(event.getBody(), "UTF-8");
                System.out.println("新文件已经获取到内容为:");
                System.out.println(body);
            }
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
            status = Status.BACKOFF;
            e.printStackTrace();
        } finally {
            transaction.close();
        }
        return status;
    }

    @Override
    public void configure(Context context) {

    }
}
