Hello,
I have a string "5.34" and I am trying to multiply it by number, say 1000.
Can I do it with gsub or ruby plugin. I have read that ruby plugin is resource intensive, so i would prefer to do it with gsub if possible.
Hello,
I have a string "5.34" and I am trying to multiply it by number, say 1000.
Can I do it with gsub or ruby plugin. I have read that ruby plugin is resource intensive, so i would prefer to do it with gsub if possible.
Don't assume that regular expression substitutions are slower than ruby filters. Measure!
Do your strings always have two decimals?
I am not sure, but from the rounding, most likely it will have two decimal points.
Here is what I have
input { stdin { codec => json } }
output { stdout { codec => rubydebug { metadata => true } } }
filter {
mutate {
add_field => { "Test1"=> "%{Test}" }
convert => { "Test1" => "float"}
}
ruby {code => "event['foo'] = event['Test1'] * 1000"}
}
I ran
echo '{"Test":"5.34"}' | ../../bin/logstash -f ../test.config
Here is the snippet from resulting JSON
"Test1" => "5.34",
"foo" => "5.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.345.34"
}
Shouldn't it multiply instead?
I can gsub . with "" but I need to count the number of places I need to move the decimal point.
The JSON you passed in has the value represented as a string, which would explain why the calculation failed and the string instead got concatenated 1000 times.
It's converted into float
Looks like adding the field and doing a convert in single mutate is the problem. I found on this Convert String to float failed
where someone was having the same type of issue. Ended up breaking up into to mutates
mutate { add_field => { "Test1"=> "%{Test}" }}
mutate { convert => {"Test1"=> "float"} }
This should be fixed.
So the resulting calculation now is as expected
"Test1" => 5.34,
"foo" => 5340.0
}
© 2020. All Rights Reserved - Elasticsearch
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant logo are trademarks of the Apache Software Foundation in the United States and/or other countries.