KafkaTopology.kt 2.41 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
/*
Jonas Waeber's avatar
Jonas Waeber committed
2
 * Drupal Sync Service
Jonas Waeber's avatar
Jonas Waeber committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 * 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.Klaxon
import com.beust.klaxon.KlaxonException
Jonas Waeber's avatar
Jonas Waeber committed
23
24
import org.apache.jena.rdf.model.Model
import org.apache.kafka.streams.KeyValue
Jonas Waeber's avatar
Jonas Waeber committed
25
26
27
import org.apache.kafka.streams.StreamsBuilder
import org.apache.kafka.streams.Topology
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
28
import org.memobase.model.*
Jonas Waeber's avatar
Jonas Waeber committed
29
import org.memobase.settings.SettingsLoader
Jonas Waeber's avatar
Jonas Waeber committed
30
import java.io.StringReader
Jonas Waeber's avatar
Jonas Waeber committed
31
32
33
34

class KafkaTopology(private val settings: SettingsLoader) {
    private val log = LogManager.getLogger("StreamsProcessing")

35
    private val municipalities = Helpers.getMunicipalities()
Jonas Waeber's avatar
Jonas Waeber committed
36
    private val transformer = Transform(municipalities)
Jonas Waeber's avatar
Jonas Waeber committed
37

Jonas Waeber's avatar
Jonas Waeber committed
38
39
40
41
42
    fun build(): Topology {
        val builder = StreamsBuilder()
        val stream = builder.stream<String, String>(settings.inputTopic)
        stream
            .flatMapValues { value -> parseJson(value) }
43
            .mapValues { value -> transformJson(value) }
Jonas Waeber's avatar
Jonas Waeber committed
44
            .map { _, value -> transformer.write(value.first, value.second) }
Jonas Waeber's avatar
Jonas Waeber committed
45
46
47
48
49
            .to(settings.outputTopic)

        return builder.build()
    }

Jonas Waeber's avatar
Jonas Waeber committed
50
    private fun parseJson(data: String): List<Input> {
Jonas Waeber's avatar
Jonas Waeber committed
51
        return try {
Jonas Waeber's avatar
Jonas Waeber committed
52
            val int = Klaxon().parse<Input>(StringReader(data))
53
54
            return if (int == null) emptyList()
            else listOf(int)
Jonas Waeber's avatar
Jonas Waeber committed
55
56
57
58
59
60
        } catch (ex: KlaxonException) {
            log.error("Failed to parse source: $data.")
            emptyList()
        }
    }

Jonas Waeber's avatar
Jonas Waeber committed
61
62
63
64
65
66
67
    private fun transformJson(input: Input): Pair<String, Model> {
        return when (input) {
            is Institution -> transformer.createInstitution(input)
            is RecordSet -> transformer.createRecordSet(input)
            else -> throw Exception("Unknown Type Input.")
        }

68

Jonas Waeber's avatar
Jonas Waeber committed
69
70
    }
}