Skip to content

AWS DMS object-mapping to Kafka topic using map-record-to-record set topic key

0

Currently we use the following configuration to map the table from SQL server to a Kafka topic using AWS DMS:

{
  "rule-type": "selection",
  "rule-id": "1",
  "rule-name": "dbo-tablename",
  "rule-action": "include",
  "object-locator": {
	"schema-name": "dbo",
	"table-name": "tablename"
  }
},
{
  "rule-type": "object-mapping",
  "rule-id": "2",
  "rule-name": "dbo-tablename-topic-map",
  "rule-action": "map-record-to-record",
  "kafka-target-topic": "sql-sourcedb.dbo.tablename",
  "object-locator": {
	"schema-name": "dbo",
	"table-name": "tablename"
  },
  "mapping-parameters": {
	"attribute-mappings": [
	  {
		"target-attribute-name": "source",
		"attribute-type": "scalar",
		"attribute-sub-type": "string",
		"value": "XX"
	  }
	]
  }
}

This configuration sets the Kafka message key to sql-sourcedb.dbo.tablname-1234 where 1234 is the primary key value of the table.

What I would like the key to be is XX-1234 where XX is source identifier. I see I can use partition-key-type and partition-key-name or a partition-key.

With the first two would it be possible to get this?

"partition-key-type": "attribute-name",
"partition-key-name": "XX-pkfield"

Or if using partition-key , could I get the pkfield value into the constant?

"partition-key": {"value": "XX-${pkfieldvalue}" }

Or if there is a different way so I end up with the combination of XX-pkfield_value.

Thank you

2 Answers
0

When using AWS DMS to migrate data to a Kafka topic, you can customize the partition key to achieve your desired format of "XX-1234" where XX is your source identifier and 1234 is the primary key value.

The partition-key-type and partition-key-name approach won't directly give you the combined format you want. These settings would allow you to use either a specific attribute name as the partition key or a schema/table name, but not a combination with a constant value.

For your specific requirement, you would need to use attribute mapping to create a new attribute that combines your source identifier with the primary key value, and then use that attribute as your partition key. Here's how you could approach it:

  1. First, create an attribute mapping that combines your source identifier with the primary key:
"mapping-parameters": {
"attribute-mappings": [
{
"target-attribute-name": "custom_key",
"attribute-type": "scalar",
"attribute-sub-type": "string",
"value": "${pkfield}"
},
{
"target-attribute-name": "source",
"attribute-type": "scalar",
"attribute-sub-type": "string",
"value": "XX"
}
]
}
  1. Then use this new attribute as your partition key:
"partition-key-type": "attribute-name",
"partition-key-name": "custom_key"

Unfortunately, AWS DMS doesn't support string concatenation directly in the attribute mapping expressions, so you can't create "XX-${pkfield}" in a single step. The approach above will get you the primary key value as the partition key, but not with the "XX-" prefix combined in the way you want.

If having this exact format is critical, you might need to consider processing the messages after they arrive in Kafka to modify the keys, or explore if a custom transformation script within AWS DMS could help achieve this specific formatting requirement.
Sources
Using Apache Kafka as a target for AWS Database Migration Service - AWS Database Migration Service
Using Amazon Kinesis Data Streams as a target for AWS Database Migration Service - AWS Database Migration Service
KafkaSettings - AWS Database Migration Service

answered a year ago

0

DMS doesn’t support concatenating static strings with column values inside the partition-key or partition-key-name fields. The partition-key-type can only reference an existing column (attribute-name) or use a fixed constant (literal), but it can’t perform dynamic string interpolation such as "XX-${pkfieldvalue}". The kafka-target-key is automatically derived from the table’s primary key unless explicitly overridden, and any prefix or composite key logic must be handled before the record reaches Kafka. If you need the Kafka message key to be XX-<pkvalue>, add a derived column in the mapping using map-record-to-record to build that value. —Taz

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.