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. 洛咕 P4474 王者之剑

    宝石只能在偶数秒取到,假设有一个宝石在奇数秒取到了,那么上一秒是偶数秒,在上一秒的时候这里的宝石就没了. 相邻的两个宝石不能同时取,很显然,先取一块,那么这是偶数秒,取完了这一块之后相邻的都没了. 只 ...

  2. tornado学习笔记

    一.UIMOTHODS: 1.在项目目录创建uimothods.py文件(名称可以任意)内容: def test2(self): return ('hello uimothods') 2.tornad ...

  3. Altium CAED 国际认证操作题例题(含下载)

    官网介绍页面 https://www.altium.com.cn/certification 共五套操作题 含资料 蓝奏云:https://www.lanzous.com/i2lj1ng 百度网盘:h ...

  4. [翻译]:Artificial Intelligence for games 5.3 STATE MACHINES:状态机

    目录 Chapter 5 Decision Making 5.3 STATE MACHINES:状态机 Chapter 5 Decision Making 5.3 STATE MACHINES:状态机 ...

  5. PAT-1004 Counting Leaves

    1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...

  6. python-两个图片相似度算法

    # -*- coding: UTF-8 -*- """ 作者:zxj 版本:1.0 日期:19-3-24 """ import cv2 im ...

  7. B. Lost Array

    链接 [http://codeforces.com/contest/1043/problem/B] 题意 自己点开链接看 分析 1到n枚举某个值,判断是否满足并统计 判断方法:假设x序列成立,那么后面 ...

  8. Final互评------《I do》---- 二次元梦之队

    一.基于NABCD评论作品,及改进建议 1.根据(不限于)NABCD评论作品的选题; N(Need,需求):该产品是一款休闲类的解密游戏,背景是编程知识.作为一款休闲游戏,有着基本的娱乐功能,可以给用 ...

  9. Linux内核分析——第六周学习笔记

    进程的描述和进程的创建 前言:以下笔记除了一些讲解视频中的概念记录,图示.图示中的补充文字.总结.分析.小结部分均是个人理解.如有错误观点,请多指教! PS.实验操作会在提交到MOOC网站的博客中写.

  10. LeetCode-----算法448.找到所有数组中消失的数字

    题目: 给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. ...