mucking about with the kafka processor
This commit is contained in:
parent
a63edb1c61
commit
da9cc6398f
3 changed files with 95 additions and 3 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id("dev.adamko.factoriowebmap.archetype.kotlin-jvm")
|
id("dev.adamko.factoriowebmap.archetype.kotlin-jvm")
|
||||||
|
kotlin("plugin.serialization")
|
||||||
application
|
application
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -19,6 +19,8 @@ dependencies {
|
||||||
|
|
||||||
implementation("org.apache.kafka:kafka-streams:3.0.0")
|
implementation("org.apache.kafka:kafka-streams:3.0.0")
|
||||||
|
|
||||||
|
implementation(libs.kotlinx.serialization)
|
||||||
|
|
||||||
implementation(platform(libs.http4k.bom))
|
implementation(platform(libs.http4k.bom))
|
||||||
implementation(libs.http4k.core)
|
implementation(libs.http4k.core)
|
||||||
implementation(libs.http4k.cloudnative)
|
implementation(libs.http4k.cloudnative)
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,47 @@
|
||||||
package dev.adamko.factorioevents.processor
|
package dev.adamko.factorioevents.processor
|
||||||
|
|
||||||
import dev.adamko.factorioevents.processor.config.ApplicationProperties
|
import dev.adamko.factorioevents.processor.config.ApplicationProperties
|
||||||
|
import dev.adamko.factorioevents.processor.model.FactorioServerLogRecord
|
||||||
|
import dev.adamko.factorioevents.processor.model.LuaSurfaceData
|
||||||
|
import java.time.Duration
|
||||||
|
import org.apache.kafka.common.serialization.Serdes
|
||||||
import org.apache.kafka.streams.KafkaStreams
|
import org.apache.kafka.streams.KafkaStreams
|
||||||
|
import org.apache.kafka.streams.KeyValue
|
||||||
import org.apache.kafka.streams.StreamsBuilder
|
import org.apache.kafka.streams.StreamsBuilder
|
||||||
|
import org.apache.kafka.streams.kstream.Branched
|
||||||
|
import org.apache.kafka.streams.kstream.Consumed
|
||||||
|
import org.apache.kafka.streams.kstream.Named
|
||||||
|
import org.apache.kafka.streams.processor.TopicNameExtractor
|
||||||
|
import org.http4k.format.Jackson
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
|
|
||||||
val appProps = ApplicationProperties()
|
val appProps = ApplicationProperties()
|
||||||
|
|
||||||
val topology = StreamsBuilder().build()
|
val builder = StreamsBuilder()
|
||||||
|
|
||||||
|
|
||||||
|
builder.stream(
|
||||||
|
"factorio-server-log",
|
||||||
|
Consumed.with(Serdes.String(), Serdes.String())
|
||||||
|
)
|
||||||
|
.mapValues { readOnlyKey, value ->
|
||||||
|
println("Mapping $readOnlyKey:$value")
|
||||||
|
Jackson.asA<FactorioServerLogRecord<*>>(value)
|
||||||
|
}
|
||||||
|
.to(TopicNameExtractor { key, value, recordContext ->
|
||||||
|
value.data.objectName
|
||||||
|
})
|
||||||
|
|
||||||
|
val topology = builder.build()
|
||||||
val properties = appProps.kafkaConfig
|
val properties = appProps.kafkaConfig
|
||||||
|
|
||||||
val streams = KafkaStreams(topology, properties)
|
val streams = KafkaStreams(topology, properties)
|
||||||
|
|
||||||
streams.setUncaughtExceptionHandler(StreamsExceptionHandler())
|
streams.setUncaughtExceptionHandler(StreamsExceptionHandler())
|
||||||
|
|
||||||
}
|
Runtime.getRuntime().addShutdownHook(Thread { streams.close(Duration.ofSeconds(10)) })
|
||||||
|
|
||||||
|
streams.start()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,62 @@
|
||||||
|
package dev.adamko.factorioevents.processor.model
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty
|
||||||
|
import com.fasterxml.jackson.annotation.JsonTypeInfo
|
||||||
|
import com.fasterxml.jackson.annotation.JsonTypeName
|
||||||
|
import com.fasterxml.jackson.annotation.JsonValue
|
||||||
|
import java.math.BigDecimal
|
||||||
|
import java.math.BigInteger
|
||||||
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
data class FactorioServerLogRecord<DataType : FactorioObjectData>(
|
||||||
|
@JsonProperty("mod_version")
|
||||||
|
val modVersion: String,
|
||||||
|
@JsonProperty("tick")
|
||||||
|
val tick: BigInteger,
|
||||||
|
@JsonProperty("event_type")
|
||||||
|
val eventType: String,
|
||||||
|
@JsonProperty("data")
|
||||||
|
val data: DataType,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@JsonTypeInfo(
|
||||||
|
use = JsonTypeInfo.Id.NAME,
|
||||||
|
include = JsonTypeInfo.As.PROPERTY,
|
||||||
|
property = "object_name",
|
||||||
|
defaultImpl = Void::class, // equivalent to null
|
||||||
|
visible = true,
|
||||||
|
)
|
||||||
|
sealed class FactorioObjectData(
|
||||||
|
@JsonProperty("object_name")
|
||||||
|
val objectName: String
|
||||||
|
)
|
||||||
|
|
||||||
|
@JsonTypeName("LuaSurface")
|
||||||
|
class LuaSurfaceData(
|
||||||
|
@JsonProperty("name")
|
||||||
|
val name: String,
|
||||||
|
@JsonProperty("index")
|
||||||
|
val index: Int,
|
||||||
|
@JsonProperty("daytime")
|
||||||
|
val daytime: BigDecimal,
|
||||||
|
) : FactorioObjectData("LuaSurface")
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
{
|
||||||
|
"tick": 1144860,
|
||||||
|
"object_name": "LuaSurface",
|
||||||
|
"event_type": "on_tick",
|
||||||
|
"data": {
|
||||||
|
"name": "nauvis",
|
||||||
|
"index": 1,
|
||||||
|
"daytime": 0.4942800000457983
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
Loading…
Add table
Add a link
Reference in a new issue