本文介紹使用映射富化函數e_dict_map、e_search_dict_map進行資料富化的實踐案例。
背景資訊
普通映射函數
普通映射函數使用文本完全符合方式來映射,如果需要更豐富的欄位匹配方式例如Regex匹配、完全符合、模糊比對,請使用搜尋映射函數。普通映射函數包括e_dict_map函數和e_table_map函數,兩者區別在於e_dict_map函數接收的是dict類型的資料,e_table_map函數接收的是通過資源函數擷取的table類型的資料。
例如:在nginx日誌中,將特定的狀態代碼轉換為文字格式設定,可以使用普通映射函數e_dict_map,
狀態代碼 | 文本 |
200 | 成功 |
300 | 跳轉 |
400 | 請求錯誤 |
500 | 伺服器錯誤 |
搜尋映射函數
搜尋映射函數的映射關鍵字是查詢字串,支援Regex匹配、完全符合、模糊比對等形式。搜尋映射函數包括e_search_dict_map函數和e_search_table_map函數,兩者區別在於e_search_dict_map函數接收的是dict類型的資料,而e_search_table_map函數接收的是通過資源函數擷取的table類型的資料。
例如:在nginx日誌中,將一定範圍內的狀態代碼轉換為文字格式設定,可以使用搜尋映射函數e_search_dict_map。
狀態代碼 | 文本 |
2XX | 成功 |
3XX | 跳轉 |
4XX | 請求錯誤 |
5XX | 伺服器錯誤 |
使用e_dict_map函數進行資料富化
本案例介紹使用e_dict_map函數完成資料富化的方法。
原始日誌
http_host: example.com http_status: 300 request_method: GET http_host: example.org http_status: 200 request_method: POST http_host: example.net http_status: 400 request_method: GET http_host: aliyundoc.com http_status: 500 request_method: GET
加工需求
將http_status欄位中的請求狀態代碼轉化為文字格式設定,並添加到status_desc欄位中。
加工規則
e_dict_map({"400": "請求錯誤", "500": "伺服器錯誤", "300": "跳轉", "200": "成功"}, "http_status", "status_desc")
說明在實際情況中,HTTP請求狀態代碼不止以上4種,詳情請參見HTTP請求狀態代碼。當http_status欄位的值為401、404時,需要更新字典覆蓋,否則無法匹配。
加工結果
http_host: example.com http_status: 300 request_method: GET status_desc: 跳轉 http_host: example.org http_status: 200 request_method: POST status_desc: 成功 http_host: example.net http_status: 400 request_method: GET status_desc: 請求錯誤 http_host: aliyundoc.com http_status: 500 request_method: GET status_desc: 伺服器錯誤
使用e_search_dict_map函數進行資料富化
如果需要Regex匹配、完全符合、模糊比對等形式進行關鍵字匹配,可以使用e_search_dict_map函數。
原始日誌
http_host: example.com http_status: 200 request_method: GET body_bytes_sent: 740 http_host: example.org http_status: 200 request_method: POST body_bytes_sent: 1123 http_host: example.net http_status: 404 request_method: GET body_bytes_sent: 711 http_host: aliyundoc.com http_status: 504 request_method: GET body_bytes_sent: 1822
加工需求
根據日誌中的http_status欄位和body_bytes_sent欄位的值的不同,為每條日誌添加不同的type資訊。
為http_status為2XX且body_bytes_sent長度小於1000的日誌,添加type欄位,並將欄位值設定為正常。
為http_status為2XX且body_bytes_sent長度大於等於1000的日誌,添加type欄位,並將欄位值設定為過長警告。
為http_status為3XX的日誌,添加type欄位,並將欄位值設定為重新導向。
為http_status為4XX的日誌,添加type欄位,並將欄位值設定為錯誤。
為其餘所有日誌,添加type欄位,並將欄位值設定為其他。
加工規則
e_search_dict_map({'http_status~="2\d+" and body_bytes_sent < 1000': "正常", 'http_status~="2\d+" and body_bytes_sent >= 1000': "過長警告", 'http_status~="3\d+"': "重新導向", 'http_status~="4\d+"': "錯誤", "*": "其他"}, "http_status", "type")
基於字典的富化,除了可以使用大括弧({})直接構建字典外,還可以基於任務配置資源、外部OSS資源、表格資源等來構建字典,詳情請參見字典構建。
加工結果
type: 正常 http_host: example.com http_status: 200 request_method: GET body_bytes_sent: 740 type: 過長警告 http_host: example.org http_status: 200 request_method: POST body_bytes_sent: 1123 type: 錯誤 http_host: example.net http_status: 404 request_method: GET body_bytes_sent: 711 type: 其他 http_host: aliyundoc.com http_status: 504 request_method: GET body_bytes_sent: 1822