【问题标题】:Logstash: Renaming nested fields based on some conditionLogstash:根据某些条件重命名嵌套字段
【发布时间】:2019-10-24 10:02:45
【问题描述】:

我正在尝试在迁移到 Amazonelasticsearch 时重命名 Elasticsearch 中的嵌套字段

在文档中,我想更改

1.如果value字段为JSON类型。将 value 字段更改为 value-keyword 并删除“value-whitespace”和“value-standard”(如果存在)

2.如果 value 字段的大小超过 15。将 value 字段更改为 value-standard

 "_source": {
          "applicationid" : "appid",
          "interactionId": "716bf006-7280-44ea-a52f-c79da36af1c5",
          "interactionInfo": [
            {
              "value": """{"edited":false}""",
              "value-standard": """{"edited":false}""",
              "value-whitespace" :  """{"edited":false}"""
              "title": "msgMeta"
            },
            {
              "title": "msg",
              "value": "hello testing",
            },
            {
              "title": "testing",
              "value": "I have a text that can be done and changed only the size exist more than 20 so we applied value-standard ",
            }
          ],
          "uniqueIdentifier": "a21ed89c-b634-4c7f-ca2c-8be6f31ae7b3",
        }
      }

最终结果应该是

 "_source": {
          "applicationid" : "appid",
          "interactionId": "716bf006-7280-44ea-a52f-c79da36af1c5",
          "interactionInfo": [
            {
              "value-keyword": """{"edited":false}""",
              "title": "msgMeta"
            },
            {
              "title": "msg",
              "value": "hello testing",
            },
            {
              "title": "testing",
              "value-standard": "I have a text that can be done and changed only the size exist more than 20 and so we applied value-standard  ",
            }
          ],
          "uniqueIdentifier": "a21ed89c-b634-4c7f-ca2c-8be6f31ae7b3",
        }
      }

【问题讨论】:

    标签: logstash


    【解决方案1】:

    对于 2),你可以这样做:

    filter {
        if [_source][interactionInfo][2][value] =~ /.{15,15}/ {
    
            mutate {
                rename => ["[_source][interactionInfo][2][value]","[_source][interactionInfo][2][value-standard]"]
            }
        }
    }
    

    正则表达式 .{15,15} 匹配任何长度为 15 个字符的字符串。如果该字段的长度小于 15 个字符,则正则表达式不匹配并且不会应用 mutate#rename

    对于 1),一种可能的解决方案是尝试使用 json 过滤器解析字段,如果没有 _jsonparsefailure 标记,则重命名该字段。

    【讨论】:

    • 不适用于我们的案例。有时标题和值的顺序可能不同。在某些情况下,我们可能有一个嵌套文档(title 和 value),而在其他情况下,我们可能有多个嵌套文档(title 和 value)
    • 那么您可能必须使用ruby 过滤器并在代码中执行此操作。或者为专门构建的应用程序删除 logstash。
    • 将尝试使用 ruby​​ 过滤器。出于目的而放弃logstash意味着没有得到你
    【解决方案2】:

    为此找到了解决方案。我在 Logstash 中使用了 ruby​​ 过滤器来检查每个文档以及嵌套文档 这是红宝石代码

    require 'json'
    
    def register(param)
    end
    
    def filter(event)
      infoarray = event.get("interactionInfo")
      infoarray.each {  |x|
          if x.include?"value"
             value = x["value"]
             if value.length > 15
               apply_only_keyword(x)
             end
           end
          if x.include?"value"
            value = x["value"]
             if validate_json(value)
               apply_only_keyword(x)
             end
           end
      }
    event.set("interactionInfo",infoarray)
    return [event]
    end
    
    
    def validate_json(value)
      if value.nil?
        return false
      end
      JSON.parse(value)
      return true
    rescue JSON::ParserError => e
      return false
    end
    
    def apply_only_keyword(x)
      x["value-keyword"] = x["value"]
      x.delete("value")
      if x.include?"value-standard"
        x.delete("value-standard")
      end
      if x.include?"value-whitespace"
        x.delete("value-whitespace")
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2012-11-17
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      相关资源
      最近更新 更多