如何解决 Amazon Athena 中的 RegexSerDe 错误“Number of matching groups doesn't match the number of columns”(匹配组数与列数不匹配)?
上次更新时间:2020 年 9 月 24 日
我使用 RegexSerDe 创建了一个 Amazon Athena 表。当我查询此表时,收到了以下错误:“Number of matching groups doesn't match the number of columns”(匹配组数与列数不匹配)。
解决方法
要解决此错误,请确保正则表达式模式中的捕获组数量与在 Athena 中创建表时定义的字段数量匹配。例如,有这样一行输入数据:
64.xxx.xx.xx - - "GET /twiki/bin/rdiff/TWiki/NewUserTemplate?rev1=1.3&rev2=1.2 HTTP/1.1" 200 4523
该行有七个字段。下面是正确的正则表达式模式:
^(+) () () \\+ {4})\\] \"(.+?)\" ({3}) (+)$
注意:RegexSerDe 采用 Java 标准。由于反斜杠是 Java String 类中的转义字符,因此必须使用双反斜杠来定义单反斜杠。例如,要定义 \w,您必须在正则表达式中使用 \\w。
在此正则表达式模式中有七个捕获组,输入数据中有七个字段。当您查询表时,RegexSerDe 不会发出“Number of matching groups doesn't match the number of columns”(匹配组数与列数不匹配)异常。
要运行 DDL 语句,请指定 SERDEPROPERTIES 的正则表达式捕获组,如下例中所示:
CREATE EXTERNAL TABLE logs
(
col1 string,
col2 string,
col3 string,
col4 string,
col5 string,
col6 string,
col7 string
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe'
WITH SERDEPROPERTIES (
"input.regex" = "^(+) () () \\+ {4})\\] \"(.+?)\" ({3}) (+)$")
LOCATION 's3://doc-example-bucket/path/'