簡體   English   中英

在 Kafka 中,更新 MongoDB 中的文檔時,“Key”與“Id”不匹配

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

我們正在嘗試使用 com.mongodb.kafka.connect.MongoSourceConnector 將 MongoDB 的所有記錄帶到 Kafka。 用於連接器的設置如下:

{
    "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"
    }
}

當所有文檔最初從 MongoDB 上傳到 Kafka 時,“key”對應於 Mongo 文檔中的“id”:

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

但是當 MongoDB 中的文檔更新時,帶有不同“密鑰”的更新會進入 Kafka:

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

因此,消費者無法識別最初上傳的文檔並對其進行更新。

請幫我找出 Kafka、Connector 或 MongoDB 端的哪些設置對此負責,以及如何將 Kafka 中的“密鑰”更改為與初始上傳期間相同。

我們面臨同樣的問題,經過一番搜索,我們開始使用以下配置。 我們定義了一個 avro 模式來提取 output 模式鍵。 密鑰是一致生成的,看起來像

結構{fullDocument._id=e2ce4bfe-d03a-4192-830d-895df5a4b095}

這里“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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM