简体   繁体   English

提取和转换 jdbc 接收器连接器的 kafka 消息特定字段

[英]extract and transform kafka message specific fields for jdbc sink connector

I have a kafka topic which is getting data from mysql database using Debezium mysql source connector, following is the format of one of the messages:我有一个 kafka 主题,它使用 Debezium mysql 源连接器从 mysql 数据库中获取数据,以下是其中一条消息的格式:

{
    "Message": {
        "schema": {
            "type": "struct",
            "fields": [
              ...
            ],
            "optional": true,
            "name": "mysql-server-1.inventory.somename"
        },
        "payload": {
            "op": "u",
            "ts_ms": 1465491411815,
            "before": {
                "id": 1004,
                "first_name": "Anne",
                "last_name": "Doof",
                "email": "annek@noanswer.org"
            },
            "after": {
                "id": 1004,
                "first_name": "Anne",
                "last_name": "Marry",
                "email": "annek@noanswer.org"
            },
            "source": {
                "db": "inventory",
                "table": "customers",
                ...
                "query": "Update customers set last_name = 'Marry' where id = 1004"
            }
        }
    }
}

I want to push ts_ms, before, after and id (from the object/row) columns into another database using jdbc sink connector with table schema as (id,before(text),after(text),timestamp) , being new to kafka cant figure out:我想使用 jdbc 接收器连接器将ts_ms, before, afterid (来自对象/行)列推送到另一个数据库中,表模式为(id,before(text),after(text),timestamp) ,是 kafka 的新手想不通:

  • how can i extract these fields only, from the message to push and ignore others?我如何仅从消息中提取这些字段以推送并忽略其他字段?

  • how can i transform before, after fields to string/serialize format?如何在字段之前、之后转换为字符串/序列化格式?

  • how can i extract id from object?如何从 object 中提取id (incase of insert operation, before will be null, for delete, after will be null) (在插入操作的情况下,之前将为 null,对于删除,之后将为空)

For the message above, the sink destination table should have data like this below at the end:对于上面的消息,sink 目标表最后应该有如下数据:

id:     1004
before: '{"id":1004,"first_name":"Anne","last_name":"Doof","email":"annek@noanswer.org"}'
after:  '{"id":1004,"first_name":"Anne","last_name":"Marry","email":"annek@noanswer.org"}'
timestamp: 1465491411815

You can use chain ofKafka Connect Transformations , like this solution .您可以使用Kafka Connect 转换链,就像这个解决方案一样。

You can create a DTO (Java object for your json payload which you are getting from your kafka topic) make use of this online converters helps you to convert your json to Java objects. You can create a DTO (Java object for your json payload which you are getting from your kafka topic) make use of this online converters helps you to convert your json to Java objects. [ http://pojo.sodhanalibrary.com/][1] [ http://pojo.sodhanalibrary.com/][1]

Once you receive your message from your kafka topic, you can use objectmapper to convert that json and map it into your appropriate DTO objects.Once you have the object ready.从您的 kafka 主题收到消息后,您可以使用 objectmapper 将 json 和 map 转换为适当的 DTO 对象。一旦您准备好 object。 You can make use of that object to extract the fields you want by just calling getId(),getBefore() etc..,您可以使用 object 通过调用 getId()、getBefore() 等来提取所需的字段,

Here is some reference code which helps you understand:以下是一些帮助您理解的参考代码:

    @KafkaListener(topics = "test")
        public void listen(String payload)  {

            logger.info("Message Received from Kafka topic: {}", payload);

            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

            DTOObject dtoObject = objectMapper.readValue(payload,DTOObject.class);

                logger.info("After Convertion: {}", objectMapper.writeValueAsString(dtoObject));

                logger.info("Get Before:{}", dtoObject.getId());



        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM