Hive - 相关值的总和

Hive - Sum of related values

我正在使用 AWS Athena 过滤负载均衡器日志。我创建了以下 table 并将日志导入 table。

CREATE EXTERNAL TABLE IF NOT EXISTS elb_logs  (
  request_timestamp string,   
  elb_response_code string,    
  url string, 
   ) 

ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe'
WITH SERDEPROPERTIES (
         'serialization.format' = '1','input.regex' = '([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*)[:\-]([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-.0-9]*) (|[-0-9]*) (-|[-0-9]*) ([-0-9]*) ([-0-9]*) \\"([^ ]*) ([^ ]*) (- |[^ ]*)\\" (\"[^\"]*\") ([A-Z0-9-]+) ([A-Za-z0-9.-]*)$' )
LOCATION 's3://athena-examples/elb/raw/';

现在我想获得 200 OK、400 和 500 响应计数。所以我执行了下面的查询。

SELECT distinct(elb_response_code),
         count(url) AS count
FROM elb_logs
GROUP BY  elb_response_code

有效,但 returns 所有回复,如下所示。

**response  count**
401   1270
201   1369
422   342
200   3568727
400   1221
404   444
304   10435
413   3
206   30
500   1542

我想对所有 400,401,404,413,422 和 2xx、3xx 和 5xx 求和所以结果应该是 4xx sum(400,401,404,413,422)

**response  count**
4xx           52145  
2xx           1363224
5xx           532

假设所有代码都是 3 个字符长

select      substr (elb_response_code,1,1) || 'xx' as elb_response_code_prefix
           ,count(*)                               as cnt

from        elb_logs

group by    1

这是更通用的解决方案

select      rpad (substr (elb_response_code,1,1),length(elb_response_code),'x') 
                      as elb_response_code_prefix
           ,count(*)  as cnt

from        elb_logs

group by    1