2017-03-26 26 views
1

我正在使用AWS Athena过滤负载均衡器日志。我创建了下表并将日志导入到表中。配置单元 - 相关值的总和

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 

它的工作,但它会返回所有的响应,如下所示。

**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总和(400,401,404,413,422)

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

回答

0

假设所有代码都长

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 
+0

谢谢,它的工作,这可能显示与xx响应代码值。像2xx,3xx在结果窗口?现在它显示2,3,4,5。 – SQLadmin

+0

查看已更新回答 –

+0

真棒:),感谢Dudu – SQLadmin