SHARE
TWEET

Untitled

a guest Sep 17th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import akka.stream.scaladsl.{Sink, Source}
  2.  
  3. object MergeExample extends App with Setup {
  4.  
  5.   object SampleOrdering extends Ordering[Sample] {
  6.     override def compare(x: Sample, y: Sample): Int = {
  7.       val result = x.key.compare(y.key)
  8.       if(result == 0) {
  9.         x.timestamp.compareTo(y.timestamp)
  10.       } else {
  11.         result
  12.       }
  13.     }
  14.  
  15.   }
  16.  
  17.   override def main(args: Array[String]): Unit = {
  18.  
  19.     val firstSource = Source(
  20.       List(
  21.         Sample(1, System.currentTimeMillis(), random.nextFloat()),
  22.         Sample(2, System.currentTimeMillis(), random.nextFloat()),
  23.         Sample(3, System.currentTimeMillis(), random.nextFloat()),
  24.         Sample(5, System.currentTimeMillis(), random.nextFloat()),
  25.       )
  26.     )
  27.  
  28.     val secondSource = Source(
  29.       List(
  30.         Sample(2, System.currentTimeMillis(), random.nextFloat()),
  31.         Sample(4, System.currentTimeMillis(), random.nextFloat()),
  32.         Sample(6, System.currentTimeMillis(), random.nextFloat()),
  33.         Sample(7, System.currentTimeMillis(), random.nextFloat()),
  34.       )
  35.     )
  36.  
  37.     implicit val ordering: SampleOrdering.type = SampleOrdering
  38.     firstSource
  39.       .mergeSorted(secondSource)
  40.       .runWith(Sink.foreach(
  41.         println
  42.       ))
  43.   }
  44. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top