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
21
/*
 * 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

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

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

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

44
    private val searchDocTransform = SearchDocTransform(settings.appSettings.getProperty(KEYS.mediaUrlPropName))
45
46
    private val institutionSearchDoc = InstitutionSearchDocBuilder()

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(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
86
87
88
        outputStreams(institutionStream)



        return builder.build()
    }

    private fun outputStreams(stream: KStream<String, Pair<Schema, Report>>) {
        stream
Jonas Waeber's avatar
Jonas Waeber committed
89
90
91
            .map { _, value -> KeyValue(value.second.id, value.second.toJson()) }
            .to(reportTopic)

92
        stream
Jonas Waeber's avatar
Jonas Waeber committed
93
            .filterNot { _, value -> value.second.status == "FAILURE" }
94
            .map { _, value -> KeyValue(value.first.id, value.first) }
Jonas Waeber's avatar
Jonas Waeber committed
95
96
97
98
99
            .mapValues { value ->
                val writer = StringWriter()
                ObjectMapper().registerKotlinModule().writeValue(writer, value)
                writer.toString()
            }
Jonas Waeber's avatar
Jonas Waeber committed
100
101
102
            .to(settings.outputTopic)
    }
}