KafkaTopology.kt 2.79 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
/*
 * mapper-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.beust.klaxon.Klaxon
import org.apache.kafka.streams.KeyValue
Jonas Waeber's avatar
Jonas Waeber committed
23
24
25
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
26
27
import org.memobase.builder.ResourceBuilder
import org.memobase.mapping.MappingConfig
Jonas Waeber's avatar
Jonas Waeber committed
28
import org.memobase.mapping.exceptions.InvalidMappingException
Jonas Waeber's avatar
Jonas Waeber committed
29
30
31
32
33
import org.memobase.settings.SettingsLoader

class KafkaTopology(private val settings: SettingsLoader
) {
    private val log = LogManager.getLogger("KafkaTopology")
Jonas Waeber's avatar
Jonas Waeber committed
34
    private val config = MappingConfig(settings.appSettings.getProperty("configs"))
Jonas Waeber's avatar
Jonas Waeber committed
35
36
37
38

    fun build(): Topology {
        val builder = StreamsBuilder()

Jonas Waeber's avatar
Jonas Waeber committed
39
40
41
        val stream = builder.stream<String, String>(settings.inputTopic)

        stream.flatMapValues { value -> parseJsonFile(value) }
Jonas Waeber's avatar
Jonas Waeber committed
42
            .flatMapValues { value -> buildResources(value) }
Jonas Waeber's avatar
Jonas Waeber committed
43
44
            .flatMap { _, value -> writeResource(value)  }
            .to(settings.outputTopic)
Jonas Waeber's avatar
Jonas Waeber committed
45
46
        return builder.build()
    }
Jonas Waeber's avatar
Jonas Waeber committed
47
48


Jonas Waeber's avatar
Jonas Waeber committed
49
50
51
52
53
54
55
56
57
    private fun parseJsonFile(message: String): List<Map<String, String>> {
        Klaxon().parse<Map<String, String>>(message).let {
            return if (it != null) {
                listOf(it)
            } else {
                emptyList()
            }
        }
    }
Jonas Waeber's avatar
Jonas Waeber committed
58

Jonas Waeber's avatar
Jonas Waeber committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    private fun buildResources(value: Map<String, String>): List<ResourceBuilder> {
        return try {
            listOf(ResourceBuilder(
                config.uriField,
                config.recordType,
                config.recordFieldMappers,
                config.physicalObjectFieldMappers,
                config.digitalObjectFieldMappers,
                value,
                settings.appSettings.getProperty("institutionId"),
                settings.appSettings.getProperty("recordSetId")
            ))
        } catch (ex: InvalidMappingException) {
            log.error(ex.localizedMessage)
            emptyList()
        }
Jonas Waeber's avatar
Jonas Waeber committed
75
    }
Jonas Waeber's avatar
Jonas Waeber committed
76

Jonas Waeber's avatar
Jonas Waeber committed
77
78
    private fun writeResource(value: ResourceBuilder): List<KeyValue<String, String>> {
        return value.write().map { return@map KeyValue(it.first, it.second) }
Jonas Waeber's avatar
Jonas Waeber committed
79
    }
Jonas Waeber's avatar
Jonas Waeber committed
80
}