This is Logstash ruby filter example to rename field names within a specified hash, based on each value type. The original question was asked at this Elastic Discuss.
With -t
, the filter can be tested:
logstash -e "filter { ruby { path => '/{path_of_the_ruby_script}/rename_by_data_type.rb' script_params => { 'field' => 'the_name_of_target_field' } } }" -t
Output:
[2022-06-15T18:14:42,729][INFO ][logstash.filters.ruby.script] Test run complete {:script_path=>"/{path_of_the_ruby_script}/rename_by_data_type.rb", :results=>{:passed=>2, :failed=>0, :errored=>0}}
Configuration OK
Specify the script file path, and a field name of the target hash field.
filter {
ruby {
path => '/{path_of_the_ruby_script}/rename_by_data_type.rb'
script_params => { 'field' => 'the_name_of_target_field' }
}
}
The data is shown in JSON format for illustration purpose.
Input:
[
{"values": {"fieldA": "a", "fieldB": "b", "fieldC": "c"}},
{"values": {"fieldA": 1, "fieldB": 2, "fieldC": 3}}
]
Output:
[
{"values": {"fieldA_String": "a", "fieldB_String": "b", "fieldC_String": "c"}},
{"values": {"fieldA_Integer": 1, "fieldB_Integer": 2, "fieldC_Integer": 3}}
]