【问题标题】:Hive - Sum of related valuesHive - 相关值的总和
【发布时间】:2017-08-19 06:31:33
【问题描述】:

我正在使用 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 sum(400,401,404,413,422)

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

【问题讨论】:

    标签: sql amazon-web-services count presto amazon-athena


    【解决方案1】:

    假设所有代码都是 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
    

    【讨论】:

    • 谢谢,它的工作,这是否可以用 xx 显示响应代码值。像结果窗口中的 2xx、3xx 一样?现在它显示 2,3,4,5。
    • 不客气。附:查看更新的解决方案。这有很多变体。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 2012-02-07
    • 1970-01-01
    相关资源
    最近更新 更多