Flume-Kafka-Flume对接Kafka以及Kafka数据分类传输

Flume-Kafka-Flume对接Kafka以及Kafka数据分类传输Flume对接KafkaFlume日志采集组件;Flume对接kafka主要是为了通过kafka的topic功能,动态的增加或者减少接收的节点,并且Flume要对接多个节点是需要多个channel和sink的会导致内存不够的情况。那么可以实现的场景就是Flume采集日志文件,通过kafka给多给业务线使用。1)配置flume(flume-kafka.conf)#definea1.sources=r1a1.sinks=k1a1.channels=c1#sourcea1

大家好,又见面了,我是你们的朋友全栈君。

Flume 对接 Kafka

Flume日志采集组件;Flume对接kafka主要是为了通过kafka的topic功能,动态的增加或者减少接收的节点,并且Flume要对接多个节点是需要多个channel和sink的会导致内存不够的情况。

那么可以实现的场景就是Flume采集日志文件,通过kafka给多给业务线使用。

1)配置 flume(flume-kafka.conf)

# define
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# source
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444

# sink
a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.kafka.bootstrap.servers = hadoop113:9092,hadoop114:9092,hadoop115:9092
a1.sinks.k1.kafka.topic = first
a1.sinks.k1.kafka.flumeBatchSize = 20
a1.sinks.k1.kafka.producer.acks = 1
a1.sinks.k1.kafka.producer.linger.ms = 1

# channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# bind
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
  1. 启动消费者
kafka-console-consumer.sh --zookeeper hadoop113:2181 --topic first
  1. 进入 flume 根目录下,启动 flume
bin/flume-ng agent -c conf/ -n a1 -f jobs/flume-kafka.conf

4)启动nc发送数据

[bd@hadoop113 ~]$ nc localhost 44444
hello
OK
word
OK

结果如下
[bd@hadoop113 ~]$ kafka-console-consumer.sh --zookeeper hadoop113:2181 --topic first
hello
word

Kafka数据分类

依据Kafka Sink的配置

Property Name Default Description
kafka.topic default-flume-topic The topic in Kafka to which the messages will be published. If this parameter is configured, messages will be published to this topic. If the event header contains a “topic” field, the event will be published to that topic overriding the topic configured here.

在消息头中携带了topic字段的话,该消息就会被发送到topic字段对应的topic去。

那么在flume接收到消息之后,可以通过拦截器为topic加上header,即可将其进行分类。

Flume拦截器如下:

public class JudgeTestStringInterceptor implements Interceptor { 
   

    // 声明一个存放事件的List
    private List<Event> allEvents;

    public void initialize () { 
   

        // 初始化
        allEvents = new ArrayList<Event>();
    }

    /** * 单个事件拦截 * @param event * @return */
    public Event intercept (Event event) { 
   

        // 1、获取事件中的头信息
        Map<String, String> headers = event.getHeaders();

        // 2、获取事件中的body信息
        String body = new String(event.getBody());

        // 3、根据body中是否有“test”来决定添加怎样的头信息
        // 有的话添加<topic, first>没有则添加<topic, second>
        if (body.contains("test")) { 
   
            headers.put("topic", "first");
        } else { 
   
            headers.put("topic", "second");
        }

        return event;
        // 如果返回null则认为该事件无用,将会被过滤
    }

    /** * 批量事件拦截 * @param list * @return */
    public List<Event> intercept (List<Event> list) { 
   

        // 1、清空集合
        allEvents.clear();

        // 2、遍历event
        for (Event event : list) { 
   
            // 3、给每个事件添加头信息
            allEvents.add(intercept(event));
        }

        return allEvents;
    }

    public void close () { 
   

    }

    // 定义一个Builder对象
    public static class Builder implements Interceptor.Builder { 
   

        public Interceptor build () { 
   

            return new JudgeTestStringInterceptor();
        }

        public void configure (Context context) { 
   

        }
    }
}

配置文件type-kafka.conf如下:

# define
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# source
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444

# interceptor
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = com.starnet.interceptor.JudgeTestStringInterceptor$Builder

# sink
a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.kafka.bootstrap.servers = hadoop113:9092,hadoop114:9092,hadoop115:9092
a1.sinks.k1.kafka.topic = first
a1.sinks.k1.kafka.flumeBatchSize = 20
a1.sinks.k1.kafka.producer.acks = 1
a1.sinks.k1.kafka.producer.linger.ms = 1

# channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# bind
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

启动flume,两个消费者以及nc之后结果如下:

[bd@hadoop113 ~]$ nc localhost 44444
test
OK
hello
OK
word
OK


[bd@hadoop113 ~]$ kafka-console-consumer.sh --zookeeper hadoop113:2181 --topic first
test

[bd@hadoop113 ~]$ kafka-console-consumer.sh --zookeeper hadoop113:2181 --topic second
hello
word
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/152396.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 分子模拟软件amber_分子模拟软件Discovery Studio教程(十):构建基于受体-配体复合物药效团模型…

    分子模拟软件amber_分子模拟软件Discovery Studio教程(十):构建基于受体-配体复合物药效团模型…DiscoveryStudio™(简称DS)是专业的生命科学分子模拟软件,DS目前的主要功能包括:蛋白质的表征(包括蛋白-蛋白相互作用)、同源建模、分子力学计算和分子动力学模拟、基于结构药物设计工具(包括配体-蛋白质相互作用、全新药物设计和分子对接)、基于小分子的药物设计工具(包括定量构效关系、药效团、数据库筛选、ADMET)和组合库的设计与分析等。本章节利刃君为大家带来了使用Discover…

    2022年5月26日
    40
  • Java安全之Commons Collections6分析

    Java安全之CommonsCollections6分析0x00前言其实在分析的几条链中都大致相同,都是基于前面一些链的变形,在本文的CC6链中,就和前面的有点小小的区别。在CC6链中也和CC

    2021年12月12日
    47
  • SpringBoot——解决Cache缓存同类中调用失败问题「建议收藏」

    SpringBoot——解决Cache缓存同类中调用失败问题「建议收藏」SpringBoot——解决Cache缓存同类中调用失败问题

    2022年4月23日
    36
  • tableau旭日图_Echart

    tableau旭日图_Echart效果图源代码ECharts//基于准备好的dom,初始化echarts实例varmyChart=echarts.init(document.getElementById(‘main’));varoption;option={silent:true,series:{radius:[‘15%’,’80%’],type:’sunburst’,sort:null,highligh…

    2022年9月25日
    3
  • gauss-jordan消元法求矩阵的逆_伪逆矩阵求法

    gauss-jordan消元法求矩阵的逆_伪逆矩阵求法转载来源于:http://student.zjzk.cn/course_ware/web-gcsx/gcsx/chapter3/chapter3.2.htmhttp://student.zjzk.cn/course_ware/web-gcsx/gcsx/chapter1/chapter1.2.htm#21先回顾一下高斯消元法:§1.2 消元法与矩阵的初等

    2022年8月21日
    6
  • android+制作开机动画,Android 开机动画制作详解

    android+制作开机动画,Android 开机动画制作详解Android开机动画作为一个经常被DIY固件的玩家熟悉的内容,我这里简单做个总结。一、Android的2种类型:原生动画和第三方动画。1.原生动画Android标准代码是带有一个开机动画的,就是我们常见的带有滚动阴影的Android字样,如下图为标准Android原生动画:2.第三方动画:Android支持客户自定义第三方动画,我们只要将做好的动画文件,命名为…

    2022年5月14日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号