Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ import java.lang.{Iterable => JIterable}
*/
object FunctionConversions {

implicit private[scala] class ForeachActionFromFunction[K, V](val p: (K, V) => Unit) extends AnyVal {
def asForeachAction: ForeachAction[K, V] = new ForeachAction[K, V] {
override def apply(key: K, value: V): Unit = p(key, value)
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, this is a public interface change, and would require a KIP.

Note that we don't really need the implicit; it just makes the internal implementation nicer. You could instead do:

def foreach(action: (K, V) => Unit): Unit =
    inner.foreach(new ForeachAction[K, V] {
      override def apply(key: K, value: V): Unit = action(key, value)
    })

right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we could.

implicit class PredicateFromFunction[K, V](val p: (K, V) => Boolean) extends AnyVal {
def asPredicate: Predicate[K, V] = new Predicate[K, V] {
override def test(key: K, value: V): Boolean = p(key, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package kstream

import org.apache.kafka.streams.KeyValue
import org.apache.kafka.streams.kstream.{KStream => KStreamJ, _}
import org.apache.kafka.streams.processor.{Processor, ProcessorContext, ProcessorSupplier, TopicNameExtractor}
import org.apache.kafka.streams.processor.{Processor, ProcessorSupplier, TopicNameExtractor}
import org.apache.kafka.streams.scala.ImplicitConversions._
import org.apache.kafka.streams.scala.FunctionConversions._

Expand Down Expand Up @@ -84,10 +84,8 @@ class KStream[K, V](val inner: KStreamJ[K, V]) {
* @return a [[KStream]] that contains records with new key and value (possibly both of different type)
* @see `org.apache.kafka.streams.kstream.KStream#map`
*/
def map[KR, VR](mapper: (K, V) => (KR, VR)): KStream[KR, VR] = {
val kvMapper = mapper.tupled andThen tuple2ToKeyValue
inner.map[KR, VR](((k: K, v: V) => kvMapper(k, v)).asKeyValueMapper)
}
def map[KR, VR](mapper: (K, V) => (KR, VR)): KStream[KR, VR] =
inner.map[KR, VR](mapper.asKeyValueMapper)

/**
* Transform the value of each input record into a new value (with possible new type) of the output record.
Expand Down Expand Up @@ -124,7 +122,7 @@ class KStream[K, V](val inner: KStreamJ[K, V]) {
* @see `org.apache.kafka.streams.kstream.KStream#flatMap`
*/
def flatMap[KR, VR](mapper: (K, V) => Iterable[(KR, VR)]): KStream[KR, VR] = {
val kvMapper = mapper.tupled andThen (iter => iter.map(tuple2ToKeyValue).asJava)
val kvMapper = mapper.tupled.andThen(_.map(tuple2ToKeyValue).asJava)
inner.flatMap[KR, VR](((k: K, v: V) => kvMapper(k, v)).asKeyValueMapper)
}

Expand Down Expand Up @@ -173,7 +171,7 @@ class KStream[K, V](val inner: KStreamJ[K, V]) {
* @see `org.apache.kafka.streams.kstream.KStream#foreach`
*/
def foreach(action: (K, V) => Unit): Unit =
inner.foreach((k: K, v: V) => action(k, v))
inner.foreach(action.asForeachAction)

/**
* Creates an array of `KStream` from this stream by branching the records in the original stream based on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ class KStreamTest extends FlatSpec with Matchers with TestDriver {
testDriver.close()
}

"foreach a KStream" should "side effect records" in {
val builder = new StreamsBuilder()
val sourceTopic = "source"

var acc = ""

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😉

builder.stream[String, String](sourceTopic).foreach((_, value) => acc += value)

val testDriver = createTestDriver(builder)

testDriver.pipeRecord(sourceTopic, ("1", "value1"))
testDriver.pipeRecord(sourceTopic, ("2", "value2"))
acc shouldBe "value1value2"

testDriver.close()
}

"selectKey a KStream" should "select a new key" in {
val builder = new StreamsBuilder()
val sourceTopic = "source"
Expand Down