1 問題背景
Flume向kafka發布數據時,發現kafka接收到的數據總是在一個partition中,而我們希望發布來的數據在所有的partition平均分布
2 解決辦法
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是從header里的key參數來確定將數據發到kafka的哪個分區中。如果為null,那么就會隨機發布至分區中。但我測試的結果是flume發布的數據會發布到一個分區中的。
所以,我們需要向header中寫上隨機的key,然后數據才會真正的向kafka分區進行隨機發布。
我們的辦法是,向flume添加攔截器,官方文檔說有一個UUID Interceptor,會為每個event的head添加一個隨機唯一的key。其實我們直接用這個即可。
在flume添加的配置文件如下:
hiveview.sources.tailSource.interceptors = i2
hiveview.sources.tailSource.interceptors.i2.type=org.apache.flume.sink.solr.morphline.UUIDInterceptor$Builder
hiveview.sources.tailSource.interceptors.i2.headerName=key
hiveview.sources.tailSource.interceptors.i2.preserveExisting=false