Hadoop生态圈-Flume的组件之自定义拦截器(interceptor)

                                              作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  本篇博客只是举例了一个自定义拦截器的方法,测试字节传输速度。

1>.自定义interceptor方法

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E7%94%9F%E6%80%81%E5%9C%88/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.interceptor; import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor; import java.util.List; /**
* 设置限速拦截器
* <p>
* 当 字节/时间,即同一时刻,如果进入的字节过多
* 则休眠一会
*/
public class MyInterceptor implements Interceptor { private int speed; //构造
private MyInterceptor(int speed) {
this.speed = speed;
} //do nothing
public void initialize() { } /**
* 1、拿出上一个event的时间,和当前时间进行相减,得出上一个event的时间间隔
* 2、得到上一个event的body字节数
* 3、相除得到上一个event的速度,并在此event中先进行停留,再返回event
*
* @param event
* @return
*/ long lastTime = -1;
long lastBodySize = 0; public Event intercept(Event event) { byte[] body = event.getBody();
int len = body.length; long current = System.nanoTime(); //第一个event
if (lastTime == -1) {
lastTime = current;
lastBodySize = len;
} //非第一个event
else {
//计算上一个event停留的时间
long interval = current - lastTime;
System.out.println("=========================" + current + "/" + lastTime + "/" + interval + "=========================");
//上一个event的速度
int now_speed = (int) ((double) lastBodySize / interval * 1000);
if (now_speed > speed) {
System.out.println("=========================" + now_speed + "=========================");
//计算需要停留多少秒 线程休眠,时间 = shouldTime - interval
try {
Thread.sleep((lastBodySize / speed) * 1000 - interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lastBodySize = len;
lastTime = System.currentTimeMillis(); }
return event; } //迭代List<Event>,将所有Event交给intercept(Event)进行处理
public List<Event> intercept(List<Event> events) {
for (Event event : events) {
intercept(event);
}
return events;
} //do nothing
public void close() { } public static class Builder implements Interceptor.Builder { private int speed; public void configure(Context context) {
speed = context.getInteger(Constants.SPEED, Constants.DEFAULT_SPEED); } public Interceptor build() {
return new MyInterceptor(speed);
}
} public static class Constants {
public static String SPEED = "speed";
public static int DEFAULT_SPEED = 1; }
}

2>.打包并将其发送到 /soft/flume/lib下

[yinzhengjie@s101 ~]$ cd /soft/flume/lib/
[yinzhengjie@s101 lib]$
[yinzhengjie@s101 lib]$ ll | grep MyFlume
-rw-r--r-- 1 yinzhengjie yinzhengjie 5231 Jun 20 18:53 MyFlume-1.0-SNAPSHOT.jar
[yinzhengjie@s101 lib]$
[yinzhengjie@s101 lib]$ rm -rf MyFlume-1.0-SNAPSHOT.jar
[yinzhengjie@s101 lib]$
[yinzhengjie@s101 lib]$ rz [yinzhengjie@s101 lib]$
[yinzhengjie@s101 lib]$ ll | grep MyFlume
-rw-r--r-- 1 yinzhengjie yinzhengjie 8667 Jun 20 21:02 MyFlume-1.0-SNAPSHOT.jar
[yinzhengjie@s101 lib]$
[yinzhengjie@s101 lib]$

3>.编写agent的配置文件

[yinzhengjie@s101 ~]$ more /soft/flume/conf/yinzhengjie_myInterceptor.conf
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1 # 定义源: seq
a1.sources.r1.type = seq
# 定义一次RPC产生的批次数量
a1.sources.r1.batchSize = # 指定添加拦截器
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = cn.org.yinzhengjie.interceptor.MyInterceptor$Builder
a1.sources.r1.interceptors.i1.speed = # Describe the sink
a1.sinks.k1.type = logger # Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity =
a1.channels.c1.transactionCapacity = # Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
[yinzhengjie@s101 ~]$

4>.启动flume并测试

[yinzhengjie@s101 ~]$ flume-ng agent -f /soft/flume/conf/yinzhengjie_myInterceptor.conf -n a1

  下图是运行agent部分的输出内容

Hadoop生态圈-Flume的组件之自定义拦截器(interceptor)的更多相关文章

  1. Hadoop生态圈-Flume的组件之自定义Sink

    Hadoop生态圈-Flume的组件之自定义Sink 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客主要介绍sink相关的API使用两个小案例,想要了解更多关于API的小技 ...

  2. Hadoop生态圈-Flume的组件之拦截器与选择器

      Hadoop生态圈-Flume的组件之拦截器与选择器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客只是配置的是Flume主流的Interceptors,想要了解更详细 ...

  3. Hadoop生态圈-Flume的组件之sink处理器

    Hadoop生态圈-Flume的组件之sink处理器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一. 二.

  4. Flume(二) —— 自定义拦截器、Source、Sink

    自定义拦截器 自定义Source 自定义Sink 引入依赖 <dependency> <groupId>org.apache.flume</groupId> < ...

  5. Struts2自定义拦截器Interceptor以及拦截器登录实例

    1.在Struts2自定义拦截器有三种方式: -->实现Interceptor接口 public class QLInterceptorAction implements Interceptor ...

  6. 第1节 flume:15、flume案例二,通过自定义拦截器实现数据的脱敏

    1.7.flume案例二 案例需求: 在数据采集之后,通过flume的拦截器,实现不需要的数据过滤掉,并将指定的第一个字段进行加密,加密之后再往hdfs上面保存 原始数据与处理之后的数据对比 图一  ...

  7. Struts2自定义拦截器

    1. 需求 自定义拦截器实现,用户登录的访问控制. 2. 定义拦截器类 public class LoginInterceptor extends AbstractInterceptor { @Ove ...

  8. struts2--实现自定义拦截器

    前言: 本篇文章,我想说清实现拦截器的办法,还有为什么要这样做: 目录: 1.需求目的 2.实现步骤及原理(文字怕描述不清,画图描述) 3.代码 4.总结 一.需求目的 规范或限制越级访问(例如:一个 ...

  9. java:struts框架3(自定义拦截器,token令牌,文件上传和下载(单/多))

    1.自定义拦截器: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

随机推荐

  1. React学习-React初识

    一.前言 为什么要去学习React呢,关于前端三大框架Angular,Vue,React其实都得去学吧,因为大家都在用啊,大家都再谈论啊,面试什么的都要求,没办法,曾几何时,大家都说求求大佬们别坑新了 ...

  2. Catlike学习笔记(1.4)-使用Unity构建分形

    又两个星期没写文章了,主要是沉迷 Screeps 这个游戏,真的是太好玩了导致我这两个礼拜 Github 小绿点几乎天天刷.其实想开一个新坑大概把自己写 AI 的心路历程记录下,不过觉得因为要消耗太多 ...

  3. 《Pro SQL Server Internals, 2nd edition》中CHAPTER 7 Designing and Tuning the Indexes中的Clustered Index Design Considerations一节(译)

    <Pro SQL Server Internals> 作者: Dmitri Korotkevitch 出版社: Apress出版年: 2016-12-29页数: 804定价: USD 59 ...

  4. 猎豹CEO傅盛:95%的人碌碌无为,只是不懂这一点!

      我一直在思索,怎么才能让一家公司更快地成长?一个人怎么才能从一群人的竞争当中脱颖而出? 1. 人的四种认知状态 最近我看了一幅图,我在其上加了一个数字注脚. 这是一个人认知的四种状态——“不知道自 ...

  5. D. Vasya and Arrays

    链接 [http://codeforces.com/contest/1036/problem/D] 题意 给你两个数组长度分别为n,m; 有这么一种操作,用某个数组的某个子区间元素之和代替这个子区间, ...

  6. 作业 20181204-5 Final阶段贡献分配规则及实施

    此作业要求参见:[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2479] 小组介绍 组长:付佳 组员:张俊余 李文涛 孙赛佳 田良 于洋 ...

  7. kafka学习总结之kafka简介

    kafka是一个分布式,基于subscribe-publish的消息系统 特性:高吞吐量.低延迟.可扩展性.持久性(消息持久化到本地磁盘).可靠性.容错性(n个副本,允许n-1个节点失败).高并发(支 ...

  8. 计算机启动出现 Invalid Partition Table

    计算机启动出现 Invalid Partition Table 解决办法 使用大白菜启动盘进入临时系统,打开程序DiskGenius 如果系统盘(一般为 C 盘)非活动状态,先激活 如果装系统的硬盘不 ...

  9. Central Control Over Distributed Routing阅读笔记

    Central Control Over Distributed Routing 0.ABSTRACT1.Introduction2.Flexible Fibbing3.Augmenting Topo ...

  10. 2018软工实践—Beta冲刺(7)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Beta 冲鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调组内工作 整体软件测试 展示GitHub当日代码/文档签入记录(组 ...