【问题标题】:How to access / address nested fields on Logstash如何访问/解决 Logstash 上的嵌套字段
【发布时间】:2020-07-20 21:06:16
【问题描述】:

我目前正在尝试使用 Logstash 过滤器将包含十六进制字符串的嵌套子字段转换为 int 类型字段

ruby{
    code => 'event.set("[transactions][gasprice_int]", event.get("[transactions][gasPrice]").to_i(16))'
  }

但它正在返回错误

[ERROR][logstash.filters.ruby    ][main][2342d24206691f4db46a60285e910d102a6310e78cf8af43c9a2f1a1d66f58a8] Ruby exception occurred: wrong number of arguments calling `to_i` (given 1, expected 0)

我还尝试使用循环遍历交易字段中的 json 对象

transactions_num = event.get("[transactions]").size
      transactions_num.times do |index|
        event.set("[transactions][#{index}][gasprice_int]", event.get("[transactions][#{index}][gasPrice].to_i(16)"))
      end

但这也返回了一个错误

[ERROR][logstash.filters.ruby    ][main][99b05fdb6022cc15b0f97ba10cabb3e7c1a8fabb8e0c47d6660861badffdb28e] Ruby exception occurred: Invalid FieldReference: `[transactions][0][gasPrice].to_i(16)`

当我不处理嵌套字段时,这种使用 ruby​​ 过滤器将十六进制字符串转换为 int 类型的方法有效,所以任何人都可以帮助我在这种情况下如何正确处理嵌套字段?

顺便说一句,

这是仍然有效的代码

ruby {
    code => 'event.set("difficulty_int", event.get("difficulty").to_i(16))'
  }

【问题讨论】:

    标签: ruby logstash filtering elk


    【解决方案1】:

    我认为您已经在最后一个示例中自己回答了这个问题 - to_i 不应该在双引号内。所以

    ...event.get("[transactions][#{index}][gasPrice].to_i(16)"))
    

    应该是

    ...event.get("[transactions][#{index}][gasPrice]").to_i(16))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 2015-08-06
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多