ruby中如何操作json数据结构
来源:12-9 filter 插件之geoip和ruby 讲解

qq_无妄_3
2019-03-13
这是bx2.txt中的数据
[{"COLLECTTIME":"2019-03-12 11:09:05","CompKey":"134.108.14.44|katest_BILLING_MQ_billing_broker_002_%RETRY%acctQueryBillMqConsumerGroup_acctQueryBillMqConsumerGroup_10911","HOSTIP":"134.108.14.44","ID":"189551268","METRICCODE":"30202624270009","METRICTYPE":"2","METRICVALUE":"0","SEQID":""},{"COLLECTTIME":"2019-03-12 11:09:05","CompKey":"134.108.14.43|katest_BILLING_MQ_billing_broker_001_%RETRY%acctQueryBillMqConsumerGroup_acctQueryBillMqConsumerGroup_10911","HOSTIP":"134.108.14.43","ID":"189551274","METRICCODE":"30202624270009","METRICTYPE":"2","METRICVALUE":"0","SEQID":""}]
这是我的conf
input {
file{
path => "/app/app/logstash-6.2.4/conf/bx2.txt"
start_position => beginning
type => "test1"
}
}
filter{
json{
skip_on_invalid_json => true
source => "message"
target => "message1"
}
ruby{
code => 'message1 = event.get("message1")
temp1=Hash["30202623260001" => "170201","30202623260002" => "170201","30202623260003" => "170201","30202623260004" => "170201","30202623260005" => "170201","30202623260006" => "170202","30202624260001" => "170201","30202624260002" => "170202","30202624260003" => "170202","30202624260004" => "170202","30202624260005" => "170202","30202624260006" => "170202","30202624260007" => "170202","30202624260008" => "170202","30202624260009" => "170203","30202624260010" => "170203","30202624260011" => "170201","30202624260012" => "170201"]
length = message1.length-1
message2 = Array.new
for i in 0..length do
temp2 = message1[i]
temp4 = temp2["METRICCODE"]
temp3 = temp1[temp4]
temp2["CONTYPE"]= temp3
message2[i] = temp2
end
event.set("doc2",message2)'
}
mutate {
remove_field => [ "message","message1" ]
}
}
output{
stdout { codec => rubydebug }
# if [tags] {
#
# }else{
# stdout { codec => rubydebug }
# file {
# path => "/app/app/logstash-6.2.4/test.txt"
# codec => line { format => "%{message1}"}
# }
# }
}
这是结果;没出来为nil。。。。
我对ruby也不了解
搞了两天了;没头绪;请教老师这是哪里的问题。
写回答
2回答
-
rockybean
2019-03-14
赞!
看起来你是想做一个 enrichment 的操作,可以考虑用 logstash 如下的 filter,这样就不用写死了。
https://www.elastic.co/guide/en/logstash/6.6/plugins-filters-elasticsearch.html
https://www.elastic.co/guide/en/logstash/6.6/plugins-filters-jdbc_static.html
https://www.elastic.co/guide/en/logstash/6.6/plugins-filters-jdbc_streaming.html
https://www.elastic.co/guide/en/logstash/6.6/plugins-filters-elasticsearch.html
00 -
qq_无妄_3
提问者
2019-03-13
搞出来了;原来是我的temp1里面没有响应的匹配字段;好愚蠢的问题啊
哈哈哈哈哈哈
00
相似问题