Working with Akka Streams is nothing less than pure pleasure. I can’t be thankful enough to the Akka team’s folks for this.
This post is just a little example of something I needed a little time figuring out myself. Amongst all the graph operators, Partition
is bizarrely not listed in the documentation at the moment of writing, even though I find it extremely useful. It lets you create some logic like that:
The example that follows is a very simple pipeline:
val oddEvenFilter = GraphDSL.create() { implicit b =>
// logic to dispatch odd and even numbers to different outlets
b.add(Partition[Int](2, n => if (n % 2 == 0) 0 else 1))
}
// this flow uses the above filter
val filterFlow = Flow.fromGraph(GraphDSL.create() { implicit b =>
import GraphDSL.Implicits._
val input = b.add(oddEvenFilter)
val sink1 = Sink.foreach[Int](n => println("even sink received " + n))
input.out(0) ~> sink1
FlowShape(input.in, input.out(1))
})
// connect to source and final sink
Source(1 to 10)
.via(filterFlow)
.map(_ * 2)
.runForeach(n => println("final sink received " + n))
In this case the filter only had two outlets (even or odd numbers), but the graph stage lets you define your own custom logic with as many ports as needed. Very powerful!
All Tags |
The Netherlands |
61
|
amsterdam |
35
|
bicycle |
21
|
Chile |
18
|
Valparaiso |
15
|
Australia |
13
|
Art |
12
|
nepal |
8
|
scala |
8
|
akka |
6
|
Santiago |
5
|
community |
4
|
France |
4
|
Gouda |
4
|
Paris |
4
|
akka-stream |
3
|
akka-streams |
3
|
dashain |
3
|
everest trek |
3
|
india |
3
|
Italy |
3
|
Melbourne |
3
|
Perth |
3
|
Road trip |
3
|
Rotterdam |
3
|
akka-http |
2
|
Argentina |
2
|
bicycle touring |
2
|
code |
2
|
custom_image |
2
|
custom_summary |
2
|
Delft |
2
|
event-sourcing |
2
|
Geraldton |
2
|
Haarlem |
2
|
leaf_bundle |
2
|
Lille |
2
|
Milan |
2
|
New Delhi |
2
|
New York |
2
|
Punta Arenas |
2
|
Rome |
2
|
Ushuaia |
2
|
Websocket |
2
|
Abcoude |
1
|
akka-cluster |
1
|
amazon web services |
1
|
android |
1
|
aws |
1
|
Berlin |
1
|
Bloemendaal |
1
|
Brisbane |
1
|
chitwan |
1
|
Circus Maximum |
1
|
covid19 |
1
|
deep learning |
1
|
distributed systems |
1
|
Enkhuizen |
1
|
Esperance |
1
|
expats |
1
|
fans club |
1
|
Fraser Island |
1
|
gps |
1
|
gpx |
1
|
guitars |
1
|
iot |
1
|
Isla Negra |
1
|
japan |
1
|
java |
1
|
Kalgoorlie |
1
|
kathmandu |
1
|
Las Vegas |
1
|
litfiba |
1
|
LoPy |
1
|
lora |
1
|
Markem |
1
|
Matisse |
1
|
Mexico |
1
|
Middelburg |
1
|
misc |
1
|
Muiden |
1
|
neural networks |
1
|
planning |
1
|
play |
1
|
reactive |
1
|
reactjs |
1
|
refluxjs |
1
|
Reims |
1
|
San Francisco |
1
|
Sodaq |
1
|
Sydney |
1
|
Texel |
1
|
Theo Van Doesburg |
1
|
tokyo |
1
|
Travel |
1
|
tulips |
1
|
USA |
1
|
webjars |
1
|
Weesp |
1
|