org.apache.kafka.streams.KafkaStreams Scala Examples
The following examples show how to use org.apache.kafka.streams.KafkaStreams.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: EmbeddedKafkaStreamsWithSchemaRegistry.scala From scalatest-embedded-kafka with MIT License | 5 votes |
package net.manub.embeddedkafka.schemaregistry.streams import net.manub.embeddedkafka.UUIDs import net.manub.embeddedkafka.schemaregistry.EmbeddedKafkaWithSchemaRegistry.consumerConfigForSchemaRegistry import net.manub.embeddedkafka.schemaregistry.{ EmbeddedKafkaConfigWithSchemaRegistry, EmbeddedKafkaWithSchemaRegistry } import net.manub.embeddedkafka.streams.TestStreamsConfig import org.apache.kafka.streams.{KafkaStreams, Topology} // TODO: need to find a better way of not duplicating this code from the kafka-streams module def runStreams[T](topicsToCreate: Seq[String], topology: Topology, extraConfig: Map[String, AnyRef] = Map.empty)(block: => T)( implicit config: EmbeddedKafkaConfigWithSchemaRegistry): T = withRunningKafka { topicsToCreate.foreach(topic => createCustomTopic(topic)) val streamId = UUIDs.newUuid().toString val streams = new KafkaStreams( topology, streamConfig(streamId, extraConfig ++ consumerConfigForSchemaRegistry)) streams.start() try { block } finally { streams.close() } }(config) }
Example 2
Source File: EmbeddedKafkaStreams.scala From scalatest-embedded-kafka with MIT License | 5 votes |
package net.manub.embeddedkafka.streams import net.manub.embeddedkafka.{EmbeddedKafka, EmbeddedKafkaConfig, UUIDs} import org.apache.kafka.streams.{KafkaStreams, Topology} def runStreams[T](topicsToCreate: Seq[String], topology: Topology, extraConfig: Map[String, AnyRef] = Map.empty)(block: => T)( implicit config: EmbeddedKafkaConfig): T = withRunningKafka { topicsToCreate.foreach(topic => createCustomTopic(topic)) val streamId = UUIDs.newUuid().toString val streams = new KafkaStreams(topology, streamConfig(streamId, extraConfig)) streams.start() try { block } finally { streams.close() } }(config) }
Example 3
Source File: AggregationExampleWithSAM.scala From kafka-streams-scala-examples with Apache License 2.0 | 5 votes |
package com.knoldus.kafka.examples import java.util.Properties import org.apache.kafka.common.serialization.Serdes import org.apache.kafka.streams.kstream._ import org.apache.kafka.streams.{KafkaStreams, StreamsConfig} import scala.collection.JavaConverters._ object AggregationExampleWithSAM { def main(args: Array[String]): Unit = { val config = { val properties = new Properties() properties.put(StreamsConfig.APPLICATION_ID_CONFIG, "stream-application") properties.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092") properties.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass) properties.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass) properties } val stringSerde = Serdes.String() val longSerde = Serdes.Long() val builder = new KStreamBuilder() val originalStream = builder.stream("SourceTopic") //Works only with Scala 2.12.x val mappedStream: KTable[String, java.lang.Long] = originalStream.flatMapValues((value: String) => value.toLowerCase.split("\\W+").toIterable.asJava) .groupBy((_, word) => word) .count("Counts") mappedStream.to(stringSerde, longSerde, "SinkTopic") val streams = new KafkaStreams(builder, config) streams.start() } }
Example 4
Source File: AggregationExample.scala From kafka-streams-scala-examples with Apache License 2.0 | 5 votes |
package com.knoldus.kafka.examples import java.util.Properties import org.apache.kafka.common.serialization.Serdes import org.apache.kafka.streams.kstream.{KStreamBuilder, KeyValueMapper, ValueMapper} import org.apache.kafka.streams.{KafkaStreams, StreamsConfig} import scala.collection.JavaConverters._ object AggregationExample { def main(args: Array[String]): Unit = { val config = { val properties = new Properties() properties.put(StreamsConfig.APPLICATION_ID_CONFIG, "stream-application") properties.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092") properties.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass) properties.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass) properties } val stringSerde = Serdes.String() val longSerde = Serdes.Long() val builder = new KStreamBuilder() val originalStream = builder.stream("SourceTopic") val mappedStream = originalStream.flatMapValues[String] { new ValueMapper[String, java.lang.Iterable[java.lang.String]]() { override def apply(value: String): java.lang.Iterable[java.lang.String] = { value.toLowerCase.split("\\W+").toIterable.asJava } } }.groupBy { new KeyValueMapper[String, String, String]() { override def apply(key: String, word: String): String = word } }.count("Counts") mappedStream.to(stringSerde, longSerde, "SinkTopic") val streams = new KafkaStreams(builder, config) streams.start() } }
Example 5
Source File: EmbeddedKafkaStreams.scala From embedded-kafka with MIT License | 5 votes |
package net.manub.embeddedkafka.streams import java.util.Properties import net.manub.embeddedkafka.ops.AdminOps import net.manub.embeddedkafka.{ EmbeddedKafka, EmbeddedKafkaConfig, EmbeddedKafkaSupport, UUIDs } import org.apache.kafka.streams.{KafkaStreams, Topology} import scala.jdk.CollectionConverters._ def runStreams[T]( topicsToCreate: Seq[String], topology: Topology, extraConfig: Map[String, AnyRef] = Map.empty )(block: => T)(implicit config: C): T = withRunningKafka { topicsToCreate.foreach(topic => createCustomTopic(topic)) val streamId = UUIDs.newUuid().toString val streams = new KafkaStreams( topology, map2Properties(streamsConfig.config(streamId, extraConfig)) ) streams.start() try { block } finally { streams.close() } }(config) private def map2Properties(map: Map[String, AnyRef]): Properties = { val props = new Properties props.putAll(map.asJava) props } } object EmbeddedKafkaStreams extends EmbeddedKafkaStreams
Example 6
Source File: WordCount.scala From kafka-streams with Apache License 2.0 | 5 votes |
import java.time.Duration import java.util.Properties import org.apache.kafka.streams.kstream.Materialized import org.apache.kafka.streams.scala.ImplicitConversions._ import org.apache.kafka.streams.scala._ import org.apache.kafka.streams.scala.kstream._ import org.apache.kafka.streams.{KafkaStreams, StreamsConfig} object WordCount extends App { import Serdes._ val props: Properties = { val p = new Properties() p.put(StreamsConfig.APPLICATION_ID_CONFIG, "wordcount-modified") p.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092") p } val builder: StreamsBuilder = new StreamsBuilder val textLines: KStream[String, String] = builder.stream[String, String]("text_lines") val wordCounts: KTable[String, Long] = textLines .flatMapValues(textLine => textLine.toLowerCase.split("\\W+")) .groupBy((_, word) => word) .count() wordCounts.toStream.to("word_count_results") val streams: KafkaStreams = new KafkaStreams(builder.build(), props) streams.start() sys.ShutdownHookThread { streams.close(Duration.ofSeconds(10)) } }
Example 7
Source File: WordCountTestable.scala From kafka-streams with Apache License 2.0 | 5 votes |
package com.supergloo import java.time.Duration import java.util.Properties import org.apache.kafka.streams.kstream.Materialized import org.apache.kafka.streams.scala.ImplicitConversions._ import org.apache.kafka.streams.{KafkaStreams, StreamsConfig, Topology} import org.apache.kafka.streams.scala.{Serdes, StreamsBuilder} import org.apache.kafka.streams.scala.kstream.{KStream, KTable} class WordCountTestable { import Serdes._ val props: Properties = { val p = new Properties() p.put(StreamsConfig.APPLICATION_ID_CONFIG, "wordcount-modified") p.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092") p } def countNumberOfWords(inputTopic: String, outputTopic: String, storeName: String): Topology = { val builder: StreamsBuilder = new StreamsBuilder val textLines: KStream[String, String] = builder.stream[String, String](inputTopic) val wordCounts: KTable[String, Long] = textLines .flatMapValues(textLine => textLine.toLowerCase.split("\\W+")) .groupBy((_, word) => word) .count()(Materialized.as("counts-store")) wordCounts.toStream.to(outputTopic) builder.build() } def toLowerCaseStream(inputTopic: String, outputTopic: String): Topology = { val builder: StreamsBuilder = new StreamsBuilder() val textLines: KStream[String, String] = builder.stream(inputTopic) val wordCounts: KStream[String, String] = textLines .mapValues(textLine => textLine.toLowerCase) wordCounts.to(outputTopic) builder.build() } } object WordCountTestable extends WordCountTestable { def main(args: Array[String]): Unit = { val builder: Topology = countNumberOfWords("input-topic", "output-topic", "counts-store") val streams: KafkaStreams = new KafkaStreams(builder, props) streams.start() sys.ShutdownHookThread { streams.close(Duration.ofSeconds(10)) } } }
Example 8
Source File: Application.scala From kafka-serde-scala with Apache License 2.0 | 5 votes |
package io.github.azhur.kafkaserdescala.example import java.util.Properties import io.github.azhur.kafkaserdecirce.CirceSupport import org.apache.kafka.clients.consumer.ConsumerConfig import org.apache.kafka.streams.{ KafkaStreams, StreamsConfig, Topology } import org.apache.kafka.streams.scala.StreamsBuilder object Application extends App with CirceSupport { import io.circe.generic.auto._ import org.apache.kafka.streams.scala.Serdes._ import org.apache.kafka.streams.scala.ImplicitConversions._ case class User(id: Long, name: String, age: Int) val topology = buildTopology("input_users", "output_users") val streamingApp = new KafkaStreams(topology, streamProperties()) streamingApp.start() sys.addShutdownHook({ streamingApp.close() }) def buildTopology(inputTopic: String, outputTopic: String): Topology = { val streamsBuilder = new StreamsBuilder() streamsBuilder .stream[String, User](inputTopic) .filter((_, user) => user.age > 18) .to(outputTopic) streamsBuilder.build() } def streamProperties(): Properties = { val streamsConfiguration = new Properties streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, "test-app") streamsConfiguration.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092") streamsConfiguration.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, Topology.AutoOffsetReset.EARLIEST.toString.toLowerCase) streamsConfiguration } }
Example 9
Source File: MetadataService.scala From kafka-with-akka-streams-kafka-streams-tutorial with Apache License 2.0 | 5 votes |
package com.lightbend.scala.kafkastreams.queriablestate import java.net.InetAddress import java.util import com.lightbend.java.configuration.kafka.ApplicationKafkaParameters import com.lightbend.scala.kafkastreams.store.HostStoreInfo import org.apache.kafka.common.TopicPartition import org.apache.kafka.streams.KafkaStreams import org.apache.kafka.streams.state.{HostInfo, StreamsMetadata} import scala.collection.JavaConverters._ def streamsMetadataForStore(store: String, port: Int): util.List[HostStoreInfo] = { // Get metadata for all of the instances of this Kafka Streams application hosting the store val metadata = streams.allMetadataForStore(store).asScala.toSeq match{ case list if !list.isEmpty => list case _ => Seq(new StreamsMetadata( new HostInfo("localhost", port), new util.HashSet[String](util.Arrays.asList(ApplicationKafkaParameters.STORE_NAME)), util.Collections.emptySet[TopicPartition])) } mapInstancesToHostStoreInfo(metadata) } private def mapInstancesToHostStoreInfo(metadatas: Seq[StreamsMetadata]) = metadatas.map(convertMetadata(_)).asJava private def convertMetadata(metadata: StreamsMetadata) : HostStoreInfo = { val currentHost = metadata.host match{ case host if host equalsIgnoreCase("localhost") => try{InetAddress.getLocalHost.getHostAddress} catch {case t: Throwable => ""} case host => host } new HostStoreInfo(currentHost, metadata.port, metadata.stateStoreNames.asScala.toSeq) } }
Example 10
Source File: RestServiceStore.scala From kafka-with-akka-streams-kafka-streams-tutorial with Apache License 2.0 | 5 votes |
package com.lightbend.scala.kafkastreams.queriablestate.withstore import javax.ws.rs.NotFoundException import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.stream.ActorMaterializer import akka.util.Timeout import com.lightbend.scala.kafkastreams.queriablestate.MetadataService import com.lightbend.java.configuration.kafka.ApplicationKafkaParameters._ import com.lightbend.scala.kafkastreams.store.StoreState import com.lightbend.scala.kafkastreams.store.store.custom.ModelStateStoreType import de.heikoseeberger.akkahttpjackson.JacksonSupport import org.apache.kafka.streams.KafkaStreams import org.apache.kafka.streams.state.QueryableStoreTypes import scala.concurrent.duration._ object RestServiceStore { implicit val system = ActorSystem("ModelServing") implicit val materializer = ActorMaterializer() implicit val executionContext = system.dispatcher implicit val timeout = Timeout(10.seconds) val host = "127.0.0.1" val port = 8888 def startRestProxy(streams: KafkaStreams, port: Int, storeType : String) = { val routes: Route = QueriesResource.storeRoutes(streams, port, storeType) Http().bindAndHandle(routes, host, port) map { binding => println(s"Starting models observer on port ${binding.localAddress}") } recover { case ex => println(s"Models observer could not bind to $host:$port - ${ex.getMessage}") } } } object QueriesResource extends JacksonSupport { private val customStoreType = new ModelStateStoreType() private val standardStoreType = QueryableStoreTypes.keyValueStore[Integer,StoreState] def storeRoutes(streams: KafkaStreams, port : Int, storeType : String): Route = { val metadataService = new MetadataService(streams) get { pathPrefix("state") { path("instances") { complete( metadataService.streamsMetadataForStore(STORE_NAME, port) ) } ~ path("value") { storeType match { case "custom" => val store = streams.store(STORE_NAME, customStoreType) if (store == null) throw new NotFoundException complete(store.getCurrentServingInfo) case _ => val store = streams.store(STORE_NAME, standardStoreType) if (store == null) throw new NotFoundException complete(store.get(STORE_ID).currentState) } } } } } }
Example 11
Source File: RestServiceInMemory.scala From kafka-with-akka-streams-kafka-streams-tutorial with Apache License 2.0 | 5 votes |
package com.lightbend.scala.kafkastreams.queriablestate.inmemory import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.stream.ActorMaterializer import akka.util.Timeout import com.lightbend.scala.kafkastreams.store.StoreState import de.heikoseeberger.akkahttpjackson.JacksonSupport import org.apache.kafka.streams.KafkaStreams import com.lightbend.scala.modelServer.model.ModelToServeStats import scala.concurrent.duration._ object RestServiceInMemory{ implicit val system = ActorSystem("ModelServing") implicit val materializer = ActorMaterializer() implicit val executionContext = system.dispatcher implicit val timeout = Timeout(10.seconds) val host = "127.0.0.1" val port = 8888 val routes: Route = QueriesResource.storeRoutes() // Surf to http://localhost:8888/state/instances for the list of currently deployed instances. // Then surf to http://localhost:8888/state/value for the current state of execution for a given model. def startRestProxy(streams: KafkaStreams, port: Int) = { Http().bindAndHandle(routes, host, port) map { binding => println(s"Starting models observer on port ${binding.localAddress}") } recover { case ex => println(s"Models observer could not bind to $host:$port - ${ex.getMessage}") } } } // Surf to http://localhost:8888/state/value object QueriesResource extends JacksonSupport { def storeRoutes(): Route = get { pathPrefix("state") { path("value") { val info: ModelToServeStats = StoreState().currentState.getOrElse(ModelToServeStats.empty) complete(info) } } } }
Example 12
Source File: StreamProcessorApp.scala From event-sourcing-kafka-streams with MIT License | 5 votes |
package org.amitayh.invoices.streamprocessor import java.util.Properties import java.util.concurrent.CountDownLatch import org.amitayh.invoices.common.Config import org.apache.kafka.streams.KafkaStreams.State import org.apache.kafka.streams.{KafkaStreams, StreamsConfig, Topology} import org.log4s.getLogger trait StreamProcessorApp extends App { def appId: String def topology: Topology private val logger = getLogger private val latch = new CountDownLatch(1) private val streams: KafkaStreams = { val props = new Properties props.put(StreamsConfig.APPLICATION_ID_CONFIG, appId) props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, Config.BootstrapServers) props.put(StreamsConfig.PROCESSING_GUARANTEE_CONFIG, StreamsConfig.EXACTLY_ONCE) new KafkaStreams(topology, props) } streams.setStateListener((newState: State, oldState: State) => { logger.info(s"$oldState -> $newState") }) streams.setUncaughtExceptionHandler((_: Thread, e: Throwable) => { logger.error(e)(s"Exception was thrown in stream processor $appId") latch.countDown() }) def start(): Unit = { logger.info("Starting...") streams.start() sys.ShutdownHookThread(close()) latch.await() } def close(): Unit = { logger.info("Shutting down...") streams.close() } start() }