
# 使用e_dict_map、e_search_dict_map函数进行数据富化
本文介绍使用映射富化函数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":  "huaweicloud.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请求状态码](https://www.restapitutorial.com/httpstatuscodes.html)。当http_status字段的值为401、404时，需要更新字典覆盖，否则无法匹配。
  
- 加工结果
  ```
  {
  "status_desc": "跳转",
  "http_status": 300,
  "request_method": "GET",
  "http_host": "example.com"
  }
  {
  "status_desc": "成功",
  "http_status": 200,
  "request_method": "POST",
  "http_host": "example.org"
  }
  {
  "status_desc": "请求错误",
  "http_status": 400,
  "request_method": "GET",
  "http_host": "example.net"
  }
  {
  "status_desc": "服务器错误",
  "http_status": 500,
  "request_method": "GET",
  "http_host": "huaweicloud.com"
  }
  ```
  
 
#### 使用e_search_dict_map函数进行数据富化
本案例介绍使用e_search_dict_map函数完成数据富化的方法。
- 原始日志
  ```
  [{
      "http_host":  "example.com",
      "http_status":  200,
      "request_method":  "GET"
  },
  {
      "http_host":  "example.org",
      "http_status":  201,
      "request_method":  "POST"
  },
  {
      "http_host":  "example.net",
      "http_status":  404,
      "request_method":  "GET"
  }]
  ```
  
- 加工需求 根据日志中的http_status字段的值的不同，为每条日志添加不同的type信息。
  - 为http_status为2XX的日志，添加type字段，并将字段值设置为正常。
  
  - 为http_status为3XX的日志，添加type字段，并将字段值设置为重定向。
  
  - 为http_status为4XX的日志，添加type字段，并将字段值设置为错误。
   
- 加工规则
  ```
  e_search_dict_map({"http_status:2??": "正常","http_status:3??": "重定向","http_status:4??": "错误"}, "http_status", "type")
  ```
  
- 加工结果
  ```
  {
  "http_status": "正常",
  "request_method": "GET",
  "http_host": "example.com"
  }
  {
  "http_status": "正常",
  "request_method": "POST",
  "http_host": "example.org"
  }
  {
  "http_status": "错误",
  "request_method": "GET",
  "http_host": "example.net"
  }
  ```
  
 
