Closed clemensk1 closed 3 years ago
The connector already uses the Kafka key to determine the primary key for the Scylla table. However, the connector requires that those fields in the key of Kafka message must also appear in the value of Kafka message. For example this would work correctly:
Kafka key:
{ "id": 1 }
Kafka value:
{
"id": 1,
"department": "foo",
"name": "bar"
}
We will consider relaxing this requirement of the connector in the future, so it would work on your example without problems.
However, you can use Kafka SMT (Single Message Transform) to transform your messages (on the fly, the original topic is not affected) into the required format. Unfortunately, it will require a few transforms and installing additional transform:
First, we will use KeyToValueTransform
, available here: https://github.com/jzaralim/KeyToValueTransform (installation is mvn package
and copying JAR file to classpath). This transform will copy key to value as follows:
{
"rowkey": {"id": 1},
"department": "foo",
"name": "bar"
}
Next, we will use Flatten$Value
transform, so that id
is not nested in the value:
{
"rowkey.id": 1,
"department": "foo",
"name": "bar"
}
Finally, using ReplaceField$Value
, we will rename rowkey.id
to id
:
{
"id": 1,
"department": "foo",
"name": "bar"
}
This is what has to be added to the configuration:
transforms = keyToValue, flatten, rename
transforms.keyToValue.type = com.github.jzaralim.kafka.connect.transform.keytovalue.KeyToValueTransform
transforms.flatten.type = org.apache.kafka.connect.transforms.Flatten$Value
transforms.rename.type = org.apache.kafka.connect.transforms.ReplaceField$Value
transforms.rename.renames = rowkey.id:id
Thank you very much for this detailed answer, I'll give it a try :)
Hello @clemensk1 ,
I have managed to solve the issue by using columns mapping.
Hi,
how can I use the kafka message key as primary key in a table?
example:
kafka message key:
kafka message value:
desired table:
Error message:
my connector config:
I use avro schema for both the key and the value of the topic.
Many Thanks!