简体   繁体   English

从数组到 Kafka 主题的值

[英]Values from array to Kafka Topic

I use Kafka Connect with Mongo as a source.我使用带有 Mongo 的 Kafka Connect 作为源。 In my case I need send to consumer data in one row.就我而言,我需要将数据连续发送给消费者。 For example - I have collection like this:例如 - 我有这样的集合:

{
    "_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",
                }
            ]
        }
    ]
}

And I configured my Connector with我配置了我的连接器

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"

In output.schema.value I read fields that I need.在 output.schema.value 中,我读取了我需要的字段。 Is it possible to get data in topic as records for each key?是否可以将主题中的数据作为每个键的记录来获取? For example values for this fields: 11 11aa11 FirstName LastName +4912222222 But now I get client_id, device_id and array with contacts.例如,此字段的值:11 11aa11 FirstName LastName +4912222222 但现在我得到了 client_id、device_id 和带有联系人的数组。 Thanks for attention!感谢关注!

I'm not sure I understand the question.我不确定我是否理解这个问题。 You've got one record "_id" .你有一个记录"_id" The Mongo connector doesn't parse anything more than that to "drill down" into specific fields, let alone get only the keys/values within an array. Mongo 连接器不会解析更多内容以“深入”到特定字段,更不用说只获取数组中的键/值。 That being said, your output value schema should match the collection and include an array type.话虽如此,您的 output 值模式应该与集合匹配并包含数组类型。 Worth pointing out that dots are not valid in Avro field names值得指出的是,点在 Avro 字段名称中无效

If you want to flatten the array into multiple contact objects, consume the Connect data on your own, and write out data to another topic, using Kafka Streams for example如果要将数组扁平化为多个联系人对象,请自行使用 Connect 数据,并将数据写入另一个主题,例如使用 Kafka Streams

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

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