自定义flume-source

package com.bigdata.flume;

import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.EventDeliveryException;
import org.apache.flume.PollableSource;
import org.apache.flume.conf.Configurable;
import org.apache.flume.event.EventBuilder;
import org.apache.flume.source.AbstractSource;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Random;

/**
 * 
 */
public class MySource extends AbstractSource implements Configurable, PollableSource {

    @Override
    public void configure(Context context) {

    }

    @Override
    public Status process() throws EventDeliveryException {
        Status status = null;

        try{
            Random random = new Random();
            int randomNum = random.nextInt(100);
            String text = "Hello World :" + random.nextInt(100);
            HashMap<String, String> header = new HashMap<String, String>();
            header.put("id", random+"");
            this.getChannelProcessor()
                    .processEvent(EventBuilder.withBody(text, Charset.forName("UTF-8"), header));

            status = Status.READY;
        }catch (Throwable t) {
            // Log exception, handle individual exceptions as needed

            status = Status.BACKOFF;

            // re-throw all Errors
            if (t instanceof Error) {
                throw (Error)t;
            }
        }
        return status;
    }

}

2.flume-source.conf

#
a1.sources=r1
a1.sinks=k1
a1.channels=c1
  
#
a1.sources.r1.type=com.bigdata.flume.MySource
  
#
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100
a1.channels.c1.byteCapacityBufferPercentage=20
a1.channels.c1.byteCapacity=800000
  
#
a1.sinks.k1.type=file_roll
#注意目录得是虚拟机上的
a1.sinks.k1.sink.directory=/tmp/a
  
# 
a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1

#运行命令bin/flume-ng agent --conf conf --name a1 --conf-file conf/fulme-source.conf -Dflume.root.logger=INFO,console
3.结果



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值