KafkaTopology.kt 4.48 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Table Data Import Service
 * Copyright (C) 2020 Memoriav
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package org.memobase

Jonas Waeber's avatar
Jonas Waeber committed
21
22
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
Jonas Waeber's avatar
Jonas Waeber committed
23
24
import org.apache.kafka.streams.StreamsBuilder
import org.apache.kafka.streams.Topology
25
26
import org.apache.kafka.streams.kstream.KStream
import org.apache.kafka.streams.kstream.Predicate
Jonas Waeber's avatar
Jonas Waeber committed
27
import org.apache.logging.log4j.LogManager
28
import org.memobase.helpers.Default
Jonas Waeber's avatar
Jonas Waeber committed
29
import org.memobase.helpers.JSON
30
import org.memobase.helpers.KEYS
31
import org.memobase.helpers.ReportStatus
Jonas Waeber's avatar
Jonas Waeber committed
32
import org.memobase.model.Report
33
import org.memobase.model.Schema
Jonas Waeber's avatar
Jonas Waeber committed
34
import org.memobase.settings.SettingsLoader
35
import java.io.StringWriter
Jonas Waeber's avatar
Jonas Waeber committed
36
37

class KafkaTopology(private val settings: SettingsLoader) {
Jonas Waeber's avatar
Jonas Waeber committed
38
39
40
    private val log = LogManager.getLogger("SearchDocService")

    private val reportTopic = settings.processReportTopic
Jonas Waeber's avatar
Jonas Waeber committed
41

42
    private val searchDocTransform = SearchDocTransform(settings.appSettings.getProperty(KEYS.SettingsProps.mediaUrl))
43
44
    private val institutionSearchDoc =
        InstitutionSearchDocBuilder(settings.appSettings.getProperty(KEYS.SettingsProps.institutionTypeLabelsPath))
45

46
47
    private val jsonWriter = ObjectMapper().registerKotlinModule().writer()

Jonas Waeber's avatar
Jonas Waeber committed
48
49
50
    fun build(): Topology {
        val builder = StreamsBuilder()
        val stream = builder.stream<String, String>(settings.inputTopic)
51
        val branchedStream = stream
52
53
            .mapValues { value -> JSON.parse(value) }
            .filter { _, value -> value.isNotEmpty() }
Jonas Waeber's avatar
Jonas Waeber committed
54
            .mapValues { value -> JSON.unpack(value) }
55
56
57
58
59
60
61
62
63
64
            .branch(
                Predicate { _, value -> value.containsKey(JSON.record) },
                Predicate { _, value -> value.containsKey(JSON.institution) },
                Predicate { _, value -> value.containsKey(JSON.recordSet) },
                Predicate { _, _ -> true }
            )

        val recordStream = branchedStream[0]
            .mapValues { readOnlyKey, value ->
                try {
65
66
67
68
                    Pair(
                        searchDocTransform.transform(value),
                        Report(readOnlyKey, "SUCCESS", "Transformed message into search doc.")
                    )
69
70
71
72
73
74
75
76
                } catch (ex: InvalidInputException) {
                    Pair(Default.searchDoc, Report(readOnlyKey, "FAILURE", ex.localizedMessage))
                }
            }

        outputStreams(recordStream)

        val institutionStream = branchedStream[1]
Jonas Waeber's avatar
Jonas Waeber committed
77
78
            .mapValues { readOnlyKey, value ->
                try {
79
80
81
82
                    Pair(
                        institutionSearchDoc.transform(readOnlyKey, value),
                        Report(readOnlyKey, "SUCCESS", "Transformed message into search doc.")
                    )
Jonas Waeber's avatar
Jonas Waeber committed
83
                } catch (ex: InvalidInputException) {
84
                    Pair(Default.institutionSearchDoc, Report(readOnlyKey, "FAILURE", ex.localizedMessage))
Jonas Waeber's avatar
Jonas Waeber committed
85
86
                }
            }
87
        outputStreams(institutionStream)
88
89
90
91
92
93
94
95
96
97

        branchedStream[3]
            .mapValues { readOnlyKey, value ->
                Report(
                    readOnlyKey,
                    ReportStatus.fatal,
                    "No record, memobase institution or record set present in input data: $value."
                )
            }
            .to(reportTopic)
98
99
100
101
102
        return builder.build()
    }

    private fun outputStreams(stream: KStream<String, Pair<Schema, Report>>) {
        stream
103
            .mapValues { value -> value.second.toJson() }
Jonas Waeber's avatar
Jonas Waeber committed
104
105
            .to(reportTopic)

106
        stream
Jonas Waeber's avatar
Jonas Waeber committed
107
            .filterNot { _, value -> value.second.status == "FAILURE" }
108
            .mapValues { value -> value.first }
Jonas Waeber's avatar
Jonas Waeber committed
109
            .mapValues { value ->
110
111
112
                val out = StringWriter()
                jsonWriter.writeValue(out, value)
                out.toString()
Jonas Waeber's avatar
Jonas Waeber committed
113
            }
Jonas Waeber's avatar
Jonas Waeber committed
114
115
116
            .to(settings.outputTopic)
    }
}