RegEx 创建 AWS Athena Table (RegexSerDe)

RegEx to create AWS Athena Table (RegexSerDe)

我正在尝试根据存储在 S3 中的日志创建 AWS Athena table。我打算使用 RegEx 创建 table 但我找不到适合我的 RegEx

CREATE EXTERNAL TABLE `dev_logs`(
  `date_time` string COMMENT '', 
  `type` string COMMENT '', 
  `request_id` string COMMENT '', 
  `body` string COMMENT '', 
  `exception` string COMMENT '')
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.RegexSerDe' 
WITH SERDEPROPERTIES ( 
  'input.regex'='\[([^ ]* +[^ ]*)\] \[([^ ]*)\] ([^ ]*) \[([^ ]* +[^ ]*)\] (\*)'
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  's3://dev/logs'

日志采用这种格式

[2020-05-04 10:26:56.393] [INFO] [123] [Building host...] []
[2020-05-04 10:27:01.623] [INFO] [] [Starting Service checks...] [exception details]

正则表达式是

'\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d*)\] \[(.*?)\] \[(.*?)\] \[(.*?)\] \[(.*?)\].*?$'

在 Hive 中,您可以使用 regexp_replace:

轻松调试它
select regexp_replace('[2020-05-04 10:26:56.393] [INFO] [123] [Building host...] []',
                      '\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d*)\] \[(.*?)\] \[(.*?)\] \[(.*?)\] \[(.*?)\].*?$',
                     ', , , , ')

结果

2020-05-04 10:26:56.393, INFO, 123, Building host...,