如何在heka的payloadregexdecoder中使用整数类型

chhqkbe1  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(310)

我尝试使用heka(而不是logstash)作为日志解析器来插入elasticsearch。此时,我使用payloadregexdecoder分割每个字段。
除整数类型外,大多数配置都能正常工作。
输入日志字符串

May 4 13:01:04 db1 monitoring: 2015-05-04T13:01:01Z 174.35.6.22 aaaa bbbb 111

这是我的heka配置示例。

[fdb]
type = "PayloadRegexDecoder"
match_regex = '^(?P<month>.+?) (?P<day>.+?) (?P<time>.+?) (?P<remote_user>.+?) monitoring: (?P<Timestamp>.+?) (?P<ip>.+?) (?P<a>.+?) (?P<b>.+?) (?P<int_test>\d+)'

[fdb.message_fields]
Type = "f_monitoring"
month = "%month%"
day = "%day%"
time = "%time%"
remote_user = "%remote_user%"
ip|ipv4 = "%ip%"
a = "%a%"
b = "%b%"
int_test|B = "%int_test%"`

这是此设置的结果。

:Timestamp: 2015-05-04 13:01:01 +0000 UTC
:Type: f_monitoring
:Hostname: 
:Pid: 0
:Uuid: b824d53c-7d7e-4424-a123-780aa37bf879
:Logger: UdpInput
:Payload: May 4 13:01:04 db1 monitoring: 2015-05-04T13:01:01Z 174.35.6.22 aaaa bbbb 111
:EnvVersion: 
:Severity: 7
:Fields:
   | name:"int_test" type:string value:"111" representation:"B"
   | name:"a" type:string value:"aaaa"
   | name:"time" type:string value:"13:01:04"
   | name:"b" type:string value:"bbbb"
   | name:"ip" type:string value:"174.35.6.22" representation:"ipv4"
   | name:"day" type:string value:"4"
   | name:"remote_user" type:string value:"db1"
   | name:"month" type:string value:"May"

我想知道如何将int\u测试的类型从string改为integer或double。

fsi0uk1n

fsi0uk1n1#

据我所知,你不能。在源代码中,正则表达式匹配的值总是一个字符串。你可以改变表示法,但我不知道那会改变什么。
不过,您可以添加一个lua解码器。我认为你不能从消息中删除字段。因此,您必须将消息的副本拉入lua,转换字段,然后发回经过修改的消息。
下面是lua代码的概要(未测试):

function process_message ()
    local mutableMessageCopy = decode_message(read_message("raw"))
    mutableMessageCopy.Fields.int_test = tonumber(mutableMessageCopy.Fields.int_test)
    inject_message(msg)
    return 0
end

如果您将lua文件命名为convertint.lua,下面是您将添加到配置中的内容;您还必须更新输入以使用“chain\u fbp”而不是“fbp”:

[chain_fbp]
type = "MultiDecoder"
subs = ['fbp', 'convert_to_integer']
cascade_strategy = "all"

[convert_to_integer]
type = "SandboxDecoder"
filename = "/absolute/path/to/the/lua/code/convertint.lua"

相关问题