全部產品
Search
文件中心

Simple Log Service:資料過濾與清洗

更新時間:Aug 27, 2025

您可以通過SPL指令和SQL函數過濾與清洗您所採集的海量日誌資料,實現資料格式標準化。本文介紹過濾與清洗資料的常見情境和相關操作。

情境1:過濾日誌(where指令)

您可以使用where指令過濾日誌。常用規則如下所示:

where <bool-expression>

樣本如下所示:

子情境1:根據欄位內容過濾資料條目。

  • 原始日誌

    #日誌1
    __source__:  192.168.0.1
    __tag__:__client_ip__:  192.168.0.2
    __tag__:__receive_time__:  1597214851
    __topic__: app
    class:  test_case
    id:  7992
    test_string:  <function test1 at 0x1027401e0>
    #日誌2
    __source__:  192.168.0.1
    __tag__:__client_ip__:  192.168.0.2
    __tag__:__receive_time__:  1597214861
    __topic__: web
    class:  test_case
    id:  7992
    test_string:  <function test1 at 0x1027401e0>
  • SPL語句

    丟棄__topic__欄位為app的日誌。

    * | where __topic__!='app'
  • 輸出結果

    __source__:  192.168.0.1
    __tag__:__client_ip__:  192.168.0.2
    __tag__:__receive_time__:  1597214861
    __topic__: web
    class:  test_case
    id:  7992
    test_string:  <function test1 at 0x1027401e0>

子情境2:使用匹配欄位名的Regex過濾資料條目。

  • 原始日誌

    #日誌1
    __source__:  192.168.0.1
    __tag__:__client_ip__:  192.168.0.2
    __tag__:__receive_time__:  1597214851
    __topic__: app
    class:  test_case
    id:  7992
    test_string:  <function test1 at 0x1027401e0>
    server_protocol:test
    #日誌2
    __source__:  192.168.0.1
    __tag__:__client_ip__:  192.168.0.2
    __tag__:__receive_time__:  1597214861
    __topic__: web
    class:  test_case
    id:  7992
    test_string:  <function test1 at 0x1027401e0>
    server_protocol: 14861
  • SPL語句

    保留server_protocol為數位欄位。

    * | where regexp_like(server_protocol, '^\d+$')
  • 輸出結果

    __source__:  192.168.0.1
    __tag__:__client_ip__:  192.168.0.2
    __tag__:__receive_time__:  1597214861
    __topic__: web
    class:  test_case
    id:  7992
    test_string:  <function test1 at 0x1027401e0>
    server_protocol: 14861

情境2:為日誌空缺欄位賦值(extend、parse-regexp指令)

您可以使用extendparse-regexp指令過濾日誌。樣本如下所示:

子情境1:原欄位不存在或者為空白時,為欄位賦值。

* | extend <output>=<expression>, ...
  • 輸入資料

    name:
  • SPL語句:為name欄位賦值

    * | extend name='lily'
  • 輸出結果

    name:lily

子情境2:使用Regex從文字欄位中提取結構化內容。

| parse-regexp -flags=<flags> <field>, <pattern> as <output>, ...
  • 輸入資料

    content: '10.0.0.0 GET /index.html 15824 0.043'
  • SPL語句

    * | parse-regexp content, '(\S+)' as ip | parse-regexp content, '\S+\s+(\w+)' as method
  • 輸出結果

    content: '10.0.0.0 GET /index.html 15824 0.043'
    ip: '10.0.0.0'
    method: 'GET'

子情境3:為多個欄位賦值。

| extend <output>=<expression> | extend <output1>=<expression> | <output2>=<expression>
  • 輸入資料

    __source__:  192.168.0.1
    __topic__:
    __tag__:
    __receive_time__:
    id:  7990
    test_string:  <function test1 at 0x1020401e0>
  • SPL語句

    為__topic__欄位、__tag__欄位和__receive_time__欄位賦值。

    * | extend __topic__='app' | extend __tag__='stu' | extend __receive_time__='1597214851'
  • 輸出資料

    __source__:  192.168.0.1
    __topic__:  app
    __tag__:  stu
    __receive_time__:  1597214851
    id:  7990
    test_string:  <function test1 at 0x1020401e0>

情境3:刪除和重新命名欄位(project-away、project-rename指令)

推薦您使用project-awayproject-rename指令進行刪除和重新命名欄位。

子情境1:刪除特定欄位。

| project-away -wildcard-off <field-pattern>, ...
  • 輸入資料

    content:123
    age:23
    name:twiss
  • SPL語句

    * | project-away age, name
  • 輸出結果

    content:123

子情境2:重新命名特定欄位。

| project-rename <output>=<field>, ...
  • 輸入資料

    content:123
    age:23
    name:twiss
  • SPL語句

    * | project-rename new_age=age, new_name=name
  • 輸出結果

    content:123
    new_age:23
    new_name:twiss

情境4:轉換日誌參數類型

子情境1:調用concat函數進行字元拼接。

  • 輸入資料

    x: 123
    y: 100
  • SPL語句

    * | extend a=cast(x as bigint) + cast(y as bigint)| extend b=concat(x, y)
  • 輸出結果

    x: 123
    y: 100
    a: 223
    b: 123100

子情境2:調用字串或日期時間轉換為標準時間。 如下使用to_unixtime函數將time1表示的日期時間轉化為Unix時間戳記。

  • 原始日誌

    time1: 2020-09-17 9:00:00
  • 加工規則

    將time1表示的日期時間轉化為Unix時間戳記。

    * | extend time1=cast(time1 as TIMESTAMP) | extend new_time=to_unixtime(time1)
  • 加工結果

    time1:  2020-09-17 9:00:00
    time2:  1600333200.0

情境5:為日誌不存在的欄位填充預設值(COALESCE運算式)

使用COALESCE運算式為不存在的欄位填充預設值。

  • 輸入資料

    server_protocol: 100
  • SPL語句

    如果server_protocol存在則y為server_protocol的值,如果server_protocol1不存在則x為200。

    * | extend x=COALESCE(server_protocol1, '200') | extend y=COALESCE(server_protocol, '200')
  • 輸出結果

    server_protocol: 100
    x: 200
    y: 100

情境6:判斷日誌並增加欄位(where和extend組合指令)

推薦您使用whereextend組合指令進行。

* | where <bool-expression> | extend <output>=<expression> |...

樣本如下所示:

  • 輸入資料

    status1: 200
    status2: 404
  • SPL語句

    * | where status1='200'| extend status1_info='normal' | where status2='404'| extend status2_info='error'
  • 輸出結果

    status1: 200
    status2: 404
    status1_info: normal
    status2_info: error