需要为Logstash使用Aggregate Filer和自定义的ruby过滤器

时间:2019-11-18 12:51:33

标签: elasticsearch logstash logstash-configuration elk logstash-jdbc

需要通过以下数据库查询中的Logstash将数据发送到ElasticSearch

DB与学生和部门的联接查询,其中一个学生可以被多个部门招募,每个部门可以有多个老师。查询后得到以下输出

student_id student_name department_id department_name teacher_id
-----------------------------------------------------------------
1           S1             d1           d1_name           t1
1           S1             d1           d1_name           t2
1           S1             d1           d1_name           t3
1           S1             d2           d2_name           t4
1           S1             d2           d2_name           t5
1           S1             d2           d2_name           t6
2           S2             d1           d2_name           t1
2           S2             d1           d1_name           t2
2           S2             d1           d1_name           t3
2           S2             d3           d3_name           t7
2           S2             d3           d3_name           t8
2           S2             d3           d3_name           t9

需要以下格式的输出:

{
    "student_id": 1,
    "student_name": "s1_name",
    "departments": [
        {
            "id": "d1",
            "name": "d1_name",
            "teacher_id": [
                "t1",
                "t2",
                "t3"
            ]
        },
        {
            "id": "d2",
            "name": "d2_name",
            "teacher_id": [
                "t4",
                "t5",
                "t6"
            ]
        }
    ]
},
{
    "student_id": 2,
    "student_name": "s2_name",
    "departments": [
        {
            "id": "d1",
            "name": "d1_name",
            "teacher_id": [
                "t1",
                "t2",
                "t3"
            ]
        },
        {
            "id": "d3",
            "name": "d3_name",
            "teacher_id": [
                "t7",
                "t8",
                "t9"
            ]
        }
    ]
}
}

我已经阅读了一些ELasticSearch文档,这些文档建议使用聚合过滤器插件。我已经尝试过,但是为一个学生实现了多个部门字段。但是无法实现部门的多名教师字段。

我有以下在Logstash过滤器中使用过的过滤器代码

aggregate {
        task_id => "%{id}"
        code => "
               map['student_id'] = event.get('student_id')
               map['student_name'] = event.get('student_name')
               map['departments'] ||= []
               map['departments'] << {
                    'id' => event.get('department_id'),
                    'name' => event.get('department_name'),

                    #Need help in this structure
                    teacher_id =>

                }.compact
              "
}

0 个答案:

没有答案