赛尔校园公共服务平台 Logo
平台使用
阿里云
百度云
移动云
智算服务
教育生态
登录 →
赛尔校园公共服务平台 Logo
平台使用 阿里云 百度云 移动云 智算服务 教育生态
登录
  1. 首页
  2. 阿里云
  3. 日志服务
  4. 操作指南
  5. 数据加工
  6. 数据加工(旧版)
  7. 最佳实践
  8. 事件判断

事件判断

  • 最佳实践
  • 发布于 2025-04-22
  • 0 次阅读
文档编辑
文档编辑

通过事件判断可以更好地对符合特定条件的数据进行相应操作,让加工逻辑更可靠。本文主要介绍使用函数进行事件判断的常见场景和最佳方案示例。

场景1:判断字段是否存在

  • 原始日志

    a: a_value
    b:       //空字符串
  • SLS DSL编排

    • 方案一(推荐):采用e_has或e_not_has。

      e_if(e_has("a"),e_set("has_a", true))
      e_if(e_has("b"),e_set("has_b", true))
      e_if(e_has("c"),e_set("has_c", true))
      e_if(e_not_has("a"),e_set("not_has_a", true))
      e_if(e_not_has("b"),e_set("not_has_b", true))
      e_if(e_not_has("c"),e_set("not_has_c", true))
    • 方案二:采用e_search。

      e_if(e_search('a: *'),e_set("has_a", true))
      e_if(e_search('b: *'), e_set("has_b", true))
      e_if(e_search('c: *'), e_set("has_c", true))
      e_if(e_search('not a: *'), e_set("not_has_a", true))
      e_if(e_search('not b: *'), e_set("not_has_b", true))
      e_if(e_search('not c: *'), e_set("not_has_c", true))
      说明

      加工规则中的e_if可通过e_if(条件1,操作1,条件2,操作2)的形式合并为一项,此处和本文其他处的拆分是为了方便阅读。

  • 加工结果

    a:a_value
    b:    //空字符串
    has_a: true
    has_b: true
    not_has_c: true

场景2:判断字段值是否存在且不为空

  • 原始日志

    a: a_value
    b:     // 空字符串
  • SLS DSL编排

    • 方案一(推荐):采用字段取值函数v

      e_if(v("a"), e_set("not_empty_a", true))
      e_if(v("b"), e_set("not_empty_b", true))
      e_if(v("c"), e_set("not_empty_c", true))
      说明

      字段取值函数v,当对应字段存在且值不为空时,其自动转换的Bool值为true,否则为false。

    • 方案二:采用e_search

      #至少一个字符
      e_if(e_search('a: "?"'), e_set("not_empty_a", true))
      e_if(e_search('b: "?"'), e_set("not_empty_b", true))
      e_if(e_search('c: "?"'), e_set("not_empty_c", true))
      
      #正则
      e_if(e_search('a~=".+"'), e_set("not_empty_a", true))
      e_if(e_search('b~=".+"'), e_set("not_empty_b", true))
      e_if(e_search('c~=".+"'), e_set("not_empty_c", true))
      
      #存在且不为空
      e_if(e_search('a: * and not a==""'), e_set("not_empty_a", true))
      e_if(e_search('b: * and not b==""'), e_set("not_empty_b", true))
      e_if(e_search('c: * and not c==""'), e_set("not_empty_b", true))
  • 加工结果

    a: a_value
    b:     //空串
    not_empty_a: true

场景3:判断字段值是否存在且为空

  • 原始日志

    a: a_value
    b:       // 空字符串
  • SLS DSL编排

    • 方案一(推荐):采用字段取值函数v

      e_if(op_and(e_has("a"), op_not(v("a"))), e_set("empty_a", true))
      e_if(op_and(e_has("b"), op_not(v("b"))), e_set("empty_b", true))
      e_if(op_and(e_has("c"), op_not(v("c"))), e_set("empty_c", true))
      
      # 错误方案
      e_if(op_not(v("a")), e_set("empty_a", true))
      e_if(op_not(v("b")), e_set("empty_b", true))
      e_if(op_not(v("c")), e_set("empty_c", true))
      说明

      字段取值函数v,当对应字段存在且值不为空时,其自动转换的Bool值为true,否则为false。当值不存在时,其返回true,op_not(None)时也是返回true。

    • 方案二:采用e_search

      e_if(e_search('a==""'), e_set("empty_a", true))
      e_if(e_search('b==""'), e_set("empty_b", true))
      e_if(e_search('c==""'), e_set("empty_c", true))
      
      # 错误调用
      e_if(e_search('a:""'), e_set("empty_a", true))
      e_if(e_search('b:""'), e_set("empty_b", true))
      说明

      以上错误调用中,因函数e_search为部分查询,字段存在时,无论其值是否空串,空串a: ""一直为true。

  • 加工结果

    a: a_value
    b:       //空字符串
    empty_b: true

场景4:基于字段值的逻辑查询判断

  • 原始日志

    "日志1"
    http_host: example.com
    status: 200
    request_method: GET
    scheme: https
    header_length: 700
    body_length: 1200
    
    "日志2"
    http_host: example.org
    status: 200
    request_method: POST
    scheme: https
    header_length: 100
    body_length: 800
    
    "日志3"
    http_host: example.net
    status: 200
    request_method: GET
    scheme:  http
    header_length: 700
    body_length: 800
    
    "日志4"
    http_host: aliyundoc.com
    status: 404
    request_method: GET
    scheme: https
    header_length: 100
    body_length: 300
  • 加工需求1

    为所有status字段值为200的日志事件,添加一个新字段type,其值为normal。

    • SLS DSL编排

      e_if(e_match("status", "200"), e_set("type", "normal"))
      或
      e_if(e_search('status==200'), e_set("type", "normal"))
      说明
      • 简单场景下,以上两种编排均可。

      • 本文情况下可采用status:200,表示status是否包含200,但推荐使用status==200更精确。

    • 加工结果

      "日志1"
      type: normal
      http_host: example.com
      status: 200
      request_method: GET
      scheme: https
      header_length: 700
      body_length: 1200
      
      "日志2"
      type: normal
      http_host: example.org
      status: 200
      request_method: POST
      scheme: https
      header_length: 100
      body_length: 800
      
      "日志3"
      type: normal
      http_host: example.net
      status: 200
      request_method: GET
      scheme: http
      header_length: 700
      body_length: 800
      
      "日志4"
      http_host: aliyundoc.com
      status: 404
      request_method: GET
      scheme: https
      header_length: 100
      body_length: 300
  • 加工需求2

    为所有status字段值为200,且request_method字段值为GET,且scheme字段值为https的日志事件添加一个新字段type,其值为normal。

    • SLS DSL编排

      e_if(e_search('status==200 and request_method==GET and scheme==https'), e_set("type", "normal"))
      或
      e_if(e_match_all("status", "200", "request_method", "GET", "scheme", "https"), e_set("type", "normal"))
      说明
      • 需要同时满足多个字段的匹配条件的应用场景中,您可采用e_search或e_match_all,e_search用法相对简洁。

      • 本文情况可以采用status: 200,表示status是否包含200,但推荐使用status==200 更精确。

    • 加工结果

      "日志1"
      type: normal
      http_host: example.com
      status: 200
      request_method: GET
      scheme: https
      header_length: 700
      body_length: 1200
      
      "日志2"
      http_host: example.org
      status: 200
      request_method: POST
      scheme: https
      header_length: 100
      body_length: 800
      
      "日志3"
      http_host: example.net
      status: 200
      request_method: GET
      scheme: http
      header_length: 700
      body_length: 800
      
      "日志4"
      http_host: aliyundoc.com
      status: 404
      request_method: GET
      scheme: https
      header_length: 100
      body_length: 300
  • 加工需求3

    为所有status字段值为200,或request_method字段值为GET,或scheme字段值为https的日志事件添加一个字段type,其值为normal。

    • SLS DSL编排

      e_if(e_search('status==200 or request_method==GET or scheme==https'), e_set("type", "normal"))
      或者
      e_if(e_match_any("status", "200", "request_method", "GET", "scheme", "https"), e_set("type", "normal"))
    • 加工结果

      "日志1"
      type: normal
      http_host: example.com
      status: 200
      request_method: GET
      scheme: https
      header_length: 700
      body_length: 100
      
      "日志2"
      type: normal
      http_host: example.org
      status: 200
      request_method: POST
      scheme: https
      header_length: 100
      body_length: 800
      
      "日志3"
      type: normal
      http_host: example.net
      status: 200
      request_method: GET
      scheme: http
      header_length: 700
      body_length: 800
      
      "日志4"
      type: normal
      http_host: aliyundoc.com
      status: 404
      request_method: GET
      scheme: https
      header_length: 100
      body_length: 1300
  • 加工需求4

    为所有status字段值为200,且request_method字段值为GET,且header_length,且body_length的字段值之和小于等于1000的日志事件,添加一个新字段type,其值为normal。

    • SLS DSL编排

      e_if(op_and(e_search('status: 200 and request_method: GET'), op_le(op_sum(v("header_length"), v("body_length")), 1000)), e_set("type", "normal"))
      说明

      在复杂的逻辑场景下,您可采用e_search和其他表达式函数的组合完成SLS DSL编排。

    • 加工结果

      "日志1"
      type: normal
      http_host: example.com
      status: 200
      request_method: GET
      scheme: https
      header_length: 700
      body_length: 100
      
      "日志2"
      http_host: example.org
      status: 200
      request_method: POST
      scheme: https
      header_length: 100
      body_length: 800
      
      "日志3"
      http_host: example.net
      status: 200
      request_method: GET
      scheme: http
      header_length: 700
      body_length: 800
      
      "日志4"
      http_host: aliyundoc.com
      status: 404
      request_method: GET
      scheme: https
      header_length: 100
      body_length: 1300
相关文章

调用函数清洗数据 2025-04-22 10:54

您可以通过日志服务数据加工函数清洗您所采集的海量日志数据,实现数据格式标准化。本文介绍调用函数清洗数据的常见场景和相关操作。 场景1:过滤日志(e_keep函数和e_drop函数)

事件判断 2025-04-22 10:54

通过事件判断可以更好地对符合特定条件的数据进行相应操作,让加工逻辑更可靠。本文主要介绍使用函数进行事件判断的常见场景和最佳方案示例。 场景1:判断字段是否存在

处理日期时间 2025-04-22 10:54

处理日期时间,将方便您对日志后续查询与可视化展示。本文档主要介绍使用函数进行日期时间数据类型转换和日期时间偏移。 概念解释

数据脱敏 2025-04-22 10:54

数据脱敏可以有效地减少敏感数据在加工、传输、使用等环节中的暴露,降低敏感数据泄露的风险,保护用户权益。本文介绍日志服务数据加工过程中常见的脱敏场景、对应的脱敏方法及示例。 背景信息

加工历史数据 2025-04-22 10:54

日志服务支持加工历史数据,本文介绍加工历史数据的操作步骤及相关问题。 前提条件

解析Syslog标准格式数据 2025-04-22 10:54

Syslog是一种行业标准的协议,可用来记录设备的日志。常见的应用场景是网络管理工具、安全管理系统、日志审计系统。本文档介绍如何使用SLS DSL中的GROK函数高效快捷地解析不同格式的Syslog日志。 概况

目录
Copyright © 2025 your company All Rights Reserved. Powered by 赛尔网络.
京ICP备14022346号-15
gongan beian 京公网安备11010802041014号