简体   繁体   English

MongoDB Kafka 连接器未生成具有 Mongo 文档 ID 的消息密钥

[英]MongoDB Kafka Connector not generating the message key with the Mongo document id

I'm using the beta release of the MongoDB Kafka Connector to publish from MongoDB to a Kafka topic.我正在使用MongoDB Kafka 连接器的测试版从 MongoDB 发布到 Kafka 主题。

Messages are generated into Kafka but their key is null when it should be the document id:消息被生成到 Kafka 中,但当它应该是文档 id 时,它们的键是空的:

在此处输入图片说明

This is my connect standalone config :这是我的连接独立配置

bootstrap.servers=xxx:9092

# The converters specify the format of data in Kafka and how to translate it into Connect data. Every Connect user will
# need to configure these based on the format they want their data in when loaded from or stored into Kafka
key.converter=org.apache.kafka.connect.json.JsonConverter
value.converter=org.apache.kafka.connect.json.JsonConverter
# Converter-specific settings can be passed in by prefixing the Converter's setting with the converter you want to apply
# it to
key.converter.schemas.enable=false
value.converter.schemas.enable=false

# The internal converter used for offsets and config data is configurable and must be specified, but most users will
# always want to use the built-in default. Offset and config data is never visible outside of Kafka Connect in this format.
internal.key.converter=org.apache.kafka.connect.json.JsonConverter
internal.value.converter=org.apache.kafka.connect.json.JsonConverter
internal.key.converter.schemas.enable=false
internal.value.converter.schemas.enable=false

And the mongodb source properties :mongodb 源属性

name=mongo-source
connector.class=com.mongodb.kafka.connect.MongoSourceConnector
tasks.max=1

# Connection and source configuration
connection.uri=mongodb+srv://xxx
database=mydb
collection=mycollection

topic.prefix=someprefix
poll.max.batch.size=1000
poll.await.time.ms=5000

# Change stream options
pipeline=[]
batch.size=0
change.stream.full.document=updateLookup
collation=

Below there's an example of a message String value:下面是消息字符串值的示例:

"{\"_id\": {\"_data\": \"xxx\"}, \"operationType\": \"replace\", \"clusterTime\": {\"$timestamp\": {\"t\": 1564140389, \"i\": 1}}, \"fullDocument\": {\"_id\": \"5\", \"name\": \"Some Client\", \"clientId\": \"someclient\", \"clientSecret\": \"1234\", \"whiteListedIps\": [], \"enabled\": true, \"_class\": \"myproject.Client\"}, \"ns\": {\"db\": \"mydb\", \"coll\": \"mycollection\"}, \"documentKey\": {\"_id\": \"5\"}}"

I tried using a transform to extract if from the value, specifically from the documentKey field:我尝试使用转换从值中提取 if,特别是从 documentKey 字段:

transforms=InsertKey
transforms.InsertKey.type=org.apache.kafka.connect.transforms.ValueToKey
transforms.InsertKey.fields=documentKey

But got an exception:但有一个例外:

Caused by: org.apache.kafka.connect.errors.DataException: Only Struct objects supported for [copying fields from value to key], found: java.lang.String
    at org.apache.kafka.connect.transforms.util.Requirements.requireStruct(Requirements.java:52)
    at org.apache.kafka.connect.transforms.ValueToKey.applyWithSchema(ValueToKey.java:79)
    at org.apache.kafka.connect.transforms.ValueToKey.apply(ValueToKey.java:65)

Any ideas to generate a key with the document id?有什么想法可以用文档 ID 生成密钥吗?

According to exception, that is thrown:根据异常,抛出:

Caused by: org.apache.kafka.connect.errors.DataException: Only Struct objects supported for [copying fields from value to key], found: java.lang.String
    at org.apache.kafka.connect.transforms.util.Requirements.requireStruct(Requirements.java:52)
    at org.apache.kafka.connect.transforms.ValueToKey.applyWithSchema(ValueToKey.java:79)
    at org.apache.kafka.connect.transforms.ValueToKey.apply(ValueToKey.java:65)

Unfortunately Mongo DB connector , that you use, it doesn't create properly schema .不幸的是,您使用的Mongo DB 连接器没有正确创建架构

Above connector create Record with key and value schema's as String .上面的连接器使用键和值模式创建 Record 为String Check this line:: How record is created by connector .检查这一行: 连接器如何创建记录 That is the reason why you can't apply Transformation to it这就是你不能对它应用 Transformation 的原因

这应该在 1.3.0 版中得到支持: https : //jira.mongodb.org/browse/KAFKA-40

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

相关问题 mongo db sink 连接器,kafka 消息密钥到 mongodb 文档字段 - mongo db sink connector, kafka message key to mongodb document field 在 Kafka 中,更新 MongoDB 中的文档时,“Key”与“Id”不匹配 - In Kafka, the "Key" does not match the "Id" when updating the document in MongoDB Kafka Mongodb 接收器连接器 - 更新文档 - Kafka Mongodb sink connector - update document MongoDB 接收器连接器:消息在 Apache Kafka 中被截断 - MongoDB Sink Connector : Message truncated in Apache Kafka kafka 连接器 debezium mongodb CDC 更新/$set 消息没有过滤器(_id 值) - kafka connector debezium mongodb CDC update/$set message without filter(_id value) 如何使用 Debezium MongoDB 源连接器将 JSON 值转换为 Kafka 消息密钥? - How to transform JSON value to Kafka message key with Debezium MongoDB Source Connector? 如何根据 kafka 主题名称或消息键/值使用 mongodb 接收器连接器对不同 dbs 和 collections 中的 kafka 主题进行分组 - How to group kafka topics in different dbs and collections with mongodb sink connector depending on kafka topic name or message key/value MONGODB KAFKA Source CONNECTOR 如何更改 Kafka 分区键 - MONGODB KAFKA Source CONNECTOR How to change Kafka Parition key Kafka 消息包含控制字符(MongoDB 源连接器) - Kafka message includes control characters (MongoDB Source Connector) Debezium Kafka连接器mongodb - Debezium Kafka connector mongodb
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM