Stan Belkin
12/17/2023, 4:01 PMupsert-kafka table all rows that not exists in the unnested message
{
"type": "indicator-ignore-list",
"content": {
"indicator": "indicator01",
"ignore": {
"host": {
"in": [
"host01",
"host02"
]
},
"user": {
"in": [
"user01",
"user02",
"user03"
]
},
"service": {
"in": [
"service01",
"service02"
]
}
}
}
}
The message represents multiple ignore rules for some process. When the message received, i need to convert it into insert/delete commands for upsert-kafka table. Each row in the table is a single rule.
I have following tables
CREATE TABLE ignore_list_request (
content ROW (
`indicator` STRING,
ignore MAP<STRING, ROW (`in` ARRAYSTRING)>
),
WATERMARK FOR ts AS ts,
ts TIMESTAMP(3) METADATA FROM 'timestamp'
) WITH (
'connector' = 'kafka',
'topic' = 'indicators-ignore-list-request',
'properties.bootstrap.servers' = '${kafka_bootstrap_servers}',
'properties.group.id' = 'indicators-ignore-list',
'format' = 'json',
'scan.startup.mode' = 'latest-offset',
'json.fail-on-missing-field' = 'false',
'json.timestamp-format.standard' = 'ISO-8601'
)
CREATE TABLE ignore_list (
`key` STRING,
content ROW (
`indicator` STRING,
`object` STRING,
`value` STRING
),
ts TIMESTAMP(3) METADATA FROM 'timestamp',
WATERMARK FOR ts AS ts,
PRIMARY KEY (`key`) NOT ENFORCED
) WITH (
'connector' = 'upsert-kafka',
'topic' = 'indicators-ignore-list',
'properties.bootstrap.servers' = '${kafka_bootstrap_servers}',
'properties.group.id' = 'indicators-ignore-list',
'key.format' = 'raw',
'value.format' = 'json',
'value.fields-include'='EXCEPT_KEY',
'value.json.timestamp-format.standard' = 'ISO-8601'
)
I already managed to handle creation on new rules. But have problem with delete.
If I try
DELETE FROM ignore_list
WHERE `key` NOT IN (
SELECT
CONCAT(R1.content.`indicator`, '|', R2.content_object, '|', R3.content_value) AS row_key
FROM ignore_list_request AS R1
CROSS JOIN UNNEST(R1.content.ignore) AS R2 (content_object, content_values)
CROSS JOIN UNNEST(R2.content_values.`in`) AS R3 (content_value)
Then I get error DELETE statement is not supported for streaming mode now .
If I try to workarround it by insert rows with null message value, then when the message processed, nothing happens.
INSERT INTO ignore_list (
`key`
)
SELECT
T1.`key`
FROM ignore_list AS T1
WHERE T1.`key` NOT IN (
SELECT
CONCAT('R1.content.`indicator`, '|', R2.content_object, '|', R3.content_value) AS row_key
FROM ignore_list_request AS R1
CROSS JOIN UNNEST(R1.content.ignore) AS R2 (content_object, content_values)
CROSS JOIN UNNEST(R2.content_values.`in`) AS R3 (content_value)
)
How to make it work?