In order to mitigate against the brute force attacks against Gitlab accounts, we are moving to all edu-ID Logins. We would like to remind you to link your account with your edu-id. Login will be possible only by edu-ID after November 30, 2021. Here you can find the instructions for linking your account.

If you don't have a SWITCH edu-ID, you can create one with this guide here

kind regards

KafkaTopology.kt 2.33 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

class KafkaTopology(private val settings: SettingsLoader) {
    private val log = LogManager.getLogger("StreamsProcessing")
Jonas Waeber's avatar
Jonas Waeber committed
34
    private val transformer = Transform()
Jonas Waeber's avatar
Jonas Waeber committed
35

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

        return builder.build()
    }

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

Jonas Waeber's avatar
Jonas Waeber committed
59
60
61
62
63
64
65
    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.")
        }

66

Jonas Waeber's avatar
Jonas Waeber committed
67
68
    }
}