logstash的mutate插件

数据修改(Mutate)

filters/mutate 插件是 Logstash 另外一个重要插件。它提供了丰富的基础类型数据处理能力。包括类型转换,字符串处理和字段处理等。html

类型转换

类型转换是 filters/mutate 插件最初诞生时的惟一功能。其应用场景在以前 Codec/JSON 小节已经提到。json

能够设置的转换类型包括:"integer","float" 和 "string"。示例以下:数组

filter {
    mutate {
        convert => ["request_time", "float"]
    }
}

注意:mutate 除了转换简单的字符值,还支持对数组类型的字段进行转换,即将 ["1","2"] 转换成 [1,2]。但不支持对哈希类型的字段作相似处理。有这方面需求的能够采用稍后讲述的 filters/ruby 插件完成。ruby

字符串处理

  • gsub
    仅对字符串类型字段有效
     gsub => ["urlparams", "[\\?#]", "_"]
  • split
    filter {
        mutate {
            split => ["message", "|"]
        }
    }
  • join

    仅对数组类型字段有效ide

    咱们在以前已经用 split 割切的基础再 join 回去。配置改为:ui

    filter {
        mutate {
            split => ["message", "|"]
        }
        mutate {
            join => ["message", ","]
        }
    }

    filter 区段以内,是顺序执行的。因此咱们最后看到的输出结果是:url

    {
        "message" => "123,321,adfd,dfjld*=123",
        "@version" => "1",
        "@timestamp" => "2014-08-20T16:01:33.972Z",
        "host" => "raochenlindeMacBook-Air.local"
    }
  • merge
    合并两个数组或者哈希字段。依然在以前 split 的基础上继续:
    filter {
        mutate {
            split => ["message", "|"]
        }
        mutate {
            merge => ["message", "message"]
        }
    }
    咱们会看到输出:
    {
           "message" => [
            [0] "123",
            [1] "321",
            [2] "adfd",
            [3] "dfjld*=123",
            [4] "123",
            [5] "321",
            [6] "adfd",
            [7] "dfjld*=123"
        ],
          "@version" => "1",
        "@timestamp" => "2014-08-20T16:05:53.711Z",
              "host" => "raochenlindeMacBook-Air.local"
    }

    若是 src 字段是字符串,会自动先转换成一个单元素的数组再合并。把上一示例中的来源字段改为 "host":spa

    filter {
        mutate {
            split => ["message", "|"]
        }
        mutate {
            merge => ["message", "host"]
        }
    }

    结果变成:.net

    {
           "message" => [
            [0] "123",
            [1] "321",
            [2] "adfd",
            [3] "dfjld*=123",
            [4] "raochenlindeMacBook-Air.local"
        ],
          "@version" => "1",
        "@timestamp" => "2014-08-20T16:07:53.533Z",
              "host" => [
            [0] "raochenlindeMacBook-Air.local"
        ]
    }

    看,目的字段 "message" 确实多了一个元素,可是来源字段 "host" 自己也由字符串类型变成数组类型了!插件

    下面你猜,若是来源位置写的不是字段名而是直接一个字符串,会产生什么奇特的效果呢?

  • strip
  • lowercase
  • uppercase

字段处理

  • rename
    重命名某个字段,若是目的字段已经存在,会被覆盖掉:
    filter {
        mutate {
            rename => ["host", "host_syslog"]
        }
    }
  • update
    更新某个字段的内容。若是字段不存在,不会新建。
  • replace
    做用和 update 相似,可是当字段不存在的时候,它会起到 add_field 参数同样的效果,自动添加新的字段。

执行次序

须要注意的是,filter/mutate 内部是有执行次序的。其次序以下:

 coerce 
rename(event) if @rename update(event) if @update replace(event) if @replace convert(event) if @convert gsub(event) if @gsub uppercase(event) if @uppercase lowercase(event) if @lowercase strip(event) if @strip remove(event) if @remove split(event) if @split join(event) if @join merge(event) if @merge copy(event)

filter_matched 这个 filters/base.rb 里继承的方法也是有次序的。

@add_field.each do |field, value|
end
@remove_field.each do |field|
end
@add_tag.each do |tag|
end
@remove_tag.each do |tag|
end

Mutate 过滤器配置选项

mutate 过滤器可使用许多配置选项,例如copy,rename,replace,join, uppercase 及 lowercase。

下表概述了它们:

配置选项 用途
add_field 向事件中添加新字段
remove_field 从事件中删除任意字段
add_tag 向事件中添加任意标签
remove_tag 从事件中删除标签(若是存在)
convert 将字段值转换为另外一种数据类型
id 想现场时间添加惟一的ID
lowercase 将字符串字段转换为小写形式
replace 用新值替换字段
strip 删除开头和结尾的空格
uppercase 将字符串字段转换为等效的大写字母
update 用新值更新现有的字段
rename 重命名时间中的字段
gsub 用于查找和替换字符串中的替换
merge 合并数组或 hash 事件

 转载自:https://blog.csdn.net/UbuntuTouch/article/details/106466873

参考资料:https://www.elastic.co/guide/en/logstash/current/plugins-filters-mutate.html

相关文章
相关标签/搜索