简体   繁体   English

在 Kafka 中,更新 MongoDB 中的文档时,“Key”与“Id”不匹配

[英]In Kafka, the "Key" does not match the "Id" when updating the document in MongoDB

We are trying to take all the records from MongoDB to Kafka using the com.mongodb.kafka.connect.MongoSourceConnector.我们正在尝试使用 com.mongodb.kafka.connect.MongoSourceConnector 将 MongoDB 的所有记录带到 Kafka。 The settings are used for connector as follows:用于连接器的设置如下:

{
    "name": "mongo-source",
    "config": {
        "connector.class": "com.mongodb.kafka.connect.MongoSourceConnector",
        "connection.uri": "mongodb:***:27017/?authSource=admin&replicaSet=myMongoCluster&authMechanism=SCRAM-SHA-256",
        "database": "someDb",
        "collection": "someCollection",
        "output.format.value":"json",
        "output.format.key":"json",
        "key.converter.schemas.enable":"false",
        "value.converter.schemas.enable":"false",
        "key.converter":"org.apache.kafka.connect.storage.StringConverter",
        "value.converter":"org.apache.kafka.connect.storage.StringConverter",
        "publish.full.document.only": "true",
        "change.stream.full.document":"updateLookup",
        "copy.existing": "true"
    }
}

When all documents are initially uploaded from MongoDB to Kafka, the "key" corresponds to the "id" from Mongo document:当所有文档最初从 MongoDB 上传到 Kafka 时,“key”对应于 Mongo 文档中的“id”:

{"_id": {"_id": {"$oid": "5e54fd0fbb5b5a7d35737232"}, "copyingData": true}}

But when a document in MongoDB is updated, an update with a different "key" gets into Kafka:但是当 MongoDB 中的文档更新时,带有不同“密钥”的更新会进入 Kafka:

{"_id": {"_data": "82627B2EF6000000022B022C0100296E5A1004A47945EC361D42A083988C14D982069C46645F696400645F0FED2B3A35686E505F5ECA0004"}}

Thus, the consumer cannot identify the initially uploaded document and update for it.因此,消费者无法识别最初上传的文档并对其进行更新。

Please help me find which settings on the Kafka, Connector or MongoDB side are responsible for this and how I can change the "Key" in Kafka to the same as during the initial upload.请帮我找出 Kafka、Connector 或 MongoDB 端的哪些设置对此负责,以及如何将 Kafka 中的“密钥”更改为与初始上传期间相同。

We were facing the same issue and after some search we started using the following config.我们面临同样的问题,经过一番搜索,我们开始使用以下配置。 We defined a avro schema to extract the output schema key.我们定义了一个 avro 模式来提取 output 模式键。 The key is generated consistently and looks like密钥是一致生成的,看起来像

Struct{fullDocument._id=e2ce4bfe-d03a-4192-830d-895df5a4b095}结构{fullDocument._id=e2ce4bfe-d03a-4192-830d-895df5a4b095}

Here "e2ce4bfe-d03a-4192-830d-895df5a4b095" is the document id.这里“e2ce4bfe-d03a-4192-830d-895df5a4b095”是文档ID。

{
  "change.stream.full.document" : "updateLookup",
  "connection.uri" : "<connection_uri>",
  "connector.class" : "com.mongodb.kafka.connect.MongoSourceConnector",
  "collection": "someCollection",
  "copy.existing" : "true",
  "database" : "someDb",
  "key.converter" : "org.apache.kafka.connect.storage.StringConverter",
  "key.converter.schemas.enable" : "false",
  "key.serializer" : "org.apache.kafka.connect.storage.StringConverter",
  "name" : "mongo-source",
  "output.format.key" : "schema",
  "output.json.formatter" : "com.mongodb.kafka.connect.source.json.formatter.SimplifiedJson"
  "publish.full.document.only" : "true",
  "output.schema.key": "{\"type\":\"record\",\"name\":\"keySchema\",\"fields\":[{\"name\":\"fullDocument._id\",\"type\":\"string\"}]}",      
  "tasks.max" : "1",
  "value.converter" : "org.apache.kafka.connect.storage.StringConverter",
  "value.converter.schemas.enable" : "false"
}  

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

相关问题 MongoDB Kafka 连接器未生成具有 Mongo 文档 ID 的消息密钥 - MongoDB Kafka Connector not generating the message key with the Mongo document id MongoDB-更新时文档大小是否重要-将项目推送到数组? - MongoDB - does document size matter when updating - pushing item to array? 如何在更新MongoDB文档后获取_id? - How to get _id after updating a MongoDB document? 更新 MongoDB 集合文档中的嵌套键值 - Updating a nested key value in a MongoDB collection document 当$ match键不在索引中时,mongodb聚合非常慢(tablescan吗?) - mongodb aggregation very slow when $match key not in index (does tablescan?) 仅按 mongodb 中的值匹配并返回文档的 id - Match only by values in mongodb and return id of the document 在MongoDB中,当文档具有嵌入式文档时,为什么更新/查找精确文档失败? - In MongoDB, why does updating/finding exact documents fail when the document has an embedded document? 如何使用MongoDB的&#39;$ match&#39;聚合运算符与嵌入式文档的ID匹配? - How to use '$match' Aggregation Operators of MongoDB to match with the id of embedded document? mongo db sink 连接器,kafka 消息密钥到 mongodb 文档字段 - mongo db sink connector, kafka message key to mongodb document field 更新文档时延迟(MongoDB/Mongoose) - Delay when updating document (MongoDB/Mongoose)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM