camel-kafka-connector icon indicating copy to clipboard operation
camel-kafka-connector copied to clipboard

Possible to use Kafka keys and headers in db inserts?

Open reidmeyer opened this issue 1 year ago • 2 comments

All of the examples for db sinks are like:

INSERT INTO accounts (username,city) VALUES (:#username,:#city)

where the username and city are key names in the json message from the kafka.

Is it possible to insert the kafka key or a kafka header (which is also json)?

reidmeyer avatar Jun 21 '24 13:06 reidmeyer

You can use camel simple language something ala :#${header.kafka.KEY}

davsclaus avatar Jul 08 '24 13:07 davsclaus

HI @davsclaus,

Perhaps I'm missing something super obvious, but I'm struggling to get it to work.

I have a kafka message that:

has value:

{
	"message": "hello there reid"
}

has kafka headers:

{
	"myheader": "myheadervalue"
}

and has a key:

"some-key"

and I have a sql insert like:

camel.kamelet.postgresql-sink.query: 'INSERT INTO mytable (message,other) VALUES (:#message,:#${header.kafka.KEY})'

this doesn't inject anything into header.kafka.KEY, weirdly.

I'm trying to do something like:

camel.kamelet.postgresql-sink.query: 'INSERT INTO mytable (message,mykey,myheader) VALUES (:#message,:#${kafka.key},:#${kafka.header.myheader})', but no luck yet.

Do you know the solution here?

reidmeyer avatar Jul 09 '24 11:07 reidmeyer