KafkaTopology.kt 3.96 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
23
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import java.io.StringWriter
24
import org.apache.kafka.streams.KeyValue
Jonas Waeber's avatar
Jonas Waeber committed
25
26
import org.apache.kafka.streams.StreamsBuilder
import org.apache.kafka.streams.Topology
27
28
import org.apache.kafka.streams.kstream.KStream
import org.apache.kafka.streams.kstream.Predicate
Jonas Waeber's avatar
Jonas Waeber committed
29
import org.apache.logging.log4j.LogManager
30
import org.memobase.helpers.Default
Jonas Waeber's avatar
Jonas Waeber committed
31
import org.memobase.helpers.JSON
32
import org.memobase.helpers.KEYS
Jonas Waeber's avatar
Jonas Waeber committed
33
import org.memobase.model.Report
34
import org.memobase.model.Schema
Jonas Waeber's avatar
Jonas Waeber committed
35
36
37
import org.memobase.settings.SettingsLoader

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
43
    private val searchDocTransform = SearchDocTransform(settings.appSettings.getProperty(KEYS.SettingsProps.mediaUrl))
    private val institutionSearchDoc = InstitutionSearchDocBuilder(settings.appSettings.getProperty(KEYS.SettingsProps.institutionTypeLabelsPath))
44

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

Jonas Waeber's avatar
Jonas Waeber committed
47
48
49
    fun build(): Topology {
        val builder = StreamsBuilder()
        val stream = builder.stream<String, String>(settings.inputTopic)
50
        val branchedStream = stream
Jonas Waeber's avatar
Jonas Waeber committed
51
52
            .flatMapValues { value -> JSON.parse(value) }
            .mapValues { value -> JSON.unpack(value) }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
            .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 {
                    Pair(searchDocTransform.transform(value), Report(readOnlyKey, "SUCCESS", "Transformed message into search doc."))
                } catch (ex: InvalidInputException) {
                    Pair(Default.searchDoc, Report(readOnlyKey, "FAILURE", ex.localizedMessage))
                }
            }

        outputStreams(recordStream)

        val institutionStream = branchedStream[1]
Jonas Waeber's avatar
Jonas Waeber committed
72
73
            .mapValues { readOnlyKey, value ->
                try {
74
                    Pair(institutionSearchDoc.transform(readOnlyKey, value), Report(readOnlyKey, "SUCCESS", "Transformed message into search doc."))
Jonas Waeber's avatar
Jonas Waeber committed
75
                } catch (ex: InvalidInputException) {
76
                    Pair(Default.institutionSearchDoc, Report(readOnlyKey, "FAILURE", ex.localizedMessage))
Jonas Waeber's avatar
Jonas Waeber committed
77
78
79
                }
            }

80
81
82
83
84
85
        outputStreams(institutionStream)
        return builder.build()
    }

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

89
        stream
Jonas Waeber's avatar
Jonas Waeber committed
90
            .filterNot { _, value -> value.second.status == "FAILURE" }
91
            .mapValues { value -> value.first }
Jonas Waeber's avatar
Jonas Waeber committed
92
            .mapValues { value ->
93
94
95
                val out = StringWriter()
                jsonWriter.writeValue(out, value)
                out.toString()
Jonas Waeber's avatar
Jonas Waeber committed
96
            }
Jonas Waeber's avatar
Jonas Waeber committed
97
98
99
            .to(settings.outputTopic)
    }
}