我已使用 RegexSerDe 创建一个 Amazon Athena 表。当我查询此表时,收到了以下错误:“Number of matching groups doesn't match the number of columns”(匹配组数与列数不匹配)。
解决方法
要纠正此错误,请确保 regex 模式中的捕获组数与您在 Athena 中创建表时定义的字段数匹配。例如,以下是输入数据行:
64.xxx.xx.xx - - [07/Jul/2020:16:06:51 -0800] "GET /twiki/bin/rdiff/TWiki/NewUserTemplate?rev1=1.3&rev2=1.2 HTTP/1.1" 200 4523
此行具有 7 个字段。下面是正确的正则表达式模式:
^([0-9.]+) ([\\w.-]) ([\\w.-]) \\[([A-Za-z0-9:/]+ [+-][0-9]{4})\\] \"(.+?)\" ([0-9]{3}) ([0-9]+)$
**注意:**RegexSerDe 采用 Java 标准。由于反斜杠是 Java String 类中的转义字符,因此必须使用双反斜杠来定义单反斜杠。例如,要定义 \w,您必须在 regex 中使用 \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" = "^([0-9.]+) ([\\w.-]) ([\\w.-]) \\[([A-Za-z0-9:/]+ [+-][0-9]{4})\\] \"(.+?)\" ([0-9]{3}) ([0-9]+)$")
LOCATION 's3://doc-example-bucket/path/'
相关信息
使用 SerDe