All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.landoop.kstreams.kcql.KStreamSAM.scala Maven / Gradle / Ivy

Go to download

Small utility lib for Kafka Streams to help with simple payload transformations

The newest version!
package com.landoop.kstreams.kcql

import org.apache.kafka.streams.KeyValue
import org.apache.kafka.streams.kstream.{KeyValueMapper, Reducer, ValueMapper}

import scala.language.implicitConversions

object KStreamSAM {
  implicit def ValueMapperConverter[T, R](f: (T) => R): ValueMapper[T, R] = {
    new ValueMapper[T, R] {
      override def apply(value: T): R = f(value)
    }
  }

  implicit def ValueMapperConverter[T, R](pf: PartialFunction[T, R]): ValueMapper[T, R] = {
    new ValueMapper[T, R] {
      override def apply(value: T): R = pf(value)
    }
  }

  implicit def Tuple2ToKeyValue[K, V](tuple: (K, V)): KeyValue[K, V] = {
    new KeyValue(tuple._1, tuple._2)
  }


  implicit def Tuple2ToKeyValueMapper[K, V, KR, VR](fn: (K, V) => (KR, VR)): KeyValueMapper[K, V, KeyValue[KR, VR]] = {
    new KeyValueMapper[K, V, KeyValue[KR, VR]] {
      override def apply(key: K, value: V): KeyValue[KR, VR] = {
        Tuple2ToKeyValue(fn(key, value))
      }
    }
  }

  implicit def functionToReducer[V](f: ((V, V) => V)): Reducer[V] = new Reducer[V] {
    override def apply(l: V, r: V): V = f(l, r)
  }


  /* implicit def ValueMapperConverterFunction[T, R](f: Function1[T, R]): ValueMapper[T, R] = {
     new ValueMapper[T, R] {
       override def apply(value: T): R = f(value)
     }
   }*/


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy