Home > OS >  Creating an array by using aggregate filter
Creating an array by using aggregate filter

Time:03-15

I'm try to create a array group by first column emp_no, I have this structure in a mysql table:

emp_no|salary|
------ ------ 
 10001| 60117|
 10001| 62102|
 10001| 66074|
 10001| 66596|
 10001| 66961|
 10001| 71046|
 10001| 74333|
 10001| 75286|

In logstash I tried aggregate filter using this configuration:

filter {
    aggregate {
    task_id => "%{emp_no}"
    code => "
      map['emp_no'] = event.get('emp_no')
      map['salaries'] ||= []
      map['salaries'] << {'salary' => event.get('salary')}
      event.cancel()
    "
    push_previous_map_as_event => true
    timeout => 3
    }
}

But this way I have this output:

{
  "emp_no": 10001,
  "salaries": [
    {
      "salary": 60117
    },
    {
      "salary": 62102
    },
    {
      "salary": 66074
    },
    {
      "salary": 66596
    },
    {
      "salary": 66961
    },
    {
      "salary": 71046
    },
    {
      "salary": 74333
    }
  ]
}

And I need this output, make an array called salaries that contains only salary values:

{
  "emp_no": 10001,
  "salaries": [
    60117,
    62102,
    66074,
    66596,
    66961,
    71046,
    ...,
  ]
}

CodePudding user response:

map['salaries'] << {'salary' => event.get('salary')}

change this to

map['salaries'] << event.get('salary')
  • Related