2

Here is a json format message in kafka.

{
    "@timestamp": "2021-08-14T11:11:00.301Z",
    "@metadata": {
        "beat": "filebeat",
        "type": "_doc",
        "version": "7.14.0"
    },
    "message": {
        "k1": "v1",
        "k2": "v2"
    }
}

Then, i use flink talbe to consume data from kafka.

CREATE TABLE table_1
(
    `message` ROW(k1 STRING, k2 STRING)
)
WITH (
    'connector' = 'kafka',
    'topic' = 'topic1',
    'json.ignore-parse-errors' = 'true',
    'properties.bootstrap.servers' = '127.0.0.1:9092',
    'properties.group.id' = 'groupid1',
    'scan.startup.mode' = 'earliest-offset',
    'format' = 'json'
);

select message.k1, message.k2 from table_v1 LIMIT 10;

The source message is produced by filebeat(output.kafka).Is it posiable to extract the value of message directly in flink table? Such as:

CREATE TABLE table_2
(
    `k1` STRING,
    `k2` STRING
)
WITH (
    'connector' = 'kafka',
    'topic' = 'topic1',
    'json.ignore-parse-errors' = 'true',
    'properties.bootstrap.servers' = '127.0.0.1:9092',
    'properties.group.id' = 'groupid1',
    'scan.startup.mode' = 'earliest-offset',
    'format' = 'json'
);

select k1, k2 from table_v2 LIMIT 10;
king
  • 300
  • 1
  • 13

1 Answers1

0

If you want this

CREATE TABLE table_2
(
    `k1` STRING,
    `k2` STRING
)
WITH (
    'connector' = 'kafka',

The input topic data should look like this

{"k1": "v1", "k2": "v2"}

i.e. you need to "flatten" the records that filebeat creates

Or you might be able to do CREATE TABLE table_2 AS SELECT message.k1, message.k2 FROM table_v1

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245