KafkaTopology.kt 2.6 KB
Newer Older
1
/*
Jonas Waeber's avatar
Jonas Waeber committed
2
 * Copyright (C) 2020-2021 Memoriav
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * 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 ch.memobase

19
20
21
import ch.memobase.reporting.Report
import ch.memobase.settings.HeaderExtractionTransformSupplier
import ch.memobase.settings.SettingsLoader
22
import com.beust.klaxon.Klaxon
Jonas Waeber's avatar
Jonas Waeber committed
23
import com.beust.klaxon.KlaxonException
24
25
import org.apache.kafka.streams.KeyValue
import org.apache.kafka.streams.StreamsBuilder
26
import org.apache.logging.log4j.LogManager
27
28
29
30
31
32
import java.io.StringReader

class KafkaTopology(
    private val settings: SettingsLoader
) {
    private val klaxon = Klaxon()
Jonas Waeber's avatar
Jonas Waeber committed
33
    private val log = LogManager.getLogger(this::class.java)
34
35
36
37
38
    fun prepare(): StreamsBuilder {
        val builder = StreamsBuilder()

        builder.stream<String, String>(settings.inputTopic)
            .transformValues(HeaderExtractionTransformSupplier<String>())
Jonas Waeber's avatar
Jonas Waeber committed
39
            .flatMapValues { value ->
40
                try {
Jonas Waeber's avatar
Jonas Waeber committed
41
42
43
44
45
46
47
                    klaxon.parse<Report>(StringReader(value.first)).let {
                        if (it != null) {
                            listOf(IndexReport(it, value.second))
                        } else {
                            emptyList()
                        }
                    }
48
                } catch (ex: ClassCastException) {
Jonas Waeber's avatar
Jonas Waeber committed
49
50
51
52
53
                    log.error(ex.localizedMessage + " in " + value.first)
                    emptyList<IndexReport>()
                } catch (ex: KlaxonException) {
                    log.error(ex.localizedMessage + " in " + value.first)
                    emptyList<IndexReport>()
Jonas Waeber's avatar
Jonas Waeber committed
54
55
56
                } catch (ex: NullPointerException) {
                    log.error(ex.localizedMessage + " in " + value.first)
                    emptyList<IndexReport>()
57
                }
Jonas Waeber's avatar
Jonas Waeber committed
58
            }
59
60
61
62
63
64
            .map { key, value ->
                KeyValue(
                    "$key-${value.report.step}-${value.metadata.sessionId}",
                    klaxon.toJsonString(value)
                )
            }
65
66
67
68
            .to(settings.outputTopic)
        return builder
    }
}