SPL 命令と SQL 関数を使用して、収集した大量のログデータをフィルタリングおよびクレンジングし、データ形式を標準化できます。このトピックでは、データフィルタリングとデータクレンジングに関する一般的なシナリオと操作について説明します。
シナリオ 1:ログのフィルタリング (where 命令)
where SPL 命令を使用してログをフィルタリングできます。構文は次のとおりです:
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:フィールド値を正規表現に一致させてログエントリをフィルタリング。
生ログ
# ログ 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: 14861SPL 文
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)
extend および parse-regexp SPL 命令を使用して、フィールドを追加または変更できます。
サブシナリオ 1:空である、または存在しないフィールドへの値の割り当て。
* | extend <output> = <expression>, ...入力データ
name:SPL 文:
nameフィールドに値 'lily' を割り当てます。* | extend name = 'lily'出力
name: lily
サブシナリオ 2:正規表現を使用してテキストフィールドから構造化コンテンツを抽出。
| 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> | extend <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-away および project-rename SPL 命令は、フィールドの削除と名前の変更を行います。
サブシナリオ 1:特定のフィールドの削除。
| project-away -wildcard-off <field-pattern>, ...入力データ
content: 123 age: 23 name: twissSPL 文:
ageフィールドとnameフィールドを削除します。* | project-away age, name出力
content: 123
サブシナリオ 2:特定のフィールドの名前の変更。
| project-rename <output>=<field>, ...入力データ
content: 123 age: 23 name: twissSPL 文:
ageフィールドの名前をnew_ageに変更し、nameフィールドの名前をnew_nameに変更します。* | project-rename new_age = age, new_name = name出力
content: 123 new_age: 23 new_name: twiss
シナリオ 4:データ型の変換
サブシナリオ 1:concat SQL 関数を使用した文字列の連結。
入力データ
x: 123 y: 100SPL 文:数値として
xとyを加算してフィールドaに格納し、文字列として連結してフィールドbに格納します。* | extend a = cast(x as bigint) + cast(y as bigint) | extend b = concat(x, y)出力
x: 123 y: 100 a: 223 b: 123100
たとえば、to_unixtime SQL 関数を使用して、日時値を 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 new_time: 1600333200.0
シナリオ 5:フィールドのデフォルト値の設定 (COALESCE)
COALESCE 式を使用して、存在しないフィールドをデフォルト値で埋めます。
入力データ
server_protocol: 100SPL 文
server_protocolフィールドが存在する場合は、その値をyフィールドに割り当てます。server_protocol1フィールドが存在しない場合は、200をxフィールドに割り当てます。* | extend x = COALESCE(server_protocol1, '200') | extend y = COALESCE(server_protocol, '200')出力
server_protocol: 100 x: 200 y: 100
シナリオ 6:条件付きでのフィールド追加 (where と extend)
where および extend SPL 命令を組み合わせて使用します。
* | where <bool-expression> | extend <output> = <expression> | ...入力データ
status1: 200 status2: 404SPL 文:
status1が200で、かつstatus2が404の場合に、status1_infoフィールドとstatus2_infoフィールドを追加します。* | where status1 = '200' and status2 = '404' | extend status1_info = 'normal', status2_info = 'error'出力
status1: 200 status2: 404 status1_info: normal status2_info: error
シナリオ 7:ナノ秒精度の UNIX タイムスタンプへの変換
生ログに UNIX 時間形式のフィールドが含まれる場合、変換関数を使用してナノ秒精度のタイムスタンプに変換できます。
入力データ
{ "__source__": "1.2.3.4", "__time__": 1704983810, "__topic__": "test" }SPL 文:
__time__フィールドの UNIX タイムスタンプを秒からナノ秒に変換します。* | extend ts_nano = cast(__time__ * 1000000000 as BIGINT)出力
{ "__source__": "1.2.3.4", "__time__": "1704983810", "__topic__": "test", "ts_nano": "1704983810000000000" }
シナリオ 8:ISO 8601 からマイクロ秒タイムスタンプへの変換
生ログに標準の ISO 8601 時刻形式のフィールドが含まれる場合、時刻変換関数を使用してマイクロ秒精度のタイムスタンプとして解析できます。
入力データ
{ "__source__": "1.2.3.4", "__time__": 1704983810, "__topic__": "test", "log_time":"2024-01-11 23:10:43.992847200" }SPL 文:
log_timeフィールドをマイクロ秒単位の UNIX タイムスタンプに変換します。* | extend ts_micro = cast(to_unixtime(cast(log_time as TIMESTAMP)) * 1000000 as BIGINT)出力
{ "__time__": "1704983810", "__topic__": "test", "__source__": "1.2.3.4", "ts_micro": "1705014643992847", "log_time": "2024-01-11 23:10:43.992847200" }