最近想要使用flume加拦截器对kafka数据进行处理,但是使用时发现sink中配置的topic不起作用数据sink到source中配置的topic,然后就开始了循环,写进去读出来再写进去,查了老半天查到了这篇文章详细解释了原因,感谢这位作者。总结一下就是source读取数据的时候会将source的topic写进event的header中,sink的时候会从这个header中读取topic。
这篇文章里没有详细的拦截器代码,下面附上我写的拦截器以及配置文件以供参考:
配置文件:
sync.sources = r1
sync.channels = c1
sync.sinks = k1
# interceptor
sync.sources.r1.interceptors = i1
sync.sources.r1.interceptors.i1.type = AddMarkInterceptor$Builder
# preserveExisting默认值是TRUE表示topic不改变,一定要写成false
sync.sources.r1.interceptors.i1.preserveExisting = false
# 自定义参数
# 修改header中的哪个key
sync.sources.r1.interceptors.i1.headerKey = topic
# 定义将数据发送到那个topic
sync.sources.r1.interceptors.i1.headerKeyValue = test_sink
# 数据处理相关的参数
sync.sources.r1.interceptors.i1.dataMarkName = mark
sync.sources.r1.interce