需求背景
从某一个kafka集群A中消费一条消息,并将该消息的中的字段分别发送给另外一个kafka集群B。
举例
某一条消息A的样式为
{"a":58,"b":100.0,"c":7.58,"ip":"192.168.1.1"}
想要拿到这消息的时候,将数据提炼为三条消息,如
{"a":58,"ip":"192.168.1.1"}
{"b":100.0,"ip":"192.168.1.1"}
{"c":7.58,"ip":"192.168.1.1"}
参考文章
准备工作
logstash安装包
此处我用的是logstash-6.6.0
配置:
1、文件config/pipelines.yml
追加以下内容,其中path.config配置项替换为生产环境路径,四个配置文件是新增的配置,默认是没有的
- pipeline.id: metric1
path.config: "/usr/local/logstash-6.6.0/bin/test/metric1.config"
- pipeline.id: metric2
path.config: "/usr/local/logstash-6.6.0/bin/test/metric2.config"
- pipeline.id: metric3
path.config: "/usr/local/logstash-6.6.0/bin/test/metric3.config"
2、准备配置文件,根据1中配置的path.config文件,写入以下配置内容,注意四个文件中的topic一致,group_id不一致。
注意:要创建三个metric文件
/usr/local/logstash-6.6.0/bin/test/metric1.config
input {
kafka {
bootstrap_servers => "192.168.21.127:9092"
group_id => "consumer-group-01"
topics => ["test"]
consumer_threads => 1
decorate_events => false
auto_offset_reset => "earliest"
poll_timeout_ms => "1"
session_timeout_ms => "60000"
request_timeout_ms => "90000"
codec => "json"
}
}
filter {
mutate {
remove_field => [ "a" ]
remove_field => [ "b" ]
}
}
output { stdout { codec => rubydebug } }
3、启动logstash,不要加-f参数指定具体的配置文件了,否则指定的pipelines.yml文件不生效
4、测试完成之后修改每个config文件中的outpu内容块替换为输出到kafka
output{
kafka {
bootstrap_servers => "192.168.183.195:9092,192.168.183.194:9092,192.168.183.196:9092"
codec => json
topic_id => "testtopic"
}
}