1

This is my pipeline output:

"attributes" => "Width:150,200;Height:200;Size:L"

I'd like to have an output:

"attributes" => [
    "Width"  => [
        150,
        200
    ],
    "Height" => 200,
    "Size"   => "L"
]

I tried using the mutate filter

mutate {
    split => ["attributes", ";"]
}

Which transforms the data this way

"attributes" => [
    [0] "Width:150",
    [1] "Height:200"
    [2] "Size:L"
]

Is there a way to transform it via logstash filters?

2 Answers 2

3

You can use kv filter:

kv {
    source => "attributes"
    field_split => ";"
    value_split => ":"
    target => "attributes"
}

ruby {
    # splits all fields so all fields are array
    code => "event['attributes'].each do |cusField| event['attributes'][cusField[0]] = cusField[1].split(',') end"
    # splits only if field contains ','
    # code => "event['attributes'].each do |cusField| event['attributes'][cusField[0]] = cusField[1].split(',') if cusField[1].include? ',' end"
}
Sign up to request clarification or add additional context in comments.

9 Comments

Just a tiny addition, if I have "Width:100,150;Height..." would I be able to explode even values by comma to array?
You can conbine this with a mutate you used in question.
I get a new field name, is there a way to preserve it in "attributes" ? Also the how to target the mutation with a new fieldname (which I dont know prior to the split)
Updated my answer for preserving in "attributes". You can refer [attributes][Weight] in mutate later.
The thing is I don't know how many attributes there will be (width, height, size and maybe a hundred more) is there a way to rename the fields in some sort of loop?
|
-2

You can use split directly

 filter {
      split {
            field => "attributes"
          }
      }

Here is the documentations: https://www.elastic.co/guide/en/logstash/5.2/plugins-filters-split.html#plugins-filters-split-remove_field

1 Comment

The split filter splits events into multiple events

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.