簡體   English   中英

從數組到 Kafka 主題的值

[英]Values from array to Kafka Topic

我使用帶有 Mongo 的 Kafka Connect 作為源。 就我而言,我需要將數據連續發送給消費者。 例如 - 我有這樣的集合:

{
    "_id" : "sdasd",
    "client_id" : "11",
    "device_id" : "11aa11",
    "contacts" : [ 
        {
            "contact_id" : "1",
            "contact_name" : "FirstName LastName",
            "contact_numbers" : [ 
                {
                    "contact_num" : "+4912222222",
                }
            ]
        },
        {
            "contact_id" : "2",
            "contact_name" : "FirstName2 LastName2",
            "contact_numbers" : [ 
                {
                    "contact_num" : "+4911111111",
                }
            ]
        }
    ]
}

我配置了我的連接器

curl -X POST -H "Content-Type: application/json" --data '
  {"name": "mongo-source-contacts",
   "config": {
     "tasks.max":"1",
     "connector.class":"com.mongodb.kafka.connect.MongoSourceConnector",
     "output.format.value":"schema",
     "output.schema.value":"{\"name\":\"MongoExchangeSchema\",\"type\":\"record\",\"namespace\":\"com.mongoexchange.avro\",\"fields\":[{\"name\": \"client_id\",\"type\": \"string\"},{\"name\": \"device_id\",\"type\": \"string\"}, {\"name\": \"contacts.contact_name\",\"type\": \"string\"}]}",
     "schema.compatibility": "NONE",
     "key.converter":"org.apache.kafka.connect.storage.StringConverter",
     "value.converter":"io.confluent.connect.avro.AvroConverter",
     "value.converter.schema.registry.url":"http://localhost:8081",
     "connection.uri":"mongodb://localhost:27017/replicaSet=globaldb",
     "publish.full.document.only": true,
     "topic.prefix":"t_cb",
     "topic.creation.default.partitions"        : 4,
     "topic.creation.default.replication.factor": 1,
     "database":"testdb",
     "collection":"contactbook_test"
}}' http://localhost:8083/connectors -w "\n"

在 output.schema.value 中,我讀取了我需要的字段。 是否可以將主題中的數據作為每個鍵的記錄來獲取? 例如,此字段的值:11 11aa11 FirstName LastName +4912222222 但現在我得到了 client_id、device_id 和帶有聯系人的數組。 感謝關注!

我不確定我是否理解這個問題。 你有一個記錄"_id" Mongo 連接器不會解析更多內容以“深入”到特定字段,更不用說只獲取數組中的鍵/值。 話雖如此,您的 output 值模式應該與集合匹配並包含數組類型。 值得指出的是,點在 Avro 字段名稱中無效

如果要將數組扁平化為多個聯系人對象,請自行使用 Connect 數據,並將數據寫入另一個主題,例如使用 Kafka Streams

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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