本文介绍使用映射富化函数e_dict_map、e_search_dict_map进行数据富化的实践案例。
背景信息
- 普通映射函数使用文本完全匹配方式来映射。普通映射函数包括e_dict_map函数和e_table_map函数,两者区别在于e_dict_map函数接收的是dict类型的数据,e_table_map函数接收的是通过资源函数获取的table类型的数据。
例如:在nginx日志中,将特定的状态码转换为文本格式,可以使用普通映射函数e_dict_map。
状态码 文本 200 成功 300 跳转 400 请求错误 500 服务器错误 - 搜索映射函数的映射关键字是查询字符串,支持正则表达式匹配、完全匹配、模糊匹配等形式。搜索映射函数包括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_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函数进行数据富化
本案例介绍使用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