When using Kafka to import data into ByteHouse, if nested JSON is encountered in the source data, virtual columns and parsing functions can be used to help parse and import the source data. This article will provide a detailed description of the import methods for such scenarios.
Kafka table includes a virtual column (Virtual Column) _content (String). The contents of _content are the JSON strings of each row. The parsing approach is to use the JSONExtract function to extract individual columns from the complete _content string using JSON path.
{ "npc_info": { "npc": "bedok" }, "case_info": { "time_info": { "year":"2021" }, "offence": "stealing", "case_no": 123 } }
Plan to import data into the target table using the following format
Column | Target Type | Expression | Value |
|---|---|---|---|
npc | String | JSONExtract(_content, 'npc_info', 'npc', 'String') | "bedok" |
year | String | JSONExtract(_content, 'case_info', 'time_info', 'year', 'String') | "2021" |
offence | String | JSONExtract(_content, 'case_info', 'offence', 'String') | "stealing" |
case_no | Int64 | JSONExtract(_content, 'case_info', 'case_no', 'Int64') | 123 |
The usage of the JSONExtract function is consistent with the ClickHouse community
JSONExtract(json[, indices_or_keys…], Return_type) JSONExtractRaw(json[, indices_or_keys…])