跳到主要内容
跳到主要内容

Regexp

输入输出别名

描述

Regex 格式会根据提供的正则表达式,对导入数据的每一行进行解析。

用法

来自 format_regexp 设置的正则表达式会应用到导入数据的每一行。正则表达式中的子模式数量必须等于导入数据集中列的数量。

导入数据的各行必须使用换行符 '\n' 或 DOS 风格的换行符 "\r\n" 分隔。

每个匹配到的子模式内容会根据 format_regexp_escaping_rule 设置,使用对应数据类型的解析方法进行解析。

如果正则表达式未能匹配某一行,并且 format_regexp_skip_unmatched 被设置为 1,则该行会被静默跳过。否则会抛出异常。

示例用法

假设有文件 data.tsv

id: 1 array: [1,2,3] string: str1 date: 2020-01-01
id: 2 array: [1,2,3] string: str2 date: 2020-01-02
id: 3 array: [1,2,3] string: str3 date: 2020-01-03

以及 imp_regex_table 表:

CREATE TABLE imp_regex_table (id UInt32, array Array(UInt32), string String, date Date) ENGINE = Memory;

我们将使用以下查询,把前面提到的文件中的数据插入到上述表中:

$ cat data.tsv | clickhouse-client  --query "INSERT INTO imp_regex_table SETTINGS format_regexp='id: (.+?) array: (.+?) string: (.+?) date: (.+?)', format_regexp_escaping_rule='Escaped', format_regexp_skip_unmatched=0 FORMAT Regexp;"

现在我们可以对表执行 SELECT 查询,查看 Regex 格式是如何解析文件中的数据的:

SELECT * FROM imp_regex_table;
┌─id─┬─array───┬─string─┬───────date─┐
│  1 │ [1,2,3] │ str1   │ 2020-01-01 │
│  2 │ [1,2,3] │ str2   │ 2020-01-02 │
│  3 │ [1,2,3] │ str3   │ 2020-01-03 │
└────┴─────────┴────────┴────────────┘

格式设置

在使用 Regexp 格式时,可以使用以下设置:

  • format_regexpString。包含以 re2 语法编写的正则表达式。

  • format_regexp_escaping_ruleString。支持以下转义规则:

    • CSV(类似于 CSV
    • JSON(类似于 JSONEachRow
    • Escaped(类似于 TSV
    • Quoted(类似于 Values
    • Raw(按整体提取子模式,不应用任何转义规则,类似于 TSVRaw
  • format_regexp_skip_unmatchedUInt8。用于指定当 format_regexp 表达式与导入数据不匹配时是否抛出异常。可设置为 01