2014-12-08 17:29:40 +01:00
|
|
|
package docs.stream.cookbook
|
|
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
import akka.NotUsed
|
2015-11-25 19:58:48 +01:00
|
|
|
import akka.stream.{ Graph, FlowShape, Inlet, Outlet, Attributes, OverflowStrategy }
|
2014-12-08 17:29:40 +01:00
|
|
|
import akka.stream.scaladsl._
|
|
|
|
|
import scala.concurrent.{ Await, Future }
|
|
|
|
|
import scala.concurrent.duration._
|
2015-11-25 19:58:48 +01:00
|
|
|
import akka.stream.stage.{ GraphStage, GraphStageLogic }
|
2014-12-08 17:29:40 +01:00
|
|
|
|
|
|
|
|
class RecipeReduceByKey extends RecipeSpec {
|
|
|
|
|
|
|
|
|
|
"Reduce by key recipe" must {
|
|
|
|
|
|
2014-12-19 11:39:41 +01:00
|
|
|
val MaximumDistinctWords = 1000
|
|
|
|
|
|
2014-12-08 17:29:40 +01:00
|
|
|
"work with simple word count" in {
|
|
|
|
|
|
|
|
|
|
def words = Source(List("hello", "world", "and", "hello", "universe", "akka") ++ List.fill(1000)("rocks!"))
|
|
|
|
|
|
|
|
|
|
//#word-count
|
2016-01-20 10:00:37 +02:00
|
|
|
val counts: Source[(String, Int), NotUsed] = words
|
2015-11-25 19:58:48 +01:00
|
|
|
// split the words into separate streams first
|
|
|
|
|
.groupBy(MaximumDistinctWords, identity)
|
2016-01-15 22:51:26 -05:00
|
|
|
//transform each element to pair with number of words in it
|
|
|
|
|
.map(_ -> 1)
|
2015-11-25 19:58:48 +01:00
|
|
|
// add counting logic to the streams
|
2016-01-15 22:51:26 -05:00
|
|
|
.reduce((l, r) => (l._1, l._2 + r._2))
|
2015-11-25 19:58:48 +01:00
|
|
|
// get a stream of word counts
|
|
|
|
|
.mergeSubstreams
|
2014-12-08 17:29:40 +01:00
|
|
|
//#word-count
|
|
|
|
|
|
2015-03-05 12:21:17 +01:00
|
|
|
Await.result(counts.grouped(10).runWith(Sink.head), 3.seconds).toSet should be(Set(
|
2014-12-08 17:29:40 +01:00
|
|
|
("hello", 2),
|
|
|
|
|
("world", 1),
|
|
|
|
|
("and", 1),
|
|
|
|
|
("universe", 1),
|
|
|
|
|
("akka", 1),
|
|
|
|
|
("rocks!", 1000)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"work generalized" in {
|
|
|
|
|
|
|
|
|
|
def words = Source(List("hello", "world", "and", "hello", "universe", "akka") ++ List.fill(1000)("rocks!"))
|
|
|
|
|
|
|
|
|
|
//#reduce-by-key-general
|
|
|
|
|
def reduceByKey[In, K, Out](
|
2014-12-19 11:39:41 +01:00
|
|
|
maximumGroupSize: Int,
|
2014-12-08 17:29:40 +01:00
|
|
|
groupKey: (In) => K,
|
2016-01-15 22:51:26 -05:00
|
|
|
map: (In) => Out)(reduce: (Out, Out) => Out): Flow[In, (K, Out), NotUsed] = {
|
2014-12-08 17:29:40 +01:00
|
|
|
|
2015-11-25 19:58:48 +01:00
|
|
|
Flow[In]
|
2016-01-15 22:51:26 -05:00
|
|
|
.groupBy[K](maximumGroupSize, groupKey)
|
|
|
|
|
.map(e => groupKey(e) -> map(e))
|
|
|
|
|
.reduce((l, r) => l._1 -> reduce(l._2, r._2))
|
2015-11-25 19:58:48 +01:00
|
|
|
.mergeSubstreams
|
2014-12-08 17:29:40 +01:00
|
|
|
}
|
|
|
|
|
|
2016-01-15 22:51:26 -05:00
|
|
|
val wordCounts = words.via(
|
|
|
|
|
reduceByKey(MaximumDistinctWords,
|
|
|
|
|
groupKey = (word: String) => word,
|
|
|
|
|
map = (word: String) => 1)((left: Int, right: Int) => left + right))
|
2014-12-08 17:29:40 +01:00
|
|
|
//#reduce-by-key-general
|
|
|
|
|
|
2015-03-05 12:21:17 +01:00
|
|
|
Await.result(wordCounts.grouped(10).runWith(Sink.head), 3.seconds).toSet should be(Set(
|
2014-12-08 17:29:40 +01:00
|
|
|
("hello", 2),
|
|
|
|
|
("world", 1),
|
|
|
|
|
("and", 1),
|
|
|
|
|
("universe", 1),
|
|
|
|
|
("akka", 1),
|
|
|
|
|
("rocks!", 1000)))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|