Flume到Kafka且均分到多个partition

@Author  : Spinach | GHB
@Link    : http://blog.csdn.net/bocai8058

说明情况

Flume向kafka发布数据时,发现kafka接收到的数据总是在一个partition中,而我们希望发布来的数据在所有的partition平均分布。

应该怎么做呢?

解决方法

Flume的官方文档是这么说的:

Kafka Sink uses the topic and key properties from the FlumeEvent headers to send events to Kafka. If topic exists in the headers, the event will be sent to that specific topic, overriding the topic configured for the Sink. If key exists in the headers, the key will used by Kafka to partition the data between the topic partitions. Events with same key will be sent to the same partition. If the key is null, events will be sent to random partitions.

(Kafka Sink使用FlumeEvent标头中的主题和关键属性将事件发送到Kafka。如果标题中存在主题,则会将事件发送到该特定主题,从而覆盖为Sink配置的主题。如果标头中存在密钥,则Kafka将使用密钥对主题分区之间的数据进行分区。具有相同密钥的事件将发送到同一分区。如果密钥为空,则将事件发送到随机分区。)

从上文中的意思,我们可以看出:kafka-sink是从header里的key参数来确定将数据发到kafka的哪个分区中。如果为null,那么就会随机发布至分区中。

但我测试的结果是flume发布的数据会发布到一个分区中的。所以,我们需要向header中写上随机的key,然后数据才会真正的向kafka分区进行随机发布。

我们的办法是,向flume添加拦截器,官方文档说有一个UUID Interceptor,会为每个event的head添加一个随机唯一的key。其实我们直接用这个即可。

# 在flume添加的配置文件如下:
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type=org.apache.flume.sink.solr.morphline.UUIDInterceptor$Builder
a1.sources.r1.interceptors.i1.headerName=key
a1.sources.r1.interceptors.i1.preserveExisting=false

引用:https://www.cnblogs.com/hark0623/p/4710804.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小学僧来啦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值