I use Elasticsearch Sink connector for stream data from kafka to elasticsearch, and I have next question.
I have next structure in kafka topic document
Partition : 0
Offset: 0
Key:
Value:
{
"attributes": {
"3": "Mike"
}
}
Timestamp: 2022-11-03 19:03:34.866
For this data I have next index template in my elasticsearch
{
"version": 1,
"index_patterns": [
"documents-*"
],
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"cashier": {
"type": "text"
}
}
}
}
And I have next configuration Elastcisearch Sink Connector
{
"name": "elasticsearch-sink",
"config": {
"connector.class": "io.confluent.connect.elasticsearch.ElasticsearchSinkConnector",
"tasks.max": "1",
"topics": "document, document-processing-error",
"key.ignore": "true",
"schema.ignore": "true",
"connection.url": "http://elasticsearch:9200",
"type.name": "_doc",
"name": "elasticsearch-sink",
"key.converter": "org.apache.kafka.connect.storage.StringConverter",
"value.converter": "org.apache.kafka.connect.json.JsonConverter",
"value.converter.schemas.enable": "false",
"flush.synchronously": "true",
"transforms": "appendTimestampToIX",
"transforms.appendTimestampToIX.type": "org.apache.kafka.connect.transforms.TimestampRouter",
"transforms.appendTimestampToIX.topic.format": "${topic}-${timestamp}",
"transforms.appendTimestampToIX.timestamp.format": "yyyy-MM-dd"
}
}
In the output I have next data in my index document-2022-11-03
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "document-2022-11-03",
"_type": "_doc",
"_id": "document-2022-11-03 0 0",
"_score": 1.0,
"_source": {
"attributes": {
"3": "Mike"
}
}
}
]
}
}
This works fine, but I need extra transformation for my data, for example if in attribute I have key 3
, I need to replace this field and add key cashier
and mutate this structure to flat JSON with random id for document, so, in the end output I need next structure (for example)
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "document-2022-11-03",
"_type": "_doc",
"_id": "134DaBfWAE6AZUyKUAbjRksjXHTmP6hDxedGm4YhBnZW",
"_score": 1.0,
"_source": {
"cashier": "Mike"
}
}
]
}
}
I tired use next config for replace field but this doesn't work for me
"transforms": "RenameField",
"transforms.RenameField.type": "org.apache.kafka.connect.transforms.ReplaceField$Value",
"transforms.RenameField.renames": "arrtubites.3:cashier"
How can I do this?
CodePudding user response:
ReplaceField
transform does not work with nested attributes such as Maps or Objects, only top-level fields of either.
If you want to convert
{
"attributes": {
"3": "Mike"
}
}
Into
{
"cashier": "Mike"
}
Then, Kafka Streams or ksqlDB are the common recommendations (aka consume elsewhere, and produce to a new topic with the logic that you want to perform).
Logstash may also be an option instead of that Kafka Connect.