Flume ——自定义Source采集源(Java代码)


Flume ——自定义Source采集源(Java代码)


  在开始阅读之前让我们喊出口号:“要想人前显贵,就要人后受罪!”加油嘎嘣儿,加油所有想获得美好未来的你!

public class MyExecSource extends AbstractSource implements EventDrivenSource, Configurable{
       private String posiFile;//偏移量记录的位置
       private String filename;//所要读取的文件夹的位置
       private String charset;//读取文件的编码格式
       private Long interval;//监听数据间隔的时间
       //以上的数据域需要从conf配置中读取出来
       private ExecutorService executor;
       private DealDateThread thread;
       
}

start方法:用来创建线程池,创建线程,并将线程放到线程池

@Override 
public synchronized void start{
executor =Executors.newSingleThreadExecutor();
DealDateThread thread = new DealDateThread(filname,podiFile,interval,charset,getChannelProcessor);
executor.submit(thread);
super.start();
}

stop停止:关闭线程池,关闭线程

@Override
	public synchronized void stop() {
	    thread.flag=false;
		executor.shutdown();
	    while (!executor.isTerminated()) {
	      try {
	        executor.awaitTermination(500, TimeUnit.MILLISECONDS);
	      } catch (InterruptedException e) {
	        Thread.currentThread().interrupt();
	      }
	    }
		super.stop();
	}

DealDateThread线程类

public static class DealDateThread() implements Runnable{
       private String posiFile;
       private String filename;
       private String charset;
       private Long interval;
       private ChannleProcdssor process;
       private boolean flag=true;//做标记,为了一直让run方法进行运行
       private Long offset=ol;
       private RandomAccessFile raf ;//文件读取对象,可以按照偏移量进行读取
       private  File file;
       public DealDateThread(String filename, String posiFile, Long interval, String charset, ChannelProcessor process) {
			this.filename = filename;
			this.posiFile = posiFile;
			this.interval = interval;
			this.charset = charset;
			this.process = process;
			
			this.file = new File(posiFile);
		    try {
		    	if(!file.exists()) {
					file.createNewFile();
				}//判断记录偏移量的文件是否存在
				String offsetStr = FileUtils.readFileToString(file);
				//设置偏移量
				if(offsetStr!=null&&!offsetStr.equals("")) {
					offset = Long.parseLong(offsetStr);
				}
				raf = new RandomAccessFile(filename, "r");
				//对读取的文件进行只读操作
				raf.seek(offset);
		    }catch(Exception e) {
		    	e.printStackTrace();
		    }
		}
		//ruan方法真正进行执行的
  		public  void run(){
               while (flag){
               try {
                    //通过event对象,将数据发送到channel
					String line = raf.readLine();
					//读取数据以后将数据发给channel需要通过event对象
					if(line!=null) {
						Event e = EventBuilder.withBody(line.getBytes(charset));
						process.processEvent(e);
						 //在获取当前的偏移量进行数据更新
						this.offset=raf.getFilePointer();
						FileUtils.writeStringToFile(file, offset+"");
					}else {
						Thread.sleep(interval);
					}
				}catch(Exception e) {
					e.printStackTrace();
				}			     
       } 
}

获取配置信息

public void configure(){
        this.filename=context.getString("filename");
		this.posiFile=context.getString("posiFile");
		this.charset=context.getString("charset");
		this.interval=context.getLong("interval");
}
                                      若有写的不正确的地方,请指出。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值